Struct Polygon

Source
pub struct Polygon<T> {
    pub exterior: SimplePolygon<T>,
    pub interiors: Vec<SimplePolygon<T>>,
}
Expand description

A polygon possibly with holes. The polygon is defined by a hull and a list of holes which are both SimplePolygons.

Fields§

§exterior: SimplePolygon<T>

The outer hull of the polygon.

§interiors: Vec<SimplePolygon<T>>

A list of holes in the polygon.

Implementations§

Source§

impl<T> Polygon<T>

Source

pub fn new_raw(exterior: Vec<Point<T>>) -> Polygon<T>

Create a new polygon from a sequence of points. Ordering of points is not normalized. This impacts the equality check.

Source

pub fn new_raw_with_holes<E, I>(exterior: E, holes: Vec<I>) -> Polygon<T>
where E: Into<SimplePolygon<T>>, I: Into<SimplePolygon<T>>,

Create a new polygon from a hull and a list of holes. Ordering of points is not normalized. This impacts the equality check.

Source

pub fn empty() -> Polygon<T>

Create empty polygon without any vertices.

Source

pub fn len(&self) -> usize

Get the number of vertices.

Source

pub fn is_empty(&self) -> bool

Check if polygon has no vertices.

Source§

impl<T> Polygon<T>
where T: Copy,

Source

pub fn edges(&self) -> Vec<Edge<T>>

Get all exterior edges of the polygon.

Source

pub fn all_edges_iter(&self) -> impl Iterator<Item = Edge<T>>

Iterate over all edges of the polygon, including interior edges.

Source§

impl<T> Polygon<T>
where T: PartialOrd,

Source

pub fn new<I>(i: I) -> Polygon<T>
where I: Into<Polygon<T>>,

Create a new polygon from a sequence of points.

Source

pub fn new_with_holes<E, I>(exterior: E, holes: Vec<I>) -> Polygon<T>
where E: Into<SimplePolygon<T>>, I: Into<SimplePolygon<T>>,

Create a new polygon from a hull and a list of holes.

Source

pub fn normalize(&mut self)

Reorder vertices and holes to get the lexicographically smallest representation of this polygon. Does not change the orientations.

Source

pub fn normalized(self) -> Polygon<T>

Reorder vertices and holes to get the lexicographically smallest representation of this polygon. Does not change the orientations.

Source§

impl<T> Polygon<T>
where T: CoordinateType,

Source

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

Source

pub fn lower_left_vertex(&self) -> Point<T>

Get the vertex with lowest x-coordinate of the exterior polygon. Prefer lower y-coordinates to break ties.

§Examples
use iron_shapes::polygon::Polygon;
use iron_shapes::point::Point;
let coords = vec![(0, 0), (1, 0), (-1, 2), (-1, 1)];

let poly = Polygon::new(coords);

assert_eq!(poly.lower_left_vertex(), Point::new(-1, 1));
Source

pub fn orientation<Area>(&self) -> Orientation
where Area: Num + From<T> + PartialOrd,

Get the orientation of the exterior polygon.

§Examples
use iron_shapes::polygon::Polygon;
use iron_shapes::point::Point;
use iron_shapes::types::Orientation;
let coords = vec![(0, 0), (3, 0), (3, 1)];

let poly = Polygon::new(coords);

assert_eq!(poly.orientation::<i64>(), Orientation::CounterClockWise);

Trait Implementations§

Source§

impl BooleanOp<Ratio<i32>> for Polygon<Ratio<i32>>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<Ratio<i32>>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<Ratio<i32>>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl BooleanOp<Ratio<i64>> for Polygon<Ratio<i64>>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<Ratio<i64>>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<Ratio<i64>>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl BooleanOp<f32> for Polygon<f32>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<f32>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<f32>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl BooleanOp<f64> for Polygon<f64>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<f64>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<f64>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl BooleanOp<i32> for Polygon<i32>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<i32>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<i32>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl BooleanOp<i64> for Polygon<i64>

Source§

fn boolean_op( &self, operation: Operation, other: &Polygon<i64>, polygon_semantics: PolygonSemantics, ) -> MultiPolygon<i64>

Compute the boolean operation of self and other. Read more
Source§

