Skip to main content

Polygon

Struct Polygon 

Source
pub struct Polygon {
    pub position: (f32, f32),
    pub vertices: Vec<(f32, f32)>,
}
Expand description

A polygon with a position and finitely many vertices given in either clockwise or counterclockwise orientation.

The polygon is represented as a star shape: to find the absolute position of the n-th vertex, add the position to the n-th element of vertices.

The position is NOT used as a vertex by default: if you want your position to be a vertex, then define a vertex with offset (0.0, 0.0).

The order of the vertices container determines the order in which the vertices are connected. Both clockwise and counterclockwise orientations are valid.

§Examples

use sepax2d::prelude::*;
 
let mut triangle = Polygon::new((3.0, 2.0));
triangle.add((-1.0, -1.0));
triangle.add((0.0, 1.0));
triangle.add((1.0, -1.0)); 
//Triangle with vertices (2, 1), (3, 3), and (4, 1)
 
assert!(triangle.is_convex());
 
let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)];
let rectangle = Polygon::from_vertices((0.0, 0.0), vertices); 
//Rectangle with vertices (0,0), (2,0), (2,1), and (0, 1)
 
assert!(rectangle.is_convex());
 
let concave_vertices = vec![(0.0, 0.0), (2.0, 0.0), (0.0, 1.0), (2.0, 1.0)];
let concave_shape = Polygon::from_vertices((0.0, 0.0), concave_vertices); 
//Non-convex hour-glass shape with the same vertices
 
assert!(!concave_shape.is_convex());

Fields§

§position: (f32, f32)§vertices: Vec<(f32, f32)>

Implementations§

Source§

impl Polygon

Source

pub fn new(position: (f32, f32)) -> Polygon

Define a new polygon with no vertices at the given position.

§Examples
use sepax2d::prelude::*;
 
let point = Polygon::new((17.0, 5.0));
Source

pub fn is_convex(&self) -> bool

Returns true when the polygon is convex, i.e. when none of its points lie on opposite sides of one of its perimeter segements, and false when it is concave.

Returns true automatically when the shape has two or fewer vertices, as points and line segments are trivially convex.

This method performs a floating point comparison with Rust’s built in epsilon constant, so it may return the incorrect answer for polygons which are almost complex or almost concave.

§Examples
use sepax2d::prelude::*;
 
let vertices = vec![(-1.0, 1.0), (1.0, 1.0), (1.0, -1.0), (-1.0, -1.0)];
let square = Polygon::from_vertices((0.0, 0.0), vertices);
 
assert!(square.is_convex());
 
let concave_vertices = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 2.0), (2.0, 1.0), (0.0, 2.0)];
let concave_shape = Polygon::from_vertices((0.0, 0.0), concave_vertices);
 
assert!(!concave_shape.is_convex());
Source

pub fn add(&mut self, vertex: (f32, f32))

Adds a vertex to the given shape, setting it as the last vertex.

§Examples
use sepax2d::prelude::*;
 
let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 2.0)];
let mut polygon = Polygon::from_vertices((0.0, 0.0), vertices); //Triangle with vertices (0, 0), (2, 0), and (2, 2)
 
polygon.add((0.0, 2.0)) //Square with vertices (0, 0), (2, 0), (2, 2), and (0, 2)
Source

pub fn from_vertices(position: (f32, f32), vertices: Vec<(f32, f32)>) -> Polygon

Creates a polygon from the given vertices.

§Examples
use sepax2d::prelude::*;
 
let vertices = vec![(0.0, 0.0), (2.0, 0.0), (2.0, 1.0), (0.0, 1.0)];
let rectangle = Polygon::from_vertices((0.0, 0.0), vertices); 
//Rectangle with vertices (0,0), (2,0), (2,1), and (0, 1)
Source

pub fn convex_from_vertices( position: (f32, f32), vertices: Vec<(f32, f32)>, ) -> Option<Polygon>

Attempts to create a convex polygon from the given vertices. It returns the poloygon if it is convex, and None if it is concave.

§Examples
use sepax2d::prelude::*;
 
let vertices = vec![(-1.0, 1.0), (1.0, 1.0), (1.0, -1.0), (-1.0, -1.0)];
let square = Polygon::convex_from_vertices((0.0, 0.0), vertices);
 
assert!(square.is_some());
 
let concave_vertices = vec![(0.0, 0.0), (4.0, 0.0), (4.0, 2.0), (2.0, 1.0), (0.0, 2.0)];
let concave_shape = Polygon::convex_from_vertices((0.0, 0.0), concave_vertices);
 
assert!(concave_shape.is_none());

Trait Implementations§

Source§

impl Clone for Polygon

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Polygon

Source§

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

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

impl Rotate for Polygon

Source§

fn rotate(&mut self, angle: f32)

Rotate the shape by the given angle, with the rotation counterclockwise when the Y-axis points up.
Source§

fn rotate_sincos(&mut self, sin: f32, cos: f32)

Rotate the shape using the given sine and cosine of an angle. Use this when you are rotating multiple shapes by the same angle and don’t want to re-calculate the trig functions.
Source§

impl Shape for Polygon

Source§

fn position(&self) -> (f32, f32)

The location of the shape in 2D space.
Source§

fn set_position(&mut self, position: (f32, f32))

Set the location of the shape.
Source§

fn num_axes(&self) -> usize

The number of axes the shape provides for testing. For polygons, it is the same as the number of vertices, but for circles it is simply one.
Source§

fn get_axis(&self, index: usize, _target: (f32, f32)) -> (f32, f32)

The method used to access the axes during the SAT calculations. This is used to avoid the memory allocation of a new vector or array each time we calculate collisions.
Source§

fn project(&self, axis: (f32, f32), _normalize: bool) -> (f32, f32)

Getting the minimum and maximum projection of the shape onto the given axis to look for overlap. Normalize denotes whether or not the axis passed in is a unit vector to avoid repeating calculations.
Source§

fn needs_closest(&self, _index: usize) -> bool

Determine whether or not the shape needs access to the closest vertex of another shape to check collisions.
Source§

fn get_closest(&self, target: (f32, f32)) -> (f32, f32)

Gets the closest vertex/primary point/position to the given target, NOT the closest point on the shape.
Source§

fn point(&self, index: usize) -> (f32, f32)

The point corresponding to the given axis, if applicable. Otherwise, position.

Auto Trait Implementations§

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.