#! Contains `sphere`, the places within a distance of a center: what holds a #! body for the quickest test of whether two might meet, their distance against #! the sum of their radii. import linear; module geometry; #! The places within `radius` of `center`. pub struct sphere { pub var center: vec3; pub var radius: f32; } #! The least sphere holding a box: of its center, reaching its corners. pub def sphere::of(b: bounds): sphere { return sphere{ b.center, b.half_size.length }; } pub def sphere::equals(&this, other: &sphere): bool { return this.center == other.center && this.radius == other.radius; } #! Whether a place is within it. pub def sphere::contains(&this, p: vec3): bool { return distance_squared(&p, &this.center) <= this.radius * this.radius; } #! Whether it and another sphere meet. pub def sphere::overlaps(&this, other: sphere): bool { val reach := this.radius + other.radius; return distance_squared(&other.center, &this.center) <= reach * reach; } #! Whether it and a box meet: whether the box's place nearest its center is #! within it. pub def sphere::overlaps(&this, b: bounds): bool { return this.contains(b.closest(this.center)); }