code wiki / _hdl_build / nx_viz_voronoi.nx
nx_viz_voronoi.nx source
↩ module page · 21 lines · 968 B
1// nx_viz_voronoi.nx -- the VORONOI layer of the sovereign Nishi viz library (the d3-delaunay nearest-site core,
2// bits-up). The Voronoi diagram IS the nearest-site map: vd_nearest returns the site owning a query point (by
3// SQUARED distance -- no sqrt, exact). Raster a grid through it = the Voronoi cells; it is also d3's delaunay.find.
4// Closes geom-voronoi (raster/nearest-site; vector Delaunay triangulation is the noted refinement). license_tier: ORIGINAL
5import "nx_syscalls.nx"
6
7func vd_nearest(sx: *i64, sy: *i64, n: i64, px: i64, py: i64) -> i64 {
8 if n <= 0 { return 0 - 1 }
9 var best: i64 = 0
10 let dx0: i64 = px - sx[0]; let dy0: i64 = py - sy[0]
11 var bestd: i64 = dx0 * dx0 + dy0 * dy0
12 var i: i64 = 1
13 while i < n {
14 let dx: i64 = px - sx[i]; let dy: i64 = py - sy[i]
15 let d: i64 = dx * dx + dy * dy
16 if d < bestd { bestd = d; best = i }
17 i = i + 1
18 }
19 return best
20}
21func main() -> i64 { return 0 }