fn intersection(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean intersection self & other. Read more
Source§

fn difference(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean difference self - other. Read more
Source§

fn union(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean union self | other. Read more
Source§

fn xor(&self, other: &Self) -> MultiPolygon<T>

Compute the boolean exclusive OR self ^ other. Read more
Source§

impl<T> Clone for Polygon<T>
where T: Clone,

Source§

fn clone(&self) -> Polygon<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Polygon<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for Polygon<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Polygon<T>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T, A> DoubledOrientedArea<A> for Polygon<T>
where T: CoordinateType, A: Num + From<T>,

Source§

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::polygon::{Polygon, DoubledOrientedArea};
let coords = vec![(0, 0), (3, 0), (3, 1)];

let poly = Polygon::new(coords);

let area: i64 = poly.area_doubled_oriented();
assert_eq!(area, 3);
Source§

impl<T> From<&Rect<T>> for Polygon<T>
where T: Copy,

Create a polygon from a rectangle.

Source§

fn from(rect: &Rect<T>) -> Polygon<T>

Converts to this type from the input type.
Source§

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>

Converts to this type from the input type.
Source§

impl<'a, T, P> From<&'a Vec<P>> for Polygon<T>
where T: CoordinateType, Point<T>: From<&'a P>,

Create a polygon from a Vec of values convertible to Points.

Source§

fn from(vec: &'a Vec<P>) -> Polygon<T>

Converts to this type from the input type.
Source§

impl<T> From<Geometry<T>> for Polygon<T>

Source§

fn from(g: Geometry<T>) -> Polygon<T>

Convert a geometry into a polygon.

Source§

impl<T> From<Polygon<T>> for Geometry<T>

Source§

fn from(x: Polygon<T>) -> Geometry<T>

Converts to this type from the input type.
Source§

impl<T> From<Rect<T>> for Polygon<T>
where T: Copy,

Create a polygon from a rectangle.

Source§

fn from(rect: Rect<T>) -> Polygon<T>

Converts to this type from the input type.
Source§

impl<T> From<SimplePolygon<T>> for Polygon<T>

Create a polygon from a simple polygon.

Source§

fn from(simple_polygon: SimplePolygon<T>) -> Polygon<T>

Converts to this type from the input type.
Source§

impl<T, P> From<Vec<P>> for Polygon<T>
where T: Copy + PartialOrd, Point<T>: From<P>,

Create a polygon from a Vec of values convertible to Points.

Source§

fn from(vec: Vec<P>) -> Polygon<T>

Converts to this type from the input type.
Source§

impl<T, P> FromIterator<P> for Polygon<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) -> Polygon<T>
where I: IntoIterator<Item = P>,

Creates a value from an iterator. Read more
Source§

impl<T> Hash for Polygon<T>
where T: Hash,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> MapPointwise<T> for Polygon<T>
where T: CoordinateType,

Source§

fn transform<F>(&self, tf: F) -> Polygon<T>
where F: Fn(Point<T>) -> Point<T>,

Point wise transformation.
Source§

impl<T> PartialEq for Polygon<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Polygon<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Polygon<T>
where T: Serialize,

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> TryBoundingBox<T> for Polygon<T>
where T: Copy + PartialOrd,

Source§

fn try_bounding_box(&self) -> Option<Rect<T>>

Return the bounding box of this geometry if a bounding box is defined.
Source§

impl<T, Dst> TryCastCoord<T, Dst> for Polygon<T>

Source§

type Output = Polygon<Dst>

Output type of the cast. This is likely the same geometrical type just with other coordinate types.
Source§

fn try_cast(&self) -> Option<<Polygon<T> as TryCastCoord<T, Dst>>::Output>

Try to cast to target data type. Read more
Source§

fn cast(&self) -> Self::Output

Cast to target data type. Read more
Source§

impl<T> WindingNumber<T> for Polygon<T>
where T: CoordinateType,

Source§

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.

See: http://geomalgorithms.com/a03-_inclusion.html

Source§

fn contains_point_non_oriented(&self, point: Point<T>) -> bool

Check if point is inside the polygon, i.e. the polygons winds around the point a non-zero number of times. Read more
Source§

fn contains_point(&self, point: Point<T>) -> bool

Check if point is inside the polygon, i.e. the polygon winds around the point an odd number of times. Read more
Source§

impl<T> Eq for Polygon<T>
where T: Eq,

Source§

impl<T> StructuralPartialEq for Polygon<T>

Auto Trait Implementations§

§

impl<T> Freeze for Polygon<T>

§

impl<T> RefUnwindSafe for Polygon<T>
where T: RefUnwindSafe,

§

impl<T> Send for Polygon<T>
where T: Send,

§

impl<T> Sync for Polygon<T>
where T: Sync,

§

impl<T> Unpin for Polygon<T>
where T: Unpin,

§

impl<T> UnwindSafe for Polygon<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<S, T> Mirror<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPointwise<T>,

Source§

fn mirror_x(&self) -> S

Return the geometrical object mirrored at the x axis.

Source§

fn mirror_y(&self) -> S

Return the geometrical object mirrored at the y axis.

Source§

impl<S, T> RotateOrtho<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPointwise<T>,

Source§

fn rotate_ortho(&self, a: Angle) -> S

Rotate the geometrical shape by a multiple of 90 degrees.
Source§

impl<S, T> Scale<T> for S
where T: Copy + Mul<Output = T>, S: MapPointwise<T>,

Source§

fn scale(&self, factor: T) -> S

Scale the geometrical shape. Scaling center is the origin (0, 0).
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<S, T> Translate<T> for S
where T: Copy + Add<Output = T>, S: MapPointwise<T>,

Source§

fn translate(&self, v: Vector<T>) -> S

Translate the geometrical object by a vector v.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> IdType for T
where T: Debug + Clone + Eq + Hash + 'static,

Source§

impl<T> IdTypeMT for T
where T: IdType + Sync + Send,

Source§

impl<T> TextType for T
where T: Eq + Hash + Clone + Debug,