[][src]Trait polys::Polygon

pub trait Polygon {
    fn area(&self) -> f64;
fn peri(&self) -> f64;
fn angles(&self) -> Vec<f64>; }

Polygon trait for structs representing 2-dimensional shapes.

Required methods

fn area(&self) -> f64

fn peri(&self) -> f64

fn angles(&self) -> Vec<f64>

Loading content...

Implementors

impl Polygon for Circle[src]

fn area(&self) -> f64[src]

Gets the area of the Circle from its radius.

Examples

use crate::polys::Polygon;
let circle = polys::Circle::new(5.0);
let area = circle.area();
assert_eq!(area, (std::f64::consts::PI * 25f64) as f64);

fn peri(&self) -> f64[src]

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);
let peri = circle.peri();
assert_eq!(peri, 10f64*PI);

fn angles(&self) -> Vec<f64>[src]

Returns an empty vector.

Examples

use crate::polys::Polygon;
let circle = polys::Circle::new(5.0);
let angles = circle.angles();
assert!(angles.is_empty());

impl Polygon for Rect[src]

fn area(&self) -> f64[src]

Gets the area of the Rect according to its width and height.

Examples

use crate::polys::Polygon;

let rect = polys::Rect::new(10.0, 5.0);
let area = rect.area();
assert_eq!(area, 50.0);

fn peri(&self) -> f64[src]

Gets the perimeter of the Rect from its width and height.

Examples

use crate::polys::Polygon;

let rect = polys::Rect::new(10.0, 5.0);
let peri = rect.peri();
assert_eq!(peri, 30f64);

fn angles(&self) -> Vec<f64>[src]

Returns interior angles (all 90) in degrees.

Examples

use crate::polys::Polygon;

let rect = polys::Rect::new(10.0, 5.0);
let angles = rect.angles();
assert_eq!(angles[0], 90f64);

impl Polygon for Reg[src]

fn area(&self) -> f64[src]

Gets the area of the Reg.

Examples

use crate::polys::Polygon;
let reg  = polys::Reg::new(3f64, 5f64);
let area = reg.area();
assert_eq!(area, 15.484296605300704);

This can also be seen by comparing the Rect square and Reg implementations.

use crate::polys::Polygon;
let square = polys::Rect::square(7f64).area();
let reg    = polys::Reg::new(7f64, 4f64) .area();
assert_eq!(square as i32, reg as i32); // casted to i32 because floats suck :)

fn peri(&self) -> f64[src]

Gets the perimeter of the Reg.

Examples

This works on a regular polygon where it has 5 of more sides.

use crate::polys::Polygon;
let reg  = polys::Reg::new(3f64, 5f64);
let peri = reg.peri();
assert_eq!(peri, 15f64);

This can also be seen by comparing the Rect square and Reg implementations.

use crate::polys::Polygon;
let square = polys::Rect::square(7f64).peri();
let reg    = polys::Reg::new(7f64, 4f64) .peri();

fn angles(&self) -> Vec<f64>[src]

Returns the size of the interior angles in degrees.

Examples

use crate::polys::Polygon;
let poly = polys::Reg::pent(12f64);
 

impl Polygon for Tri[src]

fn area(&self) -> f64[src]

Gets the area of the Tri from its sides.

Examples

use crate::polys::Polygon;

let tri = polys::Tri::new(24.0, 30.0, 18.0);
let area = tri.area();
assert_eq!(area, 216 as f64);

fn peri(&self) -> f64[src]

Gets the perimeter of the Tri from its sides.

Examples

use crate::polys::Polygon;

let tri = polys::Tri::new(24.0, 30.0, 18.0);
let peri = tri.peri();
assert_eq!(peri, 72f64);

fn angles(&self) -> Vec<f64>[src]

Returns the a vector of three angles in radians such that angles[0] is the angle opposite side1, angles[1] is the angle opposite side2, and angles[2] is the angle opposite side3.

Examples

use crate::polys::Polygon;
use std::f64::consts::PI;

let tri = polys::Tri::new(24.0, 30.0, 24.0);
let ang: Vec<f64> = tri.angles();
assert_eq!(ang[0]+ang[1]+ang[2], PI);
assert_eq!(ang[0], ang[2]); //iso tri

let tri = polys::Tri::new(12.0, 19.0, 8.0);
let ang = tri.angles();
assert_eq!(ang[0]+ang[1]+ang[2], PI);
 
let tri = polys::Tri::new(12.0, 12.0, 12.0);
let ang = tri.angles();
assert_eq!((ang[0]+ang[1]+ang[2]) as f32, PI as f32);
Loading content...