Struct iron_shapes::simple_polygon::SimplePolygon
source · pub struct SimplePolygon<T> {
points: Vec<Point<T>>,
normalized: bool,
}Expand description
A SimplePolygon is a polygon defined by vertices. It does not contain holes but can be
self-intersecting.
TODO: Implement Deref for accessing the vertices.
Fields§
§points: Vec<Point<T>>Vertices of the polygon.
normalized: boolSet to true only if the vertices are normalized.
Implementations§
source§impl<T> SimplePolygon<T>
impl<T> SimplePolygon<T>
source§impl<T: PartialOrd> SimplePolygon<T>
impl<T: PartialOrd> SimplePolygon<T>
sourcepub fn new(points: Vec<Point<T>>) -> Self
pub fn new(points: Vec<Point<T>>) -> Self
Create a new polygon from a list of points. The polygon is normalized by rotating the points.
sourcepub fn with_points_mut<R>(
&mut self,
f: impl FnOnce(&mut Vec<Point<T>>) -> R
) -> R
pub fn with_points_mut<R>( &mut self, f: impl FnOnce(&mut Vec<Point<T>>) -> R ) -> R
Mutably access the inner list of points. If the polygon was normalized, then the modified list of points will be normalized again.
source§impl<T: PartialOrd> SimplePolygon<T>
impl<T: PartialOrd> SimplePolygon<T>
sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
sourcepub fn normalized(self) -> Self
pub fn normalized(self) -> Self
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
source§impl<T: PartialEq> SimplePolygon<T>
impl<T: PartialEq> SimplePolygon<T>
sourcepub fn normalized_eq(&self, other: &Self) -> bool
pub fn normalized_eq(&self, other: &Self) -> bool
Check if polygons can be made equal by rotating their vertices.
source§impl<T: Copy> SimplePolygon<T>
impl<T: Copy> SimplePolygon<T>
source§impl<T> SimplePolygon<T>
impl<T> SimplePolygon<T>
source§impl<T: Copy> SimplePolygon<T>
impl<T: Copy> SimplePolygon<T>
sourcefn iter_cycle(&self) -> impl Iterator<Item = &Point<T>>
fn iter_cycle(&self) -> impl Iterator<Item = &Point<T>>
Get an iterator over the polygon points. Point 0 is appended to the end to close the cycle.
sourcepub fn edges(&self) -> Vec<Edge<T>>
pub fn edges(&self) -> Vec<Edge<T>>
Get all exterior edges of the polygon.
Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::edge::Edge;
let coords = vec![(0, 0), (1, 0)];
let poly = SimplePolygon::from(coords);
assert_eq!(poly.edges(), vec![Edge::new((0, 0), (1, 0)), Edge::new((1, 0), (0, 0))]);
sourcepub fn edges_iter(&self) -> impl Iterator<Item = Edge<T>> + '_
pub fn edges_iter(&self) -> impl Iterator<Item = Edge<T>> + '_
Iterate over all edges.
source§impl<T: CoordinateType> SimplePolygon<T>
impl<T: CoordinateType> SimplePolygon<T>
sourcepub fn normalize_orientation<Area>(&mut self)where
Area: Num + PartialOrd + From<T>,
pub fn normalize_orientation<Area>(&mut self)where Area: Num + PartialOrd + From<T>,
Normalize the points of the polygon such that they are arranged counter-clock-wise.
After normalizing, SimplePolygon::area_doubled_oriented() will return a semi-positive value.
For self-intersecting polygons, the orientation is not clearly defined. For example an 8 shape
has not orientation.
Here, the oriented area is used to define the orientation.
sourcepub fn normalized_orientation<Area>(self) -> Selfwhere
Area: Num + PartialOrd + From<T>,
pub fn normalized_orientation<Area>(self) -> Selfwhere Area: Num + PartialOrd + From<T>,
Call normalize_orientation() while taking ownership and returning the result.
sourcepub fn orientation<Area>(&self) -> Orientationwhere
Area: Num + From<T> + PartialOrd,
pub fn orientation<Area>(&self) -> Orientationwhere Area: Num + From<T> + PartialOrd,
Get the orientation of the polygon. The orientation is defined by the oriented area. A polygon with a positive area is oriented counter-clock-wise, otherwise it is oriented clock-wise.
Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::point::Point;
use iron_shapes::types::Orientation;
let coords = vec![(0, 0), (3, 0), (3, 1)];
let poly = SimplePolygon::from(coords);
assert_eq!(poly.orientation::<i64>(), Orientation::CounterClockWise);
sourcepub fn convex_hull(&self) -> SimplePolygon<T>where
T: Ord,
pub fn convex_hull(&self) -> SimplePolygon<T>where T: Ord,
Get the convex hull of the polygon.
Implements Andrew’s Monotone Chain algorithm. See: http://geomalgorithms.com/a10-_hull-1.html
sourcepub fn is_rectilinear(&self) -> bool
pub fn is_rectilinear(&self) -> bool
Test if all edges are parallel to the x or y axis.
sourcepub fn lower_left_vertex(&self) -> Point<T>
pub fn lower_left_vertex(&self) -> Point<T>
Get the vertex with lowest x-coordinate. Prefer lower y-coordinates to break ties.
Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::point::Point;
let coords = vec![(0, 0), (1, 0), (-1, 2), (-1, 1)];
let poly = SimplePolygon::from(coords);
assert_eq!(poly.lower_left_vertex(), Point::new(-1, 1));
sourcefn lower_left_vertex_with_index(&self) -> (usize, Point<T>)
fn lower_left_vertex_with_index(&self) -> (usize, Point<T>)
Get the vertex with lowest x-coordinate and its index. Prefer lower y-coordinates to break ties.
Trait Implementations§
source§impl<T: Clone> Clone for SimplePolygon<T>
impl<T: Clone> Clone for SimplePolygon<T>
source§fn clone(&self) -> SimplePolygon<T>
fn clone(&self) -> SimplePolygon<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<T: Debug> Debug for SimplePolygon<T>
impl<T: Debug> Debug for SimplePolygon<T>
source§impl<'de, T> Deserialize<'de> for SimplePolygon<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for SimplePolygon<T>where T: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,
source§impl<A, T> DoubledOrientedArea<A> for SimplePolygon<T>where
T: CoordinateType,
A: Num + From<T>,
impl<A, T> DoubledOrientedArea<A> for SimplePolygon<T>where T: CoordinateType, A: Num + From<T>,
source§fn area_doubled_oriented(&self) -> A
fn area_doubled_oriented(&self) -> A
Calculates the doubled oriented area.
Using doubled area allows to compute in the integers because the area of a polygon with integer coordinates is either integer or half-integer.
The area will be positive if the vertices are listed counter-clockwise, negative otherwise.
Complexity: O(n)
Examples
use iron_shapes::traits::DoubledOrientedArea;
use iron_shapes::simple_polygon::SimplePolygon;
let coords = vec![(0, 0), (3, 0), (3, 1)];
let poly = SimplePolygon::from(coords);
let area: i64 = poly.area_doubled_oriented();
assert_eq!(area, 3);
source§impl<T> From<&SimplePolygon<T>> for Polygon<T>where
T: Copy,
impl<T> From<&SimplePolygon<T>> for Polygon<T>where T: Copy,
Create a polygon from a simple polygon.
source§fn from(simple_polygon: &SimplePolygon<T>) -> Self
fn from(simple_polygon: &SimplePolygon<T>) -> Self
source§impl<I, T, P> From<I> for SimplePolygon<T>where
T: Copy + PartialOrd,
I: IntoIterator<Item = P>,
Point<T>: From<P>,
impl<I, T, P> From<I> for SimplePolygon<T>where T: Copy + PartialOrd, I: IntoIterator<Item = P>, Point<T>: From<P>,
Create a polygon from a type that is convertible into an iterator of values convertible to Points.
source§impl<T> From<SimplePolygon<T>> for Geometry<T>
impl<T> From<SimplePolygon<T>> for Geometry<T>
source§fn from(x: SimplePolygon<T>) -> Geometry<T>
fn from(x: SimplePolygon<T>) -> Geometry<T>
source§impl<T> From<SimplePolygon<T>> for Polygon<T>
impl<T> From<SimplePolygon<T>> for Polygon<T>
Create a polygon from a simple polygon.
source§fn from(simple_polygon: SimplePolygon<T>) -> Self
fn from(simple_polygon: SimplePolygon<T>) -> Self
source§impl<T, P> FromIterator<P> for SimplePolygon<T>where
T: Copy,
P: Into<Point<T>>,
impl<T, P> FromIterator<P> for SimplePolygon<T>where T: Copy, P: Into<Point<T>>,
Create a polygon from a iterator of values convertible to Points.
source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = P>,
fn from_iter<I>(iter: I) -> Selfwhere I: IntoIterator<Item = P>,
source§impl<T: Hash> Hash for SimplePolygon<T>
impl<T: Hash> Hash for SimplePolygon<T>
source§impl<T> MapPointwise<T> for SimplePolygon<T>where
T: CoordinateType,
impl<T> MapPointwise<T> for SimplePolygon<T>where T: CoordinateType,
source§impl<T: PartialEq> PartialEq<SimplePolygon<T>> for SimplePolygon<T>
impl<T: PartialEq> PartialEq<SimplePolygon<T>> for SimplePolygon<T>
source§fn eq(&self, other: &SimplePolygon<T>) -> bool
fn eq(&self, other: &SimplePolygon<T>) -> bool
self and other values to be equal, and is used
by ==.source§impl<T> Serialize for SimplePolygon<T>where
T: Serialize,
impl<T> Serialize for SimplePolygon<T>where T: Serialize,
source§impl<T> TryBoundingBox<T> for SimplePolygon<T>where
T: Copy + PartialOrd,
impl<T> TryBoundingBox<T> for SimplePolygon<T>where T: Copy + PartialOrd,
source§fn try_bounding_box(&self) -> Option<Rect<T>>
fn try_bounding_box(&self) -> Option<Rect<T>>
source§impl<T: CoordinateType + NumCast, Dst: CoordinateType + NumCast> TryCastCoord<T, Dst> for SimplePolygon<T>
impl<T: CoordinateType + NumCast, Dst: CoordinateType + NumCast> TryCastCoord<T, Dst> for SimplePolygon<T>
source§impl<T> WindingNumber<T> for SimplePolygon<T>where
T: CoordinateType,
impl<T> WindingNumber<T> for SimplePolygon<T>where T: CoordinateType,
source§fn winding_number(&self, point: Point<T>) -> isize
fn winding_number(&self, point: Point<T>) -> isize
Calculate the winding number of the polygon around this point.
TODO: Define how point on edges and vertices is handled.