pub struct Polygon<T> {
pub origin: Point<T, T>,
pub vecs: Vec<Vector2<T, T>>,
}Expand description
Represents an arbitrary polygon with coordinates of type T
The Polygon struct stores the origin point and a vector of edges that define the polygon.
It provides various operations and functionalities for working with polygons, such as
area calculation, point containment checks, and geometric property verification.
Properties:
origin: The origin point of the polygonvecs: Vector of displacement vectors from origin to other vertices
Fields§
§origin: Point<T, T>§vecs: Vec<Vector2<T, T>>Implementations§
Source§impl<T: Clone + Num + Ord + Copy + AddAssign> Polygon<T>
impl<T: Clone + Num + Ord + Copy + AddAssign> Polygon<T>
Sourcepub fn new(coords: &[Point<T, T>]) -> Self
pub fn new(coords: &[Point<T, T>]) -> Self
Constructs a new Polygon from a set of points
The first point in the slice is used as the origin, and the remaining points are used to construct displacement vectors relative to the origin.
§Arguments
coords- A slice of points representing the vertices of the polygon
§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;
use physdes::vector2::Vector2;
let p1 = Point::new(1, 1);
let p2 = Point::new(2, 2);
let p3 = Point::new(3, 3);
let p4 = Point::new(4, 4);
let p5 = Point::new(5, 5);
let poly = Polygon::new(&[p1, p2, p3, p4, p5]);
assert_eq!(poly.origin, Point::new(1, 1));
assert_eq!(poly.vecs.len(), 4);
assert_eq!(poly.vecs[0], Vector2::new(1, 1));Sourcepub fn from_origin_and_vectors(
origin: Point<T, T>,
vecs: Vec<Vector2<T, T>>,
) -> Self
pub fn from_origin_and_vectors( origin: Point<T, T>, vecs: Vec<Vector2<T, T>>, ) -> Self
Constructs a new Polygon from origin and displacement vectors
§Arguments
origin- The origin point of the polygonvecs- Vector of displacement vectors from origin
Sourcepub fn from_pointset(pointset: &[Point<T, T>]) -> Self
pub fn from_pointset(pointset: &[Point<T, T>]) -> Self
Constructs a new Polygon from a point set
The first point in the set is used as the origin, and the remaining points are used to construct displacement vectors relative to the origin.
Sourcepub fn add_assign(&mut self, rhs: Vector2<T, T>)where
T: AddAssign,
pub fn add_assign(&mut self, rhs: Vector2<T, T>)where
T: AddAssign,
Translates the polygon by adding a vector to its origin
Sourcepub fn sub_assign(&mut self, rhs: Vector2<T, T>)where
T: SubAssign,
pub fn sub_assign(&mut self, rhs: Vector2<T, T>)where
T: SubAssign,
Translates the polygon by subtracting a vector from its origin
Sourcepub fn signed_area_x2(&self) -> T
pub fn signed_area_x2(&self) -> T
Calculates the signed area of the polygon multiplied by 2
This function calculates the signed area by summing the cross products of adjacent edges. The result is multiplied by 2 to avoid the need for floating-point arithmetic.
§Returns
The signed area of the polygon multiplied by 2
§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;
use physdes::vector2::Vector2;
let p1 = Point::new(1, 1);
let p2 = Point::new(2, 2);
let p3 = Point::new(3, 3);
let p4 = Point::new(4, 4);
let p5 = Point::new(5, 5);
let poly = Polygon::new(&[p1, p2, p3, p4, p5]);
assert_eq!(poly.signed_area_x2(), 0);Sourcepub fn is_rectilinear(&self) -> bool
pub fn is_rectilinear(&self) -> bool
Checks if the polygon is rectilinear
A polygon is rectilinear if all its edges are either horizontal or vertical.
§Returns
true if the polygon is rectilinear, false otherwise
§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;
let p1 = Point::new(0, 0);
let p2 = Point::new(0, 1);
let p3 = Point::new(1, 1);
let p4 = Point::new(1, 0);
let poly = Polygon::new(&[p1, p2, p3, p4]);
assert!(poly.is_rectilinear());
let p5 = Point::new(0, 0);
let p6 = Point::new(1, 1);
let p7 = Point::new(0, 2);
let poly2 = Polygon::new(&[p5, p6, p7]);
assert!(!poly2.is_rectilinear());Sourcepub fn is_anticlockwise(&self) -> boolwhere
T: PartialOrd,
pub fn is_anticlockwise(&self) -> boolwhere
T: PartialOrd,
Checks if the polygon is oriented anticlockwise
Sourcepub fn is_convex(&self) -> boolwhere
T: PartialOrd,
pub fn is_convex(&self) -> boolwhere
T: PartialOrd,
Checks if the polygon is convex
Sourcepub fn bounding_box(&self) -> (Point<T, T>, Point<T, T>)
pub fn bounding_box(&self) -> (Point<T, T>, Point<T, T>)
Gets the bounding box of the polygon
Trait Implementations§
Source§impl<T: AddAssign + Clone + Num> AddAssign<Vector2<T, T>> for Polygon<T>
impl<T: AddAssign + Clone + Num> AddAssign<Vector2<T, T>> for Polygon<T>
Source§fn add_assign(&mut self, rhs: Vector2<T, T>)
fn add_assign(&mut self, rhs: Vector2<T, T>)
+= operation. Read moreSource§impl<T: SubAssign + Clone + Num> SubAssign<Vector2<T, T>> for Polygon<T>
impl<T: SubAssign + Clone + Num> SubAssign<Vector2<T, T>> for Polygon<T>
Source§fn sub_assign(&mut self, rhs: Vector2<T, T>)
fn sub_assign(&mut self, rhs: Vector2<T, T>)
-= operation. Read more