use nalgebra::{distance, Point3};
use std::f32::consts::FRAC_PI_3;
use std::fmt;
use Container;
use errors::SphericalCowError as Error;
#[derive(PartialEq, Debug, Clone)]
pub struct Sphere {
pub center: Point3<f32>,
pub radius: f32,
}
impl Sphere {
pub fn new(center: Point3<f32>, radius: f32) -> Result<Sphere, Error> {
if radius <= 0.0 {
Err(Error::NegativeRadius)
} else {
Ok(Sphere {
center: center,
radius: radius,
})
}
}
pub fn overlaps(&self, other: &Sphere) -> bool {
distance(&self.center, &other.center) < self.radius + other.radius
}
fn volume(&self) -> f32 {
4. * FRAC_PI_3 * self.radius.powi(3)
}
}
impl Container for Sphere {
fn contains(&self, sphere: &Sphere) -> bool {
distance(&Point3::origin(), &sphere.center) + sphere.radius <= self.radius
}
fn volume(&self) -> f32 {
self.volume()
}
}
impl fmt::Display for Sphere {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"[{{{}, {}, {}}}, {}]",
self.center.coords.x,
self.center.coords.y,
self.center.coords.z,
self.radius
)
}
}