Polygon

Struct Polygon 

Source
pub struct Polygon<T>(/* private fields */);
Expand description

A convex polygon.

The N represents how many points the polygon can have before it needs to allocate onto the heap. If you know your shapes will never have more than N edges, you can set it to that value and avoid heap allocations.

Implementations§

Source§

impl<T: Num> Polygon<T>

Source

pub fn new() -> Self

Creates a new empty polygon.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new empty polygon with at least the specified capacity.

Source

pub fn from_arr<const LEN: usize>(arr: [Vec2<T>; LEN]) -> Self

Create a new polygon using the array of points.

Source

pub fn from_slice<const LEN: usize>(slice: &[Vec2<T>]) -> Self

Create a new polygon using the slice of points.

Source

pub fn from_vec(vec: Vec<Vec2<T>>) -> Self

Create a new polygon using the array of points.

Source

pub fn from_rect(rect: Rect<T>) -> Self

Source

pub fn from_tri(tri: Triangle<T>) -> Self

Create a polygon representation of the provided triangle.

Source

pub fn from_quad(quad: Quad<T>) -> Self

Create a polygon representation of the provided quad.

Source

pub fn len(&self) -> usize

How many points the polygon has.

Source

pub fn is_empty(&self) -> bool

If the polygon has no points.

Source

pub fn capacity(&self) -> usize

The polygon’s allocated capacity.

Source

pub fn push(&mut self, p: Vec2<T>)

Add a point to the polygon.

Source

pub fn pop(&mut self) -> Option<Vec2<T>>

Remove the last point from the polygon and return it.

Source

pub fn reserve(&mut self, capacity: usize)

Reserve enough space for at least capacity points.

Source

pub fn points(&self) -> &[Vec2<T>]

Reference to the polygon’s points.

Source

pub fn points_mut(&mut self) -> &mut [Vec2<T>]

Mutable reference to the polygon’s points.

Source

pub fn clear(&mut self)

Remove all points from the polygon.

Source

pub fn remove(&mut self, index: usize) -> Vec2<T>

Remove the point at index.

Source

pub fn insert(&mut self, index: usize, p: Vec2<T>)

Insert a point at index.

Source

pub fn to_vec(self) -> Vec<Vec2<T>>

Convert the polygon into a Vec of points.

Source

pub fn resize_with<F: FnMut() -> Vec2<T>>(&mut self, new_size: usize, f: F)

Resize the polygon, using the provided function to create new points.

Source

pub fn translate(&mut self, amount: Vec2<T>)

Translate all points in the polygon.

Source

pub fn edge(&self, index: usize) -> Option<Line<T>>

Get the nth edge of the polygon. A polygon has the same amount of edges as vertices, so if this index exceeds len(), then None will be returned.

Source

pub fn edges(&self) -> impl Iterator<Item = Line<T>> + '_

An iterator over all the polygon’s edges.

Source§

impl<T: Float> Polygon<T>

Source

pub fn transform_in_place_by(&mut self, f: impl FnMut(Vec2<T>) -> Vec2<T>)

Source

pub fn transform_by_into( &self, into: &mut Self, f: impl FnMut(Vec2<T>) -> Vec2<T>, )

Source

pub fn transform_by(&self, f: impl FnMut(Vec2<T>) -> Vec2<T>) -> Self

Trait Implementations§

Source§

impl<T> AsMut<[Vec2<T>]> for Polygon<T>

Source§

fn as_mut(&mut self) -> &mut [Vec2<T>]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<[Vec2<T>]> for Polygon<T>

Source§

fn as_ref(&self) -> &[Vec2<T>]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

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

Source§

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

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<Self, __D::Error>
where __D: Deserializer<'de>,

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

impl<T: Num> Extend<Vec2<T>> for Polygon<T>

Source§

fn extend<I: IntoIterator<Item = Vec2<T>>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Num, const S: usize> From<[Vec2<T>; S]> for Polygon<T>

Source§

fn from(value: [Vec2<T>; S]) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Polygon<T>> for DynShape<T>

Source§

