-
-
Notifications
You must be signed in to change notification settings - Fork 359
Add Graham scan in Rust #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||
#[derive(Debug)] | ||||||
struct Point { | ||||||
x: f64, | ||||||
y: f64, | ||||||
} | ||||||
|
||||||
// Check if the turn of the points is counter clockwise. | ||||||
fn counter_clockwise(a: &Point, b: &Point, c: &Point) -> bool { | ||||||
(b.x - a.x) * (c.y - a.y) >= (b.y - a.y) * (c.x - a.x) | ||||||
} | ||||||
|
||||||
// Calculate the polar angle of a point relative to a reference point. | ||||||
fn polar_angle(reference: &Point, point: &Point) -> f64 { | ||||||
(point.y - point.y).atan2(point.x - reference.x) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
fn graham_scan(mut points: Vec<Point>) -> Vec<Point> { | ||||||
// First, sort the points so the one with the lowest y-coordinate comes first (the pivot) | ||||||
sort_based_on_lowest_coordinate(&mut points); | ||||||
|
||||||
// Take ownership of the pivot point | ||||||
let pivot = points.remove(0); | ||||||
|
||||||
// Sort all points based on the angle between the pivot point and itself | ||||||
&mut points | ||||||
.sort_by(|a, b| (polar_angle(a, &pivot).partial_cmp(&polar_angle(b, &pivot))).unwrap()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
for consistency with the |
||||||
|
||||||
// Reinsert the pivot point | ||||||
points.insert(0, pivot); | ||||||
|
||||||
let n = points.len(); | ||||||
let mut m = 1; | ||||||
|
||||||
// Move the points of the hull towards the beginning of the vector. | ||||||
for mut i in 2..n { | ||||||
while counter_clockwise(&points[m - 1], &points[m], &points[i]) { | ||||||
if m > 1 { | ||||||
m -= 1; | ||||||
} else if m == i { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
break; | ||||||
} else { | ||||||
i += 1; | ||||||
} | ||||||
} | ||||||
|
||||||
m += 1; | ||||||
points.swap(i, m); | ||||||
} | ||||||
|
||||||
// Remove all non-hull points from the vector | ||||||
points.truncate(m + 1); | ||||||
points | ||||||
} | ||||||
|
||||||
fn sort_based_on_lowest_coordinate(points: &mut Vec<Point>) { | ||||||
points.sort_unstable_by(|a, b| (a.y).partial_cmp(&b.y).unwrap()); | ||||||
} | ||||||
|
||||||
fn main() { | ||||||
let points = vec![ | ||||||
Point { x: 1.0, y: 3.0 }, | ||||||
Point { x: 2.0, y: 4.0 }, | ||||||
Point { x: 4.0, y: 0.0 }, | ||||||
Point { x: 1.0, y: 0.0 }, | ||||||
Point { x: 0.0, y: 2.0 }, | ||||||
Point { x: 2.0, y: 2.0 }, | ||||||
Point { x: 3.0, y: 4.0 }, | ||||||
Point { x: 3.0, y: 1.0 }, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change these points to be the ones from the Julia example? |
||||||
]; | ||||||
|
||||||
let hull_points = graham_scan(points); | ||||||
println!("{:#?}", hull_points); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want this function to return a bool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed something in my previous review, which is that in order to be consistent with the text (https://www.algorithm-archive.org/contents/graham_scan/graham_scan.html),
this should return an integer.