1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use core::f64::consts::PI; use crate::Polygon; /// Struct that represents a circle. #[derive(Debug)] pub struct Circle { pub radius: f64, } impl Circle { /// Returns a new Circle from given radius *r*. /// # Examples /// ``` /// let circle = polys::Circle::new(5.0); /// ``` pub fn new(r: f64) -> Option<Circle> { if r == 0.0 { None } else { Some(Circle { radius: r }) } } } impl Polygon for Circle { /// Gets the area of the Circle from its radius. /// # Examples /// ``` /// use crate::polys::Polygon; /// use std::f64::consts::PI; /// /// let circle = polys::Circle::new(5.0) /// .expect("Could not make Circle"); /// let area = circle.area().expect("Is none"); /// assert_eq!(area, PI * 25.0); /// ``` fn area(&self) -> Option<f64> { Some(PI * self.radius * self.radius) } /// Gets the circumferance of the Circle from its radius. /// # Examples /// ``` /// use crate::polys::Polygon; /// use core::f64::consts::PI; /// /// let circle = polys::Circle::new(5.0) /// .expect("Could not make Circle"); /// let peri = circle.peri().expect("Is none"); /// assert_eq!(peri, 10.0*PI); /// ``` fn peri(&self) -> Option<f64> { Some(2.0 * PI * self.radius) } /// Returns None. /// # Examples /// ``` /// use crate::polys::Polygon; /// let circle = polys::Circle::new(5.0) /// .expect("Could not make Circle"); /// let angles = circle.angles(); /// assert!(angles.is_none()); /// ``` fn angles(&self) -> Option<Vec<f64>> { None } }