pub struct Polygon<T> {
pub origin: Point<T, T>,
pub vecs: Vec<Vector2<T, 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 typePoint<T, T>, 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, T>§vecs: Vec<Vector2<T, T>>Implementations§
Source§impl<T: Clone + Num + Copy + AddAssign> Polygon<T>
impl<T: Clone + Num + Copy + AddAssign> Polygon<T>
Sourcepub fn new(coords: &[Point<T, T>]) -> Self
pub fn new(coords: &[Point<T, 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 ofPoint<T, T>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));Sourcepub fn signed_area_x2(&self) -> T
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§impl<T: Clone + Num + Ord + Copy> Polygon<T>
impl<T: Clone + Num + Ord + Copy> Polygon<T>
Sourcepub fn create_mono_polygon<F>(
pointset: &[Point<T, T>],
f: F,
) -> Vec<Point<T, T>>
pub fn create_mono_polygon<F>( pointset: &[Point<T, T>], f: F, ) -> Vec<Point<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 ofPoint<T, T>objects, representing a set of points in a 2D space.f: The parameterfis a closure that takes a reference to aPoint<T, 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, T>>, which is a vector of points.
Create a x-mono Polygon object
Sourcepub fn create_xmono_polygon(pointset: &[Point<T, T>]) -> Vec<Point<T, T>>
pub fn create_xmono_polygon(pointset: &[Point<T, T>]) -> Vec<Point<T, 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, T>.
Sourcepub fn create_ymono_polygon(pointset: &[Point<T, T>]) -> Vec<Point<T, T>>
pub fn create_ymono_polygon(pointset: &[Point<T, T>]) -> Vec<Point<T, 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, T> objects.
Sourcepub fn point_in_polygon(pointset: &[Point<T, T>], q: &Point<T, T>) -> bool
pub fn point_in_polygon(pointset: &[Point<T, T>], q: &Point<T, 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 parameterqrepresents 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.