Public
Edited
Jan 23, 2024
Importers
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function intersect_sphere(origin, dir, center, radius)
{
const a = lensq(dir); // If dir is normalized, than we can just assume this is 1
const b = dot(dir, sub(origin, center));
const c = lensq(sub(origin, center)) - radius * radius;
if(b*b - a*c < 0)
return [];
let t0 = (-b - Math.sqrt(b*b - a*c)) / a;
let t1 = (-b + Math.sqrt(b*b - a*c)) / a;
if(t1 < 0)
return [];
else if(t0 < 0)
return [t1];
else if(t1 != t0)
return [t0, t1];
else
return [t0];
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more