Skip to content

Distance

Voronoi edges | Inigo Quilez

vex
// run over points
// provide seed points in ancillary port: 1
float search_dist = chf("search_distance");
// first pass
int near_pts[] = nearpoints(1, v@P, search_dist);//from @P
int near_a = near_pts[0];
int near_b = near_pts[1];
vector a = point(1, "P", near_a); //closest cell
vector b = point(1, "P", near_b);
vector ab = normalize(b-a);
vector m = (a + b) * 0.5;
vector mx = m - @P;
float dst = dot(mx, ab);
// second pass
near_pts = nearpoints(1, a, search_dist);//from closest cell
float min_dst = dst;
int closest_c = -1;
foreach (int i; near_pts){
if (i == near_b || i == near_a) continue;
vector c = point(1, "P", i);
vector ac = normalize(c-a);
vector mc = (a+c)/2;
vector mcx = mc - @P;
float dst_c = dot(mcx, ac);
if (dst_c < min_dst){
closest_c = i;
}
min_dst = min(dst_c, min_dst);
}
//f@voro = min_dst;
f@voro = min_dst % 0.15;
i@facet_id = closest_c != -1 ? closest_c : near_b;