pub struct Polygon<T> { /* private fields */ }
Implementations
sourceimpl<T> Polygon<T>
impl<T> Polygon<T>
sourcepub fn new_unchecked(vertices: Vec<Point<T, 2>>) -> Polygon<T> where
T: PolygonScalar,
pub fn new_unchecked(vertices: Vec<Point<T, 2>>) -> Polygon<T> where
T: PolygonScalar,
$O(1)$
sourceimpl<T> Polygon<T>
impl<T> Polygon<T>
pub fn validate(&self) -> Result<(), Error> where
T: PolygonScalar,
pub fn validate_weakly(&self) -> Result<(), Error> where
T: PolygonScalar,
pub fn locate(&self, origin: &Point<T, 2>) -> PointLocation where
T: PolygonScalar,
pub fn triangulate(
&self
) -> impl Iterator<Item = (Cursor<'_, T>, Cursor<'_, T>, Cursor<'_, T>)> + '_ where
T: PolygonScalar,
pub fn centroid(&self) -> Point<T, 2> where
T: PolygonScalar,
pub fn bounding_box(&self) -> (Point<T, 2>, Point<T, 2>) where
T: PolygonScalar,
sourcepub fn signed_area<F>(&self) -> F where
T: PolygonScalar + Into<F>,
F: NumOps<F, F> + Sum + FromPrimitive,
pub fn signed_area<F>(&self) -> F where
T: PolygonScalar + Into<F>,
F: NumOps<F, F> + Sum + FromPrimitive,
Computes the area of a polygon. If the polygon winds counter-clockwise, the area will be a positive number. If the polygon winds clockwise, the area will be negative.
Return type
This function is polymorphic for the same reason as signed_area_2x
.
Time complexity
$O(n)$
Examples
// The area of this polygon is 1/2 and won't fit in an `i32`
let p = Polygon::new(vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([0, 1]),
])?;
assert_eq!(p.signed_area::<i32>(), 0);
Run// The area of this polygon is 1/2 and will fit in a `Ratio<i32>`.
let p = Polygon::new(vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([0, 1]),
])?;
assert_eq!(p.signed_area::<Ratio<i32>>(), Ratio::from((1,2)));
Runsourcepub fn signed_area_2x<F>(&self) -> F where
T: PolygonScalar + Into<F>,
F: NumOps<F, F> + Sum,
pub fn signed_area_2x<F>(&self) -> F where
T: PolygonScalar + Into<F>,
F: NumOps<F, F> + Sum,
Compute double the area of a polygon. If the polygon winds counter-clockwise, the area will be a positive number. If the polygon winds clockwise, the area will be negative.
Why compute double the area? If you are using integer coordinates then the doubled area will also be an integer value. Computing the exact requires a division which may leave you with a non-integer result.
Return type
Storing the area of a polygon may require more bits of precision than are used
for the coordinates. For example, if 32bit integers are used for point coordinates
then you might need 65 bits to store the area doubled. For this reason, the return
type is polymorphic and should be selected with care. If you are unsure which type
is appropriate, use BigInt
or BigRational
.
Time complexity
$O(n)$
Examples:
// The area of this polygon is 1/2 and cannot fit in an `i32` variable
// so lets compute twice the area.
let p = Polygon::new(vec![
Point::new([0, 0]),
Point::new([1, 0]),
Point::new([0, 1]),
])?;
assert_eq!(p.signed_area_2x::<i32>(), 1);
Run// The area of a polygon may require more bits of precision than are
// used for the coordinates.
let p = Polygon::new(vec![
Point::new([0, 0]),
Point::new([i32::MAX, 0]),
Point::new([0, i32::MAX]),
])?;
assert_eq!(p.signed_area_2x::<i64>(), 4611686014132420609_i64);
Runpub fn orientation(&self) -> Orientation where
T: PolygonScalar,
pub fn boundary_slice(&self) -> &[PointId]
pub fn iter_boundary(&self) -> CursorIter<'_, T>ⓘNotable traits for CursorIter<'a, T>impl<'a, T> Iterator for CursorIter<'a, T> type Item = Cursor<'a, T>;
pub fn iter_boundary_edges(&self) -> EdgeIter<'_, T>ⓘNotable traits for EdgeIter<'a, T>impl<'a, T: Clone> Iterator for EdgeIter<'a, T> type Item = DirectedEdge<'a, T, 2>;
pub fn map_points<F>(self, f: F) -> Polygon<T> where
T: Clone,
F: Fn(Point<T, 2>) -> Point<T, 2>,
pub fn iter(&self) -> Iter<'_, T>ⓘNotable traits for Iter<'a, T>impl<'a, T> Iterator for Iter<'a, T> type Item = &'a Point<T, 2>;
pub fn iter_mut(&mut self) -> IterMut<'_, T>ⓘNotable traits for IterMut<'a, T>impl<'a, T> Iterator for IterMut<'a, T> type Item = &'a mut Point<T, 2>;
pub fn map<U, F>(self, f: F) -> Polygon<U> where
T: Clone,
U: Clone,
F: Fn(T) -> U + Clone,
pub fn cast<U>(self) -> Polygon<U> where
T: Clone + Into<U>,
pub fn float(self) -> Polygon<OrderedFloat<f64>> where
T: Clone + Into<f64>,
pub fn vertices_reverse(&mut self, pt1: Position, pt2: Position)
pub fn vertices_join(&mut self, pt1: Position, pt2: Position)
sourcepub fn direct(&self, edge: IndexEdge) -> DirectedIndexEdge
pub fn direct(&self, edge: IndexEdge) -> DirectedIndexEdge
Panics if the edge isn’t part of the polygon.
pub fn ensure_ccw(&mut self) where
T: PolygonScalar,
sourceimpl Polygon<OrderedFloat<f64>>
impl Polygon<OrderedFloat<f64>>
pub fn normalize(&self) -> Polygon<OrderedFloat<f64>>
Trait Implementations
sourceimpl<'a, T> From<&'a PolygonConvex<T>> for &'a Polygon<T>
impl<'a, T> From<&'a PolygonConvex<T>> for &'a Polygon<T>
sourcefn from(convex: &'a PolygonConvex<T>) -> &'a Polygon<T>
fn from(convex: &'a PolygonConvex<T>) -> &'a Polygon<T>
Converts to this type from the input type.
sourceimpl<T> From<PolygonConvex<T>> for Polygon<T>
impl<T> From<PolygonConvex<T>> for Polygon<T>
sourcefn from(convex: PolygonConvex<T>) -> Polygon<T>
fn from(convex: PolygonConvex<T>) -> Polygon<T>
Converts to this type from the input type.
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more