Struct libreda_db::layout::prelude::SimplePolygon
source · pub struct SimplePolygon<T> { /* private fields */ }
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.
Implementations§
source§impl<T> SimplePolygon<T>
impl<T> SimplePolygon<T>
sourcepub fn new_raw(points: Vec<Point<T>>) -> SimplePolygon<T>
pub fn new_raw(points: Vec<Point<T>>) -> SimplePolygon<T>
Create a new polygon from a list of points. The points are taken as they are, without reordering or simplification.
sourcepub fn empty() -> SimplePolygon<T>
pub fn empty() -> SimplePolygon<T>
Create empty polygon without any vertices.
source§impl<T> SimplePolygon<T>where
T: PartialOrd,
impl<T> SimplePolygon<T>where
T: PartialOrd,
sourcepub fn new(points: Vec<Point<T>>) -> SimplePolygon<T>
pub fn new(points: Vec<Point<T>>) -> SimplePolygon<T>
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> SimplePolygon<T>where
T: PartialOrd,
impl<T> SimplePolygon<T>where
T: PartialOrd,
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) -> SimplePolygon<T>
pub fn normalized(self) -> SimplePolygon<T>
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
source§impl<T> SimplePolygon<T>where
T: PartialEq,
impl<T> SimplePolygon<T>where
T: PartialEq,
sourcepub 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.
source§impl<T> SimplePolygon<T>where
T: Copy,
impl<T> SimplePolygon<T>where
T: Copy,
sourcepub 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.
source§impl<T> SimplePolygon<T>where
T: Copy,
impl<T> SimplePolygon<T>where
T: Copy,
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> SimplePolygon<T>where
T: CoordinateType,
impl<T> SimplePolygon<T>where
T: CoordinateType,
sourcepub fn normalize_orientation<Area>(&mut self)
pub fn normalize_orientation<Area>(&mut self)
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) -> SimplePolygon<T>
pub fn normalized_orientation<Area>(self) -> SimplePolygon<T>
Call normalize_orientation()
while taking ownership and returning the result.
sourcepub fn orientation<Area>(&self) -> Orientation
pub fn orientation<Area>(&self) -> Orientation
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));
Trait Implementations§
source§impl<T> Clone for SimplePolygon<T>where
T: Clone,
impl<T> Clone for SimplePolygon<T>where
T: Clone,
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 for SimplePolygon<T>where
T: Debug,
impl<T> Debug for SimplePolygon<T>where
T: Debug,
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<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>,
source§impl<A, T> DoubledOrientedArea<A> for SimplePolygon<T>
impl<A, T> DoubledOrientedArea<A> for SimplePolygon<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>) -> Polygon<T>
fn from(simple_polygon: &SimplePolygon<T>) -> Polygon<T>
source§impl<I, T, P> From<I> for SimplePolygon<T>
impl<I, T, P> From<I> for SimplePolygon<T>
Create a polygon from a type that is convertible into an iterator of values convertible to Point
s.
source§fn from(iter: I) -> SimplePolygon<T>
fn from(iter: I) -> SimplePolygon<T>
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>) -> Polygon<T>
fn from(simple_polygon: SimplePolygon<T>) -> Polygon<T>
source§impl<T, P> FromIterator<P> for SimplePolygon<T>
impl<T, P> FromIterator<P> for SimplePolygon<T>
Create a polygon from a iterator of values convertible to Point
s.
source§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>,
source§impl<T> Hash for SimplePolygon<T>where
T: Hash,
impl<T> Hash for SimplePolygon<T>where
T: Hash,
source§impl<'a, T> IntoIterator for &'a SimplePolygon<T>
impl<'a, T> IntoIterator for &'a 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 for SimplePolygon<T>where
T: PartialEq,
impl<T> PartialEq for SimplePolygon<T>where
T: PartialEq,
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§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,
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, Dst> TryCastCoord<T, Dst> for SimplePolygon<T>
impl<T, Dst> TryCastCoord<T, Dst> for SimplePolygon<T>
§type Output = SimplePolygon<Dst>
type Output = SimplePolygon<Dst>
source§fn try_cast(&self) -> Option<<SimplePolygon<T> as TryCastCoord<T, Dst>>::Output>
fn try_cast(&self) -> Option<<SimplePolygon<T> as TryCastCoord<T, Dst>>::Output>
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.
impl<T> Eq for SimplePolygon<T>where
T: Eq,
impl<T> StructuralPartialEq for SimplePolygon<T>
Auto Trait Implementations§
impl<T> Freeze for SimplePolygon<T>
impl<T> RefUnwindSafe for SimplePolygon<T>where
T: RefUnwindSafe,
impl<T> Send for SimplePolygon<T>where
T: Send,
impl<T> Sync for SimplePolygon<T>where
T: Sync,
impl<T> Unpin for SimplePolygon<T>where
T: Unpin,
impl<T> UnwindSafe for SimplePolygon<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more