fn from(value: Polygon<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Num> From<Quad<T>> for Polygon<T>

Source§

fn from(value: Quad<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Num> From<Rect<T>> for Polygon<T>

Source§

fn from(value: Rect<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Num> From<Triangle<T>> for Polygon<T>

Source§

fn from(value: Triangle<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Num> From<Vec<Vec2<T>>> for Polygon<T>

Source§

fn from(value: Vec<Vec2<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T: Num> FromIterator<Vec2<T>> for Polygon<T>

Source§

fn from_iter<I: IntoIterator<Item = Vec2<T>>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> IntoIterator for Polygon<T>

Source§

type Item = Vec2<T>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Vec2<T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Num> Numeric for Polygon<T>

Source§

type AsU8 = Polygon<u8>

Source§

type AsU16 = Polygon<u16>

Source§

type AsU32 = Polygon<u32>

Source§

type AsU64 = Polygon<u64>

Source§

type AsU128 = Polygon<u128>

Source§

type AsUSize = Polygon<usize>

Source§

type AsI8 = Polygon<i8>

Source§

type AsI16 = Polygon<i16>

Source§

type AsI32 = Polygon<i32>

Source§

type AsI64 = Polygon<i64>

Source§

type AsI128 = Polygon<i128>

Source§

type AsISize = Polygon<isize>

Source§

type AsF32 = Polygon<f32>

Source§

type AsF64 = Polygon<f64>

Source§

fn to_u8(self) -> Self::AsU8

Source§

fn to_u16(self) -> Self::AsU16

Source§

fn to_u32(self) -> Self::AsU32

Source§

fn to_u64(self) -> Self::AsU64

Source§

fn to_u128(self) -> Self::AsU128

Source§

fn to_usize(self) -> Self::AsUSize

Source§

fn to_i8(self) -> Self::AsI8

Source§

fn to_i16(self) -> Self::AsI16

Source§

fn to_i32(self) -> Self::AsI32

Source§

fn to_i64(self) -> Self::AsI64

Source§

fn to_i128(self) -> Self::AsI128

Source§

fn to_isize(self) -> Self::AsISize

Source§

fn to_f32(self) -> Self::AsF32

Source§

fn to_f64(self) -> Self::AsF64

Source§

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

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::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
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, S> Polygonal<T> for S
where T: Float, S: AsRef<[Vec2<T>]>,

Source§

fn nearest_vertex(&self, source: Vec2<T>) -> Vec2<T>

Get the nearest point on this polygon to the source point.
Source§

fn all_edges<F>(&self, cond: F) -> bool
where F: FnMut(Line<T>) -> bool,

Iterate all polygonal edges of the shape, returning true if any of them satisfy the conditional function provided.
Source§

fn all_normals<F>(&self, cond: F) -> bool
where F: FnMut(Vec2<T>) -> bool,

Iterate all edge normals of the shape, returning true if any of them satisfy the conditional function provided.
Source§

fn visit_normals<F>(&self, plot: F)
where F: FnMut(Vec2<T>),

Walk through every normal of the polygon.
Source§

fn visit_edges<F: FnMut(Line<T>)>(&self, plot: F)

Walk through every edge of the polygon.
Source§

impl<T, S> Shape<T> for S
where T: Float, S: AsRef<[Vec2<T>]>,

Source§

fn centroid(&self) -> Vec2<T>

Centroid of the shape.
Source§

fn contains(&self, p: Vec2<T>) -> bool

If the point is contained within the shape.
Source§

fn bounds(&self) -> Rect<T>

Rectangular bounds of the shape.
Source§

fn project_onto_axis(&self, axis: Vec2<T>) -> Projection<T>

Project the shape onto the axis.
Source§

fn project_point(&self, p: Vec2<T>) -> Vec2<T>

Project a point onto the outside surface of the shape.
Source§

fn rayhit(&self, ray: &Ray<T>) -> bool

Check if a ray intersects this shape.
Source§

fn raycast(&self, ray: &Ray<T>) -> Option<RayHit<T>>

Raycast against the shape.
Source§

fn overlaps_rect(&self, rect: &Rect<T>) -> bool

If this shape overlaps the rectangle.
Source§

fn overlaps_circ(&self, circ: &Circle<T>) -> bool

If this shape overlaps the circle.
Source§

fn overlaps_poly<P>(&self, poly: &P) -> bool
where P: Polygonal<T>,

If this shape overlaps the polygon.
Source§

fn extract_from_circ(&self, circ: &Circle<T>) -> Option<Vec2<T>>

If this shape and the circle overlap, return a push-out vector that can be used to extract them from each other.
Source§

fn extract_from_poly<P>(&self, poly: &P) -> Option<Vec2<T>>
where P: Polygonal<T>,

If this shape and the polygon overlap, return a push-out vector that can be used to extract them from each other.
Source§

fn is_convex(&self) -> bool

If this shape is convex.
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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,