Skip to content

Commit

Permalink
aa_rectangle sq_distance bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Dec 9, 2024
1 parent 9dad9e1 commit caf531d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
18 changes: 8 additions & 10 deletions jagua-rs/src/geometry/convex_hull.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fsize;
use crate::geometry::primitives::point::Point;
use crate::geometry::primitives::simple_polygon::SimplePolygon;
use ordered_float::OrderedFloat;

/// Returns the indices of the points in the [SimplePolygon] that form the [convex hull](https://en.wikipedia.org/wiki/Convex_hull)
pub fn convex_hull_indices(shape: &SimplePolygon) -> Vec<usize> {
Expand Down Expand Up @@ -32,18 +33,15 @@ pub fn convex_hull_from_points(mut points: Vec<Point>) -> Vec<Point> {
//https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain

//sort the points by x coordinate
points.sort_by(|a, b| {
let (a_x, b_x) = (a.0, b.0);
a_x.partial_cmp(&b_x).unwrap()
});
points.sort_by_key(|p| OrderedFloat(p.0));

let mut lower_hull = points
.iter()
.fold(vec![], |hull, p| grow_convex_hull(hull, p));
.fold(vec![], |hull, p| grow_convex_hull(hull, *p));
let mut upper_hull = points
.iter()
.rev()
.fold(vec![], |hull, p| grow_convex_hull(hull, p));
.fold(vec![], |hull, p| grow_convex_hull(hull, *p));

//First and last element of both hull parts are the same point
upper_hull.pop();
Expand All @@ -53,15 +51,15 @@ pub fn convex_hull_from_points(mut points: Vec<Point>) -> Vec<Point> {
lower_hull
}

fn grow_convex_hull(mut h: Vec<Point>, np: &Point) -> Vec<Point> {
fn grow_convex_hull(mut h: Vec<Point>, next: Point) -> Vec<Point> {
//pop all points from the hull which will be made irrelevant due to the new point
while h.len() >= 2 && cross(&h[h.len() - 2], &h[h.len() - 1], np) <= 0.0 {
while h.len() >= 2 && cross(h[h.len() - 2], h[h.len() - 1], next) <= 0.0 {
h.pop();
}
h.push(*np);
h.push(next);
h
}

fn cross(a: &Point, b: &Point, c: &Point) -> fsize {
fn cross(a: Point, b: Point, c: Point) -> fsize {
(b.0 - a.0) * (c.1 - a.1) - (b.1 - a.1) * (c.0 - a.0)
}
4 changes: 2 additions & 2 deletions jagua-rs/src/geometry/primitives/aa_rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl DistanceFrom<Point> for AARectangle {
match self.collides_with(point) {
false => (GeoPosition::Exterior, self.sq_distance(point)),
true => {
let (x, y) = (NotNan::new(point.0).unwrap(), NotNan::new(point.1).unwrap());
let Point(x, y) = *point;
let min_distance = [
(x - self.x_min).abs(),
(x - self.x_max).abs(),
Expand All @@ -323,7 +323,7 @@ impl DistanceFrom<Point> for AARectangle {
.into_iter()
.min_by_key(|&d| OrderedFloat(d))
.unwrap();
(GeoPosition::Interior, min_distance)
(GeoPosition::Interior, min_distance.powi(2))
}
}
}
Expand Down

0 comments on commit caf531d

Please sign in to comment.