Created
November 25, 2015 13:34
-
-
Save LLBlumire/2ff64430a4c83fa9c34e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! # Polygon | |
//! | |
//! A standard trait and set of implementations for managing polygons in rust. | |
//! | |
//! ## Output Angles | |
//! For return values 0 is north and the angle increases clockwise. | |
//! | |
//! ## Output Gradients | |
//! For output gradients, f64::INFINITY is returned for north, and the gradient decreases | |
//! clockwise until f64::NEG_INFINITY. | |
/// Functions for manipulating a polygon. | |
pub trait Polygon: Sized { | |
// Required Implementations | |
/// translate(x, y) | |
/// Translate polygon by (x, y). | |
fn translate(&self, f64, f64) -> Self; | |
/// rot_rad(r) | |
/// Rotate polygon about (0, 0) by r radians. | |
fn rotate_radians(&self, f64) -> Self; | |
/// Get a slice of all of the angles of lines present in the polygon in radians | |
fn angles_radians(&self) -> Vec<f64>; | |
/// collides_with(&rhs) | |
/// Check if polygon collides with another polygon (rhs). | |
fn collides_with<P: Polygon>(&self, &P) -> Self; | |
// Derived Implementations | |
/// Translate polygon by (x, 0) | |
fn translate_x(&self, x: f64) -> Self { self.translate(x, 0.0) } | |
/// Translate polygon by (0, y) | |
fn translate_y(&self, y: f64) -> Self { self.translate(0.0, y) } | |
/// Rotate polygon about (0, 0) by r degrees. | |
fn rotate_degrees(&self, r: f64) -> Self { self.rotate_radians(r.to_radians()) } | |
/// Get a slice of all the angles of lines present in the polygon in degrees | |
fn angles_degrees(& self) -> Vec<f64> { | |
self.angles_radians().iter().map(|a| a.to_degrees()).collect() | |
} | |
/// Get a slice of all of the angles of lines present in the polygon as a gradients | |
fn angles_gradients(&self) -> Vec<f64> { | |
self.angles_radians().iter().map(|a| a.tan().powi(-1)).collect() | |
} | |
/// Get a slice of all of the angles of normals to lines present in the polygon in radians | |
fn normals_radians(&self) -> Vec<f64> { | |
self.angles_radians().iter().map(|a| a + std::f64::consts::PI).collect() | |
} | |
/// Get a slice of all of the angles of normals to lines present in the polygon in degrees | |
fn normals_degrees(&self) -> Vec<f64> { | |
self.normals_radians().iter().map(|a| a.to_degrees()).collect() | |
} | |
/// Get a slice of all of the angles of normals to lines present in the polygon as a gradients | |
fn normals_gradients(&self) -> Vec<f64> { | |
self.normals_radians().iter().map(|a| -a.tan()).collect() | |
} | |
/// Rotate polygon about (x, y) by r radians. | |
fn rotate_about_radians(&self, x: f64, y: f64, r: f64) -> Self { | |
self.translate(-x, -y).rotate_radians(r).translate(x, y) | |
} | |
/// Rotate polygon about (x, y) by r degrees. | |
fn rotate_about_degrees(&self, x: f64, y: f64, r: f64) -> Self { | |
self.rotate_about_radians(x, y, r.to_radians()) | |
} | |
// Aliases | |
/// Alias for Polygon::translate | |
fn trans(&self, x: f64, y: f64) -> Self { self.translate(x, y) } | |
/// Alias for Polygon::rotate_radians | |
fn rot_rad(&self, r: f64) -> Self { self.rotate_radians(r) } | |
/// Alias for Polygon::translate_x | |
fn trans_x(&self, x: f64) -> Self { self.translate_x(x) } | |
/// Alias for Polygon::translate_y | |
fn trans_y(&self, y: f64) -> Self { self.translate_y(y) } | |
/// Alias for Polygon::rotate_degrees | |
fn rot_deg(&self, r: f64) -> Self { self.rotate_degrees(r) } | |
/// Alias for Polygon::rotate_about_radians | |
fn rot_about_rad(&self, x: f64, y: f64, r: f64) -> Self { self.rotate_about_radians(x, y, r) } | |
/// Alias for Polygon::rotate_about_degrees | |
fn rot_about_deg(&self, x: f64, y: f64, r: f64) -> Self { self.rotate_about_degrees(x, y, r) } | |
/// Alias for Polygon::angles_radians | |
fn ang_rad(&self) -> Vec<f64> { self.angles_radians() } | |
/// Alias for Polygon::angles_degrees | |
fn ang_deg(&self) -> Vec<f64> { self.angles_degrees() } | |
/// Alias for Polygon::angles_gradients | |
fn ang_grad(&self) -> Vec<f64> { self.angles_gradients() } | |
/// Alias for Polygon::normals_radians | |
fn norm_rad(&self) -> Vec<f64> { self.normals_radians() } | |
/// Alias for Polygon::normals_degrees | |
fn norm_deg(&self) -> Vec<f64> { self.normals_degrees() } | |
/// Alias for Polygon::normals_gradients | |
fn norm_grad(&self) -> Vec<f64> { self.normals_gradients() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment