Struct physdes::polygon::Polygon

source ·
pub struct Polygon<T> {
    pub origin: Point<T>,
    pub vecs: Vec<Vector2<T>>,
}
Expand description

The Polygon struct represents a polygon with a generic type T and contains an origin point and a vector of 2D vectors.

Properties:

  • origin: The origin property represents the starting point or the reference point of the polygon. It is of type Point, where T is the generic type parameter of the Polygon struct.
  • vecs: vecs is a vector that stores the vectors representing the sides of the polygon. Each vector represents the direction and magnitude of a side of the polygon.

Fields§

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

Implementations§

source§

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

source

pub fn new(coords: &[Point<T>]) -> Self

The new function constructs a new Polygon object by calculating the vectors between each coordinate and the origin.

Arguments:

  • coords: An array slice of Point objects, representing the coordinates of the polygon. The first element of the slice is considered the origin of the polygon, and the remaining elements are treated as vectors relative to the origin.

Returns:

The new function returns a new instance of the Polygon object.

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 signed_area_x2(&self) -> T

The signed_area_x2 function calculates the signed area multiplied by 2 of a polygon.

Returns:

The function signed_area_x2 returns the signed area multiplied by 2. Signed area x 2

Panics

Panics if n < 2

Returns:

The new function returns a new instance of the Polygon object.

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 lb(&self) -> Point<T>

The function lb returns a Point<T>.

Returns:

a value of type Point<T>.

source

pub fn ub(&self) -> Point<T>

The function ub returns a Point<T>.

Returns:

a value of type Point<T>.

source§

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

source

pub fn create_mono_polygon<F>(pointset: &[Point<T>], f: F) -> Vec<Point<T>>where F: Fn(&&Point<T>) -> (T, T),

The function create_mono_polygon takes a set of points and a function, and returns a sorted list of points that form a monotonic polygon.

Arguments:

  • pointset: pointset is a slice of Point objects, representing a set of points in a 2D space.
  • f: The parameter f is a closure that takes a reference to a Point<T> and returns a tuple (T, T). It is used to determine the ordering of the points in the polygon.

Returns:

The function create_mono_polygon returns a Vec<Point<T>>, which is a vector of points. Create a x-mono Polygon object

source

pub fn create_xmono_polygon(pointset: &[Point<T>]) -> Vec<Point<T>>

The function create_xmono_polygon creates a monotone polygon object using a given point set, with the x-coordinate as the primary sorting criterion.

Arguments:

  • pointset: A slice of Point objects, where each Point object has xcoord and ycoord properties.

Returns:

The function create_xmono_polygon returns a vector of Point<T>.

source

pub fn create_ymono_polygon(pointset: &[Point<T>]) -> Vec<Point<T>>

The function creates a y-monotone polygon object using a given point set.

Arguments:

  • pointset: A slice of Point objects, where each Point object has two fields: ycoord and xcoord.

Returns:

The function create_ymono_polygon returns a vector of Point<T> objects.

source

pub fn point_in_polygon(pointset: &[Point<T>], q: &Point<T>) -> bool

The function point_in_polygon determines if a given point is within a polygon.

The code below is from Wm. Randolph Franklin wrf@ecse.rpi.edu (see URL below) with some minor modifications for integer. It returns true for strictly interior points, false for strictly exterior, and ub for points on the boundary. The boundary behavior is complex but determined; in particular, for a partition of a region into polygons, each Point is “in” exactly one Polygon. (See p.243 of [O’Rourke (C)] for a discussion of boundary behavior.)

See http://www.faqs.org/faqs/graphics/algorithms-faq/ Subject 2.03

Arguments:

  • pointset: A slice of points representing the vertices of the polygon. Each point has x and y coordinates.
  • q: The parameter q represents the point that we want to determine if it is within the polygon or not.

Returns:

The function point_in_polygon returns a boolean value. It returns true if the given point q is strictly inside the polygon defined by the pointset array, false if the point is strictly outside the polygon, and ub (undefined behavior) if the point lies on the boundary of the polygon.

Auto Trait Implementations§

§

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> UnwindSafe for Polygon<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.