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
impl Polygon
Sourcepub fn new(position: (f32, f32)) -> Polygon
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));Sourcepub fn is_convex(&self) -> bool
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());Sourcepub fn add(&mut self, vertex: (f32, f32))
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)Sourcepub fn from_vertices(position: (f32, f32), vertices: Vec<(f32, f32)>) -> Polygon
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)Sourcepub fn convex_from_vertices(
position: (f32, f32),
vertices: Vec<(f32, f32)>,
) -> Option<Polygon>
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());