rgeometry::data::polygon

Struct PolygonConvex

source
pub struct PolygonConvex<T>(/* private fields */);

Implementations§

source§

impl<T> PolygonConvex<T>
where T: PolygonScalar,

source

pub fn new_unchecked(poly: Polygon<T>) -> PolygonConvex<T>

Assume that a polygon is convex.

§Safety

The input polygon has to be strictly convex, ie. no vertices are allowed to be concave or colinear.

§Time complexity

$O(1)$

source

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

Locate a point relative to a convex polygon.

§Time complexity

$O(\log n)$

§Examples
source

pub fn validate(&self) -> Result<(), Error>

Validates the following properties:

  • Each vertex is convex, ie. not concave or colinear.
  • All generate polygon properties hold true (eg. no duplicate points, no self-intersections).
§Time complexity

$O(n \log n)$

source

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

source

pub fn polygon(&self) -> &Polygon<T>

$O(1)$

source

pub fn random<R>(n: usize, rng: &mut R) -> PolygonConvex<T>

Uniformly sample a random convex polygon.

The output polygon is rooted in (0,0), grows upwards, and has a height and width of T::max_value().

§Time complexity

$O(n \log n)$

source§

impl PolygonConvex<OrderedFloat<f64>>

Methods from Deref<Target = Polygon<T>>§

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 iter(&self) -> Iter<'_, T>

source

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

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

source

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

source

pub fn normalize(&self) -> Polygon<OrderedFloat<f64>>

Trait Implementations§

source§

impl<T: Clone> Clone for PolygonConvex<T>

source§

fn clone(&self) -> PolygonConvex<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 PolygonConvex<T>

source§

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

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

impl<T> Deref for PolygonConvex<T>

source§

type Target = Polygon<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Distribution<PolygonConvex<isize>> for Standard

source§

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PolygonConvex<isize>

Generate a random value of T, using rng as the source of randomness.
source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>
where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>
where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F 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 PolygonConvex<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

Auto Trait Implementations§

§

impl<T> Freeze for PolygonConvex<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for PolygonConvex<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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V