Struct libreda_db::prelude::SimpleRPolygon
pub struct SimpleRPolygon<T> {
half_points: Vec<T, Global>,
normalized: bool,
}
Expand description
A SimpleRPolygon
is a rectilinear polygon. It does not contain holes but can be self-intersecting.
The vertices are stored in an implicit format (one coordinate of two neighbour vertices is always the same
for rectilinear polygons). This reduces memory usage but has the drawback that edges must
alternate between horizontal and vertical. Vertices between two edges of the same orientation will
be dropped.
Fields§
§half_points: Vec<T, Global>
§normalized: bool
Implementations§
§impl<T> SimpleRPolygon<T>
impl<T> SimpleRPolygon<T>
pub fn empty() -> SimpleRPolygon<T>
pub fn empty() -> SimpleRPolygon<T>
Create empty polygon without any vertices.
pub fn num_points(&self) -> usize
👎Deprecated: use len() instead
pub fn num_points(&self) -> usize
Get the number of vertices.
pub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverse the order of the vertices in-place.
§impl<T> SimpleRPolygon<T>
impl<T> SimpleRPolygon<T>
pub fn reversed(self) -> SimpleRPolygon<T>
pub fn reversed(self) -> SimpleRPolygon<T>
Reverse the order of vertices.
§impl<T> SimpleRPolygon<T>where
T: Copy,
impl<T> SimpleRPolygon<T>where T: Copy,
pub fn edges(&self) -> impl Iterator<Item = REdge<T>>
pub fn edges(&self) -> impl Iterator<Item = REdge<T>>
Get all exterior edges of the polygon.
Examples
use iron_shapes::simple_rpolygon::SimpleRPolygon;
use iron_shapes::redge::REdge;
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly = SimpleRPolygon::try_new(&coords).unwrap();
let edges: Vec<_> = poly.edges().collect();
assert_eq!(edges, vec![
REdge::new((0, 0), (1, 0)),
REdge::new((1, 0), (1, 1)),
REdge::new((1, 1), (0, 1)),
REdge::new((0, 1), (0, 0)),
]);
§impl<T> SimpleRPolygon<T>where
T: Copy + PartialEq<T>,
impl<T> SimpleRPolygon<T>where T: Copy + PartialEq<T>,
pub fn try_new<P>(points: &Vec<P, Global>) -> Option<SimpleRPolygon<T>>where
P: Copy + Into<Point<T>>,
pub fn try_new<P>(points: &Vec<P, Global>) -> Option<SimpleRPolygon<T>>where P: Copy + Into<Point<T>>,
Create new rectilinear polygon from points.
Returns None
if the polygon defined by the points is not rectilinear.
use iron_shapes::simple_rpolygon::SimpleRPolygon;
let poly1 = SimpleRPolygon::try_new(&vec![(0, 0), (1, 0), (1, 1), (0, 1)]);
assert!(poly1.is_some());
// A triangle cannot be rectilinear.
let poly1 = SimpleRPolygon::try_new(&vec![(0, 0), (1, 0), (1, 1)]);
assert!(poly1.is_none());
§impl<T> SimpleRPolygon<T>where
T: CoordinateType,
impl<T> SimpleRPolygon<T>where T: CoordinateType,
pub fn transformed(&self, tf: &SimpleTransform<T>) -> SimpleRPolygon<T>
pub fn transformed(&self, tf: &SimpleTransform<T>) -> SimpleRPolygon<T>
Apply the transformation to this rectilinear polygon.
pub fn to_simple_polygon(&self) -> SimplePolygon<T>
pub fn to_simple_polygon(&self) -> SimplePolygon<T>
Convert to a SimplePolygon
.
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 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_rpolygon::SimpleRPolygon;
use iron_shapes::point::Point;
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly = SimpleRPolygon::try_new(&coords).unwrap();
assert_eq!(poly.lower_left_vertex(), Point::new(0, 0));
pub fn orientation(&self) -> Orientation
pub fn orientation(&self) -> Orientation
Get the orientation of the polygon, i.e. check if it is wound clock-wise or counter-clock-wise.
Examples
use iron_shapes::simple_rpolygon::SimpleRPolygon;
use iron_shapes::point::Point;
use iron_shapes::types::Orientation;
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly = SimpleRPolygon::try_new(&coords).unwrap();
assert_eq!(poly.orientation(), Orientation::CounterClockWise);
§impl<T> SimpleRPolygon<T>where
T: Copy + PartialOrd<T>,
impl<T> SimpleRPolygon<T>where T: Copy + PartialOrd<T>,
§impl<T> SimpleRPolygon<T>where
T: PartialOrd<T>,
impl<T> SimpleRPolygon<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) -> SimpleRPolygon<T>
pub fn normalized(self) -> SimpleRPolygon<T>
Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.
§impl<T> SimpleRPolygon<T>where
T: PartialEq<T>,
impl<T> SimpleRPolygon<T>where T: PartialEq<T>,
pub fn normalized_eq(&self, other: &SimpleRPolygon<T>) -> bool
pub fn normalized_eq(&self, other: &SimpleRPolygon<T>) -> bool
Equality test for simple polygons.
Two polygons are equal iff a cyclic shift on their vertices can be applied such that the both lists of vertices match exactly.
Trait Implementations§
§impl<T> Clone for SimpleRPolygon<T>where
T: Clone,
impl<T> Clone for SimpleRPolygon<T>where T: Clone,
§fn clone(&self) -> SimpleRPolygon<T>
fn clone(&self) -> SimpleRPolygon<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 SimpleRPolygon<T>where
T: Debug,
impl<T> Debug for SimpleRPolygon<T>where T: Debug,
§impl<'de, T> Deserialize<'de> for SimpleRPolygon<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for SimpleRPolygon<T>where T: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D
) -> Result<SimpleRPolygon<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>( __deserializer: __D ) -> Result<SimpleRPolygon<T>, <__D as Deserializer<'de>>::Error>where __D: Deserializer<'de>,
§impl<T> DoubledOrientedArea<T> for SimpleRPolygon<T>where
T: CoordinateType,
impl<T> DoubledOrientedArea<T> for SimpleRPolygon<T>where T: CoordinateType,
§fn area_doubled_oriented(&self) -> T
fn area_doubled_oriented(&self) -> T
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_rpolygon::SimpleRPolygon;
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly = SimpleRPolygon::try_new(&coords).unwrap();
assert_eq!(poly.area_doubled_oriented(), 2);
§impl<T> From<Rect<T>> for SimpleRPolygon<T>where
T: CoordinateType,
impl<T> From<Rect<T>> for SimpleRPolygon<T>where T: CoordinateType,
§fn from(r: Rect<T>) -> SimpleRPolygon<T>
fn from(r: Rect<T>) -> SimpleRPolygon<T>
§impl<T> From<SimpleRPolygon<T>> for Geometry<T>
impl<T> From<SimpleRPolygon<T>> for Geometry<T>
§fn from(x: SimpleRPolygon<T>) -> Geometry<T>
fn from(x: SimpleRPolygon<T>) -> Geometry<T>
§impl<T> Hash for SimpleRPolygon<T>where
T: Hash,
impl<T> Hash for SimpleRPolygon<T>where T: Hash,
§impl<T> PartialEq<SimpleRPolygon<T>> for SimpleRPolygon<T>where
T: PartialEq<T>,
impl<T> PartialEq<SimpleRPolygon<T>> for SimpleRPolygon<T>where T: PartialEq<T>,
§fn eq(&self, other: &SimpleRPolygon<T>) -> bool
fn eq(&self, other: &SimpleRPolygon<T>) -> bool
self
and other
values to be equal, and is used
by ==
.§impl<T> Serialize for SimpleRPolygon<T>where
T: Serialize,
impl<T> Serialize for SimpleRPolygon<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 SimpleRPolygon<T>where
T: Copy + PartialOrd<T>,
impl<T> TryBoundingBox<T> for SimpleRPolygon<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 SimpleRPolygon<T>where
T: CoordinateType + NumCast,
Dst: CoordinateType + NumCast,
impl<T, Dst> TryCastCoord<T, Dst> for SimpleRPolygon<T>where T: CoordinateType + NumCast, Dst: CoordinateType + NumCast,
§type Output = SimpleRPolygon<Dst>
type Output = SimpleRPolygon<Dst>
§fn try_cast(
&self
) -> Option<<SimpleRPolygon<T> as TryCastCoord<T, Dst>>::Output>
fn try_cast( &self ) -> Option<<SimpleRPolygon<T> as TryCastCoord<T, Dst>>::Output>
§impl<T> WindingNumber<T> for SimpleRPolygon<T>where
T: CoordinateType,
impl<T> WindingNumber<T> for SimpleRPolygon<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