Skip to main content

Polygon

Struct Polygon 

Source
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.

      *-----*-----*
     /     / \   \
    /     /   \   \
   *-----*     *---*
   |  origin
   *--> vecs[0]

Properties:

  • origin: The origin point of the polygon
  • vecs: Vector of displacement vectors from origin to other vertices

§Examples

use physdes::point::Point;
use physdes::polygon::Polygon;
use physdes::vector2::Vector2;

let origin = Point::new(0, 0);
let vecs = vec![Vector2::new(1, 0), Vector2::new(1, 1), Vector2::new(0, 1)];
let poly = Polygon::from_origin_and_vectors(origin, vecs);
assert_eq!(poly.origin, Point::new(0, 0));
assert_eq!(poly.vecs.len(), 3);

Fields§

§origin: Point<T, T>§vecs: Vec<Vector2<T, T>>

Implementations§

Source§

impl<T: Clone + Num + Ord + Copy + AddAssign> Polygon<T>

Source

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));
Source

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 polygon
  • vecs - Vector of displacement vectors from origin
Source

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.

Source

pub fn area(&self) -> T
where T: Sub<Output = T> + AddAssign + Mul<Output = T> + Copy,

Calculates the area of the polygon

Uses the shoelace formula (signed area):

$$A = \frac{1}{2} \sum_{i=0}^{n-1} (x_i y_{i+1} - x_{i+1} y_i)$$

§Returns

The area of the polygon as a value of type T

§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;

// Create a unit square
let points = vec![
    Point::new(0, 0),
    Point::new(1, 0),
    Point::new(1, 1),
    Point::new(0, 1),
];
let poly = Polygon::new(&points);
let area = poly.area();
// Note: area returns signed area (may be negative depending on vertex order)
Source

pub fn vertices(&self) -> Vec<Point<T, T>>

Gets all vertices of the polygon as points

§Returns

A vector of all polygon vertices in order

§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;

let points = vec![
    Point::new(0, 0),
    Point::new(1, 0),
    Point::new(1, 1),
    Point::new(0, 1),
];
let poly = Polygon::new(&points);
let vertices = poly.vertices();
assert_eq!(vertices.len(), 4);
assert_eq!(vertices[0], Point::new(0, 0));
Source

pub fn add_assign(&mut self, rhs: Vector2<T, T>)
where T: AddAssign,

Translates the polygon by adding a vector to its origin.

Source

pub fn sub_assign(&mut self, rhs: Vector2<T, T>)
where T: SubAssign,

Translates the polygon by subtracting a vector from its origin.

Source

pub fn signed_area_x2(&self) -> T

Calculates the signed area of the polygon multiplied by 2

Computes twice the signed area via the shoelace formula:

$$2A = \sum_{i=0}^{n-1} (x_i y_{i+1} - x_{i+1} y_i)$$

This avoids the need for floating-point arithmetic by omitting the $\frac{1}{2}$ factor.

§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);
Source

pub fn get_vertices(&self) -> Vec<Point<T, T>>

Returns all vertices of the polygon as points.

Source

pub fn bounding_box(&self) -> (Point<T, T>, Point<T, T>)
where T: Ord + Copy,

Gets the bounding box of the polygon

§Returns

A tuple of (min_point, max_point) representing the bounding box

§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;

let points = vec![
    Point::new(0, 0),
    Point::new(2, 0),
    Point::new(2, 2),
    Point::new(0, 2),
];
let poly = Polygon::new(&points);
let (min_pt, max_pt) = poly.bounding_box();
assert_eq!(min_pt, Point::new(0, 0));
assert_eq!(max_pt, Point::new(2, 2));
Source

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());
Source

pub fn is_anticlockwise(&self) -> bool
where T: PartialOrd,

Checks if the polygon is oriented anticlockwise

Uses the cross product sign at the minimum-coordinate vertex:

$$\vec{p_{prev}} \times \vec{p_{next}} > 0$$ where $\vec{p} = (current - prev)$ and $\vec{q} = (next - current)$

Source

pub fn is_convex(&self) -> bool
where T: PartialOrd,

Checks if the polygon is convex

A polygon is convex if all its interior angles are less than 180 degrees and no edges bend inward. All consecutive cross products must have the same sign:

$$(v_i - v_{i-1}) \times (v_{i+1} - v_i) \text{ has consistent sign for all } i$$

§Returns

true if the polygon is convex, false otherwise

§Examples
use physdes::point::Point;
use physdes::polygon::Polygon;

// Convex square
let convex_points = vec![
    Point::new(0, 0),
    Point::new(1, 0),
    Point::new(1, 1),
    Point::new(0, 1),
];
let convex_poly = Polygon::new(&convex_points);
assert!(convex_poly.is_convex());

// Concave polygon (L-shape)
let concave_points = vec![
    Point::new(0, 0),
    Point::new(2, 0),
    Point::new(2, 1),
    Point::new(1, 1),
    Point::new(1, 2),
    Point::new(0, 2),
];
let concave_poly = Polygon::new(&concave_points);
assert!(!concave_poly.is_convex());

Trait Implementations§

Source§

impl<T: AddAssign + Clone + Num> AddAssign<Vector2<T, T>> for Polygon<T>

Source§

fn add_assign(&mut self, rhs: Vector2<T, T>)

Translates the polygon by adding a vector to its origin.

Source§

impl<T: Clone> Clone for Polygon<T>

Source§

fn clone(&self) -> Polygon<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Polygon<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for Polygon<T>

Source§

fn default() -> Polygon<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Eq> Eq for Polygon<T>

Source§

impl<T: PartialEq> PartialEq for Polygon<T>

Source§

fn eq(&self, other: &Self) -> bool

Returns true if two polygons have the same origin and vectors.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: SubAssign + Clone + Num> SubAssign<Vector2<T, T>> for Polygon<T>

Source§

fn sub_assign(&mut self, rhs: Vector2<T, T>)

Translates the polygon by subtracting a vector from its origin.

Auto Trait Implementations§

§

impl<T> Freeze for Polygon<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Polygon<T>
where T: RefUnwindSafe,

§

impl<T> Send for Polygon<T>
where T: Send,

§

impl<T> Sync for Polygon<T>
where T: Sync,

§

impl<T> Unpin for Polygon<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Polygon<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Polygon<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.