[][src]Struct fart_2d_geom::Polygon

pub struct Polygon<T, U> { /* fields omitted */ }

A polygon.

The polygon's vertices are in counter-clockwise order.

No guarantees whether this polygon is convex or not.

  • T is the numeric type. i32 or f64 etc.
  • U is the unit. ScreenSpace or WorldSpace etc.

Methods

impl<T, U> Polygon<T, U> where
    T: Copy + NumAssign + PartialOrd + Signed + Debug
[src]

pub fn new(vertices: Vec<TypedPoint2D<T, U>>) -> Polygon<T, U>[src]

Construct a new polygon.

pub fn random(
    rng: &mut dyn RngCore,
    x_dist: &mut impl Distribution<T>,
    y_dist: &mut impl Distribution<T>,
    n: usize
) -> Polygon<T, U> where
    T: NumCast
[src]

Generate a random n-gon with the given x an y point distributions.

use euclid::UnknownUnit;
use fart_2d_geom::Polygon;
use rand::{thread_rng, distributions::Uniform};

// Generate a random pentagon whose vertices are uniformly distributed
// between `(0, 0)` and `(100, 100)`
let pentagon = Polygon::<f64, UnknownUnit>::random(
    &mut thread_rng(),
    &mut Uniform::new(0.0, 100.0),
    &mut Uniform::new(0.0, 100.0),
    5
);

pub fn vertices(&self) -> &[TypedPoint2D<T, U>][src]

Get this polygon's vertices.

pub fn get(&self, i: usize) -> Option<TypedPoint2D<T, U>>[src]

Get the ith point in this polygon.

pub fn len(&self) -> usize[src]

Get the number of vertices in this polygon.

pub fn next(&self, i: usize) -> usize[src]

Get the index of the next vertex after i in this polygon. Handles wrapping around back to index 0 for you.

pub fn prev(&self, i: usize) -> usize[src]

Get the index of the previous vertex after i in this polygon. Handles wrapping around back to index n - 1 for you.

pub fn area(&self) -> T where
    T: Signed
[src]

Get the area of this polygon.

use euclid::{point2, UnknownUnit};
use fart_2d_geom::Polygon;

let square: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(0, 0),
    point2(10, 0),
    point2(10, 10),
    point2(0, 10),
]);

assert_eq!(square.area(), 100);

let triangle: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(-6, -6),
    point2(6, 0),
    point2(0, 0),
]);

assert_eq!(triangle.area(), 18);

pub fn is_diagonal(&self, a: usize, b: usize) -> bool[src]

Do the ath and bth vertices within this polygon form a diagonal?

If a and b are diagonal, then there is a direct line of sight between them, and they are internal to this polygon.

use euclid::{point2, UnknownUnit};
use fart_2d_geom::Polygon;

let p: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(0,  0),
    point2(10, 0),
    point2(5,  5),
    point2(10, 10),
    point2(0,  10),
]);

assert!(p.is_diagonal(0, 2));
assert!(!p.is_diagonal(1, 3));

pub fn in_cone(&self, a: usize, b: usize) -> bool[src]

Is b within the cone from prev(a) to a to next(a)?

use euclid::{point2, UnknownUnit};
use fart_2d_geom::Polygon;

// For a convex polygon, all non-adjacent points should be in cone.
let p: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(2, -1),
    point2(1, 2),
    point2(0, -2),
    point2(1, -2),
]);

assert!(p.in_cone(0, 2));
assert!(p.in_cone(1, 3));
assert!(p.in_cone(2, 0));
assert!(p.in_cone(3, 1));

// For a non-convex polygon, that is not always the case!
let p: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(0, 0),
    point2(3, -1),
    point2(1, 0),
    point2(3, 1),
]);

assert!(p.in_cone(0, 2));
assert!(!p.in_cone(1, 3));
assert!(p.in_cone(2, 0));
assert!(!p.in_cone(3, 1));

pub fn triangulate<F>(self, f: F) where
    F: FnMut(TypedPoint2D<T, U>, TypedPoint2D<T, U>, TypedPoint2D<T, U>), 
[src]

Triangulate this polygon by ear cutting.

The given function f is invoked with the vertices that make up each triangle in this polygon's triangulation.

This is an O(n2) algorithm.

use euclid::{point2, UnknownUnit};
use fart_2d_geom::Polygon;

let p: Polygon<i32, UnknownUnit> = Polygon::new(vec![
    point2(0, 0),
    point2(1, 0),
    point2(1, 1),
    point2(0, 1),
]);

p.triangulate(|a, b, c| {
    println!("Triangle within the triangulation: {:?} {:?} {:?}", a, b, c);
});

pub fn edges<'a>(&'a self) -> impl 'a + Iterator<Item = Line<T, U>>[src]

Iterate over this polygon's edge lines.

Example

use euclid::point2;
use fart_2d_geom::{line, Polygon};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct WorldSpaceUnits;

let p = Polygon::<i32, WorldSpaceUnits>::new(vec![
    point2(0, 0),
    point2(0, 2),
    point2(1, 1),
    point2(2, 2),
    point2(0, 2),
]);

assert_eq!(
    p.edges().collect::<Vec<_>>(),
    [
        line(point2(0, 0), point2(0, 2)),
        line(point2(0, 2), point2(1, 1)),
        line(point2(1, 1), point2(2, 2)),
        line(point2(2, 2), point2(0, 2)),
        line(point2(0, 2), point2(0, 0)),
    ]
);

Trait Implementations

impl<T: Clone, U: Clone> Clone for Polygon<T, U>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T, U> Debug for Polygon<T, U> where
    T: Debug
[src]

Auto Trait Implementations

impl<T, U> Send for Polygon<T, U> where
    T: Send,
    U: Send

impl<T, U> Sync for Polygon<T, U> where
    T: Sync,
    U: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.