[][src]Struct fart_2d_geom::ConvexPolygon

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

A convex polygon.

This is a thin newtype wrapper over Polygon, and dereferences to the underlying Polygon, but it's guaranteed that this polygon is convex.

Methods

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

pub fn hull(vertices: Vec<TypedPoint2D<T, U>>) -> Option<ConvexPolygon<T, U>>[src]

Compute the convex hull of the given vertices.

If the convex hull is a polygon with non-zero area, return it. Otherwise return None.

Example

use euclid::{point2, UnknownUnit};
use fart_2d_geom::ConvexPolygon;
use std::collections::HashSet;

let hull = ConvexPolygon::<i32, UnknownUnit>::hull(vec![
    point2(0, 0),
    point2(0, 1),
    point2(0, 2),
    point2(1, 0),
    point2(1, 1),
    point2(1, 2),
    point2(2, 0),
    point2(2, 1),
    point2(2, 2),
]).expect("should have a convex hull for non-collinear vertex sets");

let actual_hull_vertices = hull.vertices().iter().cloned().collect::<HashSet<_>>();

let expected_hull_vertices = vec![
    point2(0, 0),
    point2(0, 2),
    point2(2, 0),
    point2(2, 2)
].into_iter().collect::<HashSet<_>>();

assert_eq!(actual_hull_vertices, expected_hull_vertices);

// Returns `None` for empty and collinear sets.
assert!(ConvexPolygon::<i32, UnknownUnit>::hull(vec![]).is_none());
assert!(ConvexPolygon::<i32, UnknownUnit>::hull(vec![point2(0, 0)]).is_none());
assert!(ConvexPolygon::<i32, UnknownUnit>::hull(vec![point2(0, 0), point2(1, 1)]).is_none());
assert!(ConvexPolygon::<i32, UnknownUnit>::hull(vec![point2(0, 0), point2(1, 1), point2(2, 2)]).is_none());

pub fn contains_point(&self, point: TypedPoint2D<T, U>) -> bool[src]

Does this convex polygon properly contain the given point?

Example

use euclid::point2;
use fart_2d_geom::ConvexPolygon;

let p = ConvexPolygon::<i32, ()>::hull(vec![
    point2(0, 0),
    point2(10, 2),
    point2(5, 10),
]).unwrap();

assert!(p.contains_point(point2(5, 5)));
assert!(!p.contains_point(point2(-3, -3)));

// Points exactly on the edge are not considered contained.
assert!(!p.contains_point(point2(0, 0)));

pub fn improperly_contains_point(&self, point: TypedPoint2D<T, U>) -> bool[src]

Does this convex polygon properly contain the given point?

Example

use euclid::point2;
use fart_2d_geom::ConvexPolygon;

let p = ConvexPolygon::<i32, ()>::hull(vec![
    point2(0, 0),
    point2(10, 2),
    point2(5, 10),
]).unwrap();

assert!(p.improperly_contains_point(point2(5, 5)));
assert!(!p.improperly_contains_point(point2(-3, -3)));

// Points exactly on the edge are considered contained.
assert!(p.improperly_contains_point(point2(0, 0)));

Methods from Deref<Target = Polygon<T, U>>

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 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 ConvexPolygon<T, U>[src]

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

Performs copy-assignment from source. Read more

impl<T, U> DerefMut for ConvexPolygon<T, U>[src]

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

impl<T, U> Deref for ConvexPolygon<T, U>[src]

type Target = Polygon<T, U>

The resulting type after dereferencing.

Auto Trait Implementations

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

impl<T, U> Sync for ConvexPolygon<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.