Function point_in_convex_poly2d

Source
pub fn point_in_convex_poly2d(pt: &Point2<f32>, poly: &[Point2<f32>]) -> bool
Expand description

Tests if the given point is inside a convex polygon with arbitrary orientation.

This function uses a faster algorithm than point_in_poly2d but only works for convex polygons. It checks if the point is on the same side of all edges of the polygon.

The polygon is assumed to be closed, i.e., first and last point of the polygon are implicitly assumed to be connected by an edge.

§Arguments

  • pt - The point to test
  • poly - A slice of points defining the convex polygon vertices in any order (clockwise or counter-clockwise)

§Returns

true if the point is inside or on the boundary of the polygon, false otherwise.

§Examples

§Point Inside a Square

use parry2d::utils::point_in_convex_poly2d;
use parry2d::na::Point2;

let square = vec![
    Point2::origin(),
    Point2::new(2.0, 0.0),
    Point2::new(2.0, 2.0),
    Point2::new(0.0, 2.0),
];

let inside = Point2::new(1.0, 1.0);
let outside = Point2::new(3.0, 1.0);

assert!(point_in_convex_poly2d(&inside, &square));
assert!(!point_in_convex_poly2d(&outside, &square));

§Point on the Boundary

use parry2d::utils::point_in_convex_poly2d;
use parry2d::na::Point2;

let triangle = vec![
    Point2::origin(),
    Point2::new(2.0, 0.0),
    Point2::new(1.0, 2.0),
];

let on_edge = Point2::new(1.0, 0.0);
assert!(point_in_convex_poly2d(&on_edge, &triangle));

§Empty Polygon

use parry2d::utils::point_in_convex_poly2d;
use parry2d::na::Point2;

let empty: Vec<Point2<f32>> = vec![];
let point = Point2::new(1.0, 1.0);

// An empty polygon contains no points
assert!(!point_in_convex_poly2d(&point, &empty));