A sphere or a circle is positioned and oriented to intersect with three given points.
Optionally replace sphere
with circle
in line 34.
// 3D INTERSECTION
function vector intersection(vector r1, r2, e1, e2){
float u = dot(e1, e2);
float t1 = dot(r2 - r1, e1);
float t2 = dot(r2 - r1, e2);
float d1 = (t1 - u * t2) / (1 - u * u);
float d2 = (t2 - u * t1) / (u * u - 1);
vector p1 = r1 + e1 * d1;
vector p2 = r2 + e2 * d2;
vector pos_center = (p1 + p2) * 0.5;
return pos_center;
}
// INPUT POSITIONS
vector pos_A = point(0, 'P', 0);
vector pos_B = point(0, 'P', 1);
vector pos_C = point(0, 'P', 2);
vector mid_AB = (pos_A + pos_B) * 0.5;
vector mid_BC = (pos_B + pos_C) * 0.5;
// DIRECTIONS
vector dir_BA = normalize(pos_B - pos_A);
vector dir_BC = normalize(pos_B - pos_C);
vector dir_rect = normalize(cross(dir_BA, dir_BC));
vector dir_BA_rect = normalize(cross(dir_BA, dir_rect));
vector dir_BC_rect = normalize(cross(dir_BC, dir_rect));
// ADD SPHERE
vector pos_center = intersection(mid_AB, mid_BC, dir_BA_rect, dir_BC_rect);
float radius = distance(pos_center, pos_A);
int pt_add = addpoint(0, pos_center);
int sphere_add = addprim(0, 'sphere', pt_add);
matrix3 xform_sphere = dihedral({0,0,1}, dir_rect);
scale(xform_sphere, radius);
setprimintrinsic(0, 'transform', sphere_add, xform_sphere);