Skip to main content

point_in_polygon

Function point_in_polygon 

Source
pub fn point_in_polygon<T>(point: &Point<T, T>, polygon: &Polygon<T>) -> bool
where T: Clone + Copy + PartialOrd + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Add<Output = T> + Zero + Num + AddAssign + Ord,
Expand description

Checks if a point is inside a polygon using the ray casting algorithm

A horizontal ray from the point to the right intersects an edge if:

$$(y_i > q_y) \neq (y_j > q_y) \land q_x < \frac{x_j - x_i}{y_j - y_i}(q_y - y_i) + x_i$$

§Arguments

  • point - The point to check
  • polygon - The polygon to test against

§Returns

true if the point is inside the polygon, false otherwise

§Examples

use physdes::{Point, algorithms::point_in_polygon};
use physdes::Polygon;

let points = vec![
    Point::new(0, 0),
    Point::new(10, 0),
    Point::new(10, 10),
    Point::new(0, 10),
];
let polygon = Polygon::new(&points);

assert!(point_in_polygon(&Point::new(5, 5), &polygon));
assert!(!point_in_polygon(&Point::new(15, 15), &polygon));