Struct libreda_db::prelude::SimplePolygon
pub struct SimplePolygon<T> {
points: Vec<Point<T>, Global>,
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>, Global>
§normalized: bool
Implementations§
§impl<T> SimplePolygon<T>
impl<T> SimplePolygon<T>
pub fn new_raw(points: Vec<Point<T>, Global>) -> SimplePolygon<T>
pub fn new_raw(points: Vec<Point<T>, Global>) -> SimplePolygon<T>
Create a new polygon from a list of points. The points are taken as they are, without reordering or simplification.
pub fn empty() -> SimplePolygon<T>
pub fn empty() -> SimplePolygon<T>
Create empty polygon without any vertices.
§impl<T> SimplePolygon<T>where
T: PartialOrd<T>,
impl<T> SimplePolygon<T>where T: PartialOrd<T>,
pub fn new(points: Vec<Point<T>, Global>) -> SimplePolygon<T>
pub fn new(points: Vec<Point<T>, Global>) -> SimplePolygon<T>
Create a new polygon from a list of points. The polygon is normalized by rotating the points.
pub fn with_points_mut<R>(
&mut self,
f: impl FnOnce(&mut Vec<Point<T>, Global>) -> R
) -> R
pub fn with_points_mut<R>( &mut self, f: impl FnOnce(&mut Vec<Point<T>, Global>) -> R ) -> R
Mutably access the inner list of points. If the polygon was normalized, then the modified list of points will be normalized again.
§impl<T> SimplePolygon<T>where
T: PartialOrd<T>,
impl<T> SimplePolygon<T>where T: PartialOrd<T>,
pub fn normalize(&mut self)
pub fn normalize(&mut self)
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
pub fn normalized(self) -> SimplePolygon<T>
pub fn normalized(self) -> SimplePolygon<T>
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
§impl<T> SimplePolygon<T>where
T: PartialEq<T>,
impl<T> SimplePolygon<T>where T: PartialEq<T>,
pub fn normalized_eq(&self, other: &SimplePolygon<T>) -> bool
pub fn normalized_eq(&self, other: &SimplePolygon<T>) -> bool
Check if polygons can be made equal by rotating their vertices.
§impl<T> SimplePolygon<T>where
T: Copy,
impl<T> SimplePolygon<T>where T: Copy,
pub fn from_rect(rect: &Rect<T>) -> SimplePolygon<T>
pub fn from_rect(rect: &Rect<T>) -> SimplePolygon<T>
Create a new simple polygon from a rectangle.
§impl<T> SimplePolygon<T>where
T: Copy,
impl<T> SimplePolygon<T>where T: Copy,
pub fn edges(&self) -> Vec<Edge<T>, Global>
pub fn edges(&self) -> Vec<Edge<T>, Global>
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))]);
pub fn edges_iter(&self) -> impl Iterator<Item = Edge<T>>
pub fn edges_iter(&self) -> impl Iterator<Item = Edge<T>>
Iterate over all edges.
§impl<T> SimplePolygon<T>where
T: CoordinateType,
impl<T> SimplePolygon<T>where T: CoordinateType,
pub fn normalize_orientation<Area>(&mut self)where
Area: Num + PartialOrd<Area> + From<T>,
pub fn normalize_orientation<Area>(&mut self)where Area: Num + PartialOrd<Area> + 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.
pub fn normalized_orientation<Area>(self) -> SimplePolygon<T>where
Area: Num + PartialOrd<Area> + From<T>,
pub fn normalized_orientation<Area>(self) -> SimplePolygon<T>where Area: Num + PartialOrd<Area> + From<T>,
Call normalize_orientation()
while taking ownership and returning the result.
pub fn orientation<Area>(&self) -> Orientationwhere
Area: Num + From<T> + PartialOrd<Area>,
pub fn orientation<Area>(&self) -> Orientationwhere Area: Num + From<T> + PartialOrd<Area>,
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);
pub 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
pub fn is_rectilinear(&self) -> bool
pub fn is_rectilinear(&self) -> bool
Test if all edges are parallel to the x or y axis.
pub 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));
Trait Implementations§
§impl<T> Clone for SimplePolygon<T>where
T: Clone,
impl<T> Clone for SimplePolygon<T>where T: Clone,
§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 more§impl<T> Debug for SimplePolygon<T>where
T: Debug,
impl<T> Debug for SimplePolygon<T>where T: Debug,
§impl<'de, T> Deserialize<'de> for SimplePolygon<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for SimplePolygon<T>where T: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D
) -> Result<SimplePolygon<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>( __deserializer: __D ) -> Result<SimplePolygon<T>, <__D as Deserializer<'de>>::Error>where __D: Deserializer<'de>,
§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>,
§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);
§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.
§fn from(simple_polygon: &SimplePolygon<T>) -> Polygon<T>
fn from(simple_polygon: &SimplePolygon<T>) -> Polygon<T>
§impl<I, T, P> From<I> for SimplePolygon<T>where
T: Copy + PartialOrd<T>,
I: IntoIterator<Item = P>,
Point<T>: From<P>,
impl<I, T, P> From<I> for SimplePolygon<T>where T: Copy + PartialOrd<T>, I: IntoIterator<Item = P>, Point<T>: From<P>,
Create a polygon from a type that is convertible into an iterator of values convertible to Point
s.
§fn from(iter: I) -> SimplePolygon<T>
fn from(iter: I) -> SimplePolygon<T>
§impl<T> From<SimplePolygon<T>> for Geometry<T>
impl<T> From<SimplePolygon<T>> for Geometry<T>
§fn from(x: SimplePolygon<T>) -> Geometry<T>
fn from(x: SimplePolygon<T>) -> Geometry<T>
§impl<T> From<SimplePolygon<T>> for Polygon<T>
impl<T> From<SimplePolygon<T>> for Polygon<T>
Create a polygon from a simple polygon.
§fn from(simple_polygon: SimplePolygon<T>) -> Polygon<T>
fn from(simple_polygon: SimplePolygon<T>) -> Polygon<T>
§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 Point
s.
§fn from_iter<I>(iter: I) -> SimplePolygon<T>where
I: IntoIterator<Item = P>,
fn from_iter<I>(iter: I) -> SimplePolygon<T>where I: IntoIterator<Item = P>,
§impl<T> Hash for SimplePolygon<T>where
T: Hash,
impl<T> Hash for SimplePolygon<T>where T: Hash,
§impl<T> MapPointwise<T> for SimplePolygon<T>where
T: CoordinateType,
impl<T> MapPointwise<T> for SimplePolygon<T>where T: CoordinateType,
§impl<T> PartialEq<SimplePolygon<T>> for SimplePolygon<T>where
T: PartialEq<T>,
impl<T> PartialEq<SimplePolygon<T>> for SimplePolygon<T>where T: PartialEq<T>,
§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 ==
.§impl<T> Serialize for SimplePolygon<T>where
T: Serialize,
impl<T> Serialize for SimplePolygon<T>where T: Serialize,
§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>( &self, __serializer: __S ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where __S: Serializer,
§impl<T> TryBoundingBox<T> for SimplePolygon<T>where
T: Copy + PartialOrd<T>,
impl<T> TryBoundingBox<T> for SimplePolygon<T>where T: Copy + PartialOrd<T>,
§fn try_bounding_box(&self) -> Option<Rect<T>>
fn try_bounding_box(&self) -> Option<Rect<T>>
§impl<T, Dst> TryCastCoord<T, Dst> for SimplePolygon<T>where
T: CoordinateType + NumCast,
Dst: CoordinateType + NumCast,
impl<T, Dst> TryCastCoord<T, Dst> for SimplePolygon<T>where T: CoordinateType + NumCast, Dst: CoordinateType + NumCast,
§type Output = SimplePolygon<Dst>
type Output = SimplePolygon<Dst>
§fn try_cast(&self) -> Option<<SimplePolygon<T> as TryCastCoord<T, Dst>>::Output>
fn try_cast(&self) -> Option<<SimplePolygon<T> as TryCastCoord<T, Dst>>::Output>
§impl<T> WindingNumber<T> for SimplePolygon<T>where
T: CoordinateType,
impl<T> WindingNumber<T> for SimplePolygon<T>where T: CoordinateType,
§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.
§fn contains_point_non_oriented(&self, point: Point<T>) -> bool
fn contains_point_non_oriented(&self, point: Point<T>) -> bool
point
is inside the polygon, i.e. the polygons winds around the point
a non-zero number of times. Read more§fn contains_point(&self, point: Point<T>) -> bool
fn contains_point(&self, point: Point<T>) -> bool
point
is inside the polygon, i.e. the polygon winds around the point
an odd number of times. Read more