Skip to content

Commit

Permalink
Fix point_in_poly2d for self-intersecting polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
sebcrozet committed Dec 4, 2024
1 parent dad61d2 commit fa18d6a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
37 changes: 30 additions & 7 deletions crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,35 @@ const RENDER_SCALE: f32 = 30.0;
#[macroquad::main("parry2d::utils::point_in_poly2d")]
async fn main() {
let mut spikes = spikes_polygon();
let mut squares = squares_polygon();
let test_points = grid_points();

let animation_rotation = UnitComplex::new(0.02);
let spikes_render_pos = Point2::new(screen_width() / 2.0, screen_height() / 2.0);
let polygon_render_pos = Point2::new(screen_width() / 2.0, screen_height() / 2.0);

for i in 0.. {
let polygon = if (i / 350) % 2 == 0 {
&mut spikes
} else {
&mut squares
};

loop {
clear_background(BLACK);

spikes
polygon
.iter_mut()
.for_each(|pt| *pt = animation_rotation * *pt);
draw_polygon(&spikes, RENDER_SCALE, spikes_render_pos, BLUE);

draw_polygon(&polygon, RENDER_SCALE, polygon_render_pos, BLUE);

/*
* Compute polygon intersections.
*/
for point in &test_points {
if point_in_poly2d(point, &spikes) {
draw_point(*point, RENDER_SCALE, spikes_render_pos, RED);
if point_in_poly2d(point, &polygon) {
draw_point(*point, RENDER_SCALE, polygon_render_pos, RED);
} else {
draw_point(*point, RENDER_SCALE, spikes_render_pos, GREEN);
draw_point(*point, RENDER_SCALE, polygon_render_pos, GREEN);
}
}

Expand Down Expand Up @@ -60,6 +68,21 @@ fn spikes_polygon() -> Vec<Point2<f32>> {
polygon
}

fn squares_polygon() -> Vec<Point2<f32>> {
let scale = 3.0;
[
Point2::new(-1.0, -1.0) * scale,
Point2::new(0.0, -1.0) * scale,
Point2::new(0.0, 1.0) * scale,
Point2::new(-2.0, 1.0) * scale,
Point2::new(-2.0, -2.0) * scale,
Point2::new(1.0, -2.0) * scale,
Point2::new(1.0, 2.0) * scale,
Point2::new(-1.0, 2.0) * scale,
]
.to_vec()
}

fn grid_points() -> Vec<Point2<f32>> {
let count = 40;
let spacing = 0.6;
Expand Down
21 changes: 19 additions & 2 deletions src/utils/point_in_poly2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,35 @@ pub fn point_in_poly2d(pt: &Point2<Real>, poly: &[Point2<Real>]) -> bool {
let perp = dpt.perp(&seg_dir);
winding += match (dpt.y >= 0.0, b.y > pt.y) {
(true, true) if perp < 0.0 => 1,
(false, false) if perp > 0.0 => -1,
(false, false) if perp > 0.0 => 1,
_ => 0,
};
}

winding != 0
winding % 2 == 1
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn point_in_poly2d_self_intersecting() {
let poly = [
[-1.0, -1.0],
[0.0, -1.0],
[0.0, 1.0],
[-2.0, 1.0],
[-2.0, -2.0],
[1.0, -2.0],
[1.0, 2.0],
[-1.0, 2.0],
]
.map(Point2::from);
assert!(!point_in_poly2d(&[-0.5, -0.5].into(), &poly));
assert!(point_in_poly2d(&[0.5, -0.5].into(), &poly));
}

#[test]
fn point_in_poly2d_concave() {
let poly = [
Expand Down

0 comments on commit fa18d6a

Please sign in to comment.