rgeometry::data

Struct Polygon

source
pub struct Polygon<T> { /* private fields */ }

Implementations§

source§

impl<T> Polygon<T>

source

pub fn new_unchecked(vertices: Vec<Point<T, 2>>) -> Polygon<T>
where T: PolygonScalar,

$O(1)$

source

pub fn new(points: Vec<Point<T, 2>>) -> Result<Polygon<T>, Error>
where T: PolygonScalar,

$O(n^2)$

source

pub fn validate(&self) -> Result<(), Error>
where T: PolygonScalar,

source

pub fn validate_weakly(&self) -> Result<(), Error>
where T: PolygonScalar,

source

pub fn locate(&self, origin: &Point<T, 2>) -> PointLocation
where T: PolygonScalar,

source

pub fn triangulate( &self, ) -> impl Iterator<Item = (Cursor<'_, T>, Cursor<'_, T>, Cursor<'_, T>)> + '_
where T: PolygonScalar,

source

pub fn centroid(&self) -> Point<T>
where T: PolygonScalar,

source

pub fn bounding_box(&self) -> (Point<T>, Point<T>)
where T: PolygonScalar,

source

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);
// 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)));
source

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);
// 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);
source

pub fn orientation(&self) -> Orientation
where T: PolygonScalar,

source

pub fn point(&self, idx: PointId) -> &Point<T>

Access point of a given vertex.

§Time complexity

$O(1)$

source

pub fn cursor(&self, idx: PointId) -> Cursor<'_, T>

Access cursor of a given vertex.

§Time complexity

$O(1)$

source

pub fn boundary_slice(&self) -> &[PointId]

source

pub fn equals(&self, other: &Self) -> bool
where T: PolygonScalar,

source

pub fn iter_boundary(&self) -> CursorIter<'_, T>

source

pub fn iter_boundary_edges(&self) -> EdgeIter<'_, T>

source

pub fn map_points<F>(self, f: F) -> Polygon<T>
where T: Clone, F: Fn(Point<T>) -> Point<T>,

source

pub fn iter(&self) -> Iter<'_, T>

source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

source

pub fn map<U, F>(self, f: F) -> Polygon<U>
where T: Clone, U: Clone, F: Fn(T) -> U + Clone,

source

pub fn cast<U>(self) -> Polygon<U>
where T: Clone + Into<U>,

source

pub fn float(self) -> Polygon<OrderedFloat<f64>>
where T: Clone + Into<f64>,

source

pub fn vertices_reverse(&mut self, pt1: Position, pt2: Position)

source

pub fn vertices_join(&mut self, pt1: Position, pt2: Position)

source

pub fn direct(&self, edge: IndexEdge) -> DirectedIndexEdge

Panics if the edge isn’t part of the polygon.

source

pub fn ensure_ccw(&mut self) -> Result<(), Error>
where T: PolygonScalar,

source

pub fn is_monotone(&self, direction: &Vector<T, 2>) -> bool
where T: PolygonScalar,

source§

impl Polygon<OrderedFloat<f64>>

Trait Implementations§

source§

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

source§

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

Returns a copy 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> Debug for Polygon<T>

source§

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

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

impl<'a, T> From<&'a PolygonConvex<T>> for &'a Polygon<T>

source§

fn from(convex: &'a PolygonConvex<T>) -> &'a Polygon<T>

Converts to this type from the input type.
source§

impl<T> From<PolygonConvex<T>> for Polygon<T>

source§

fn from(convex: PolygonConvex<T>) -> Polygon<T>

Converts to this type from the input type.
source§

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

source§

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

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> Mul<&Polygon<T>> for &Transform<T, 2>
where T: TransformScalar,

source§

type Output = Polygon<T>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl<T> Mul<&Polygon<T>> for Transform<T, 2>
where T: TransformScalar,

source§

type Output = Polygon<T>

The resulting type after applying the * operator.
source§

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

Performs the * operation. Read more
source§

impl<T> Mul<Polygon<T>> for &Transform<T, 2>
where T: TransformScalar,

source§

type Output = Polygon<T>

The resulting type after applying the * operator.
source§

fn mul(self, other: Polygon<T>) -> Polygon<T>

Performs the * operation. Read more
source§

impl<T> Mul<Polygon<T>> for Transform<T, 2>
where T: TransformScalar,

source§

type Output = Polygon<T>

The resulting type after applying the * operator.
source§

fn mul(self, other: Polygon<T>) -> Polygon<T>

Performs the * operation. Read more

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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<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.