-
-
Notifications
You must be signed in to change notification settings - Fork 359
Added Graham Scan in Haskell #112
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
Conversation
|
||
type Point = (Double, Double) | ||
|
||
angle :: Point -> Point -> Point -> Double |
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.
Is this the same as the old julia function called graham_angle
?
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'm not sure, I wrote it without looking at the Julia code. Why?
I know that it's little overdoing it since when it's used in the Graham scan the first two points are always fixed, so I could get away with something more ad-hoc. Although it could make the code slightly harder to read?
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.
Look at my C code as an example.
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.
An example of what? I see you use polar_angle.
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.
Would this not work?
angle :: Point -> Point -> Double
angle a@(xa, ya) b@(xb, yb)
| a==b = 0
| theta<0 = theta+2*pi
| otherwise = theta
where theta = atan2 (yb-ya) (xb-xa)
and
sortedPts = init $ sortOn (negate . angle p0) pts
I don't know Haskell BTW.
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.
Yes, it looks like it would work. I re-used the function that I wrote in Jarvis March for simplicity, but I suppose I could rework this one.
Got rid of the angle function altogether, I'm simply using atan2 inside the sort function now. |
You should be able to just use the dot product instead of explicitly computing the angle. This method is not only significantly faster, it's also less likely to be affected by floating point precision issues. It's also a very well known optimization of the Graham scan algorithm, ideally I think we should mention it in the chapter as well. |
@Gustorn Fair point. I'll make the appropriate changes. |
Suggestion for later: write a chapter where you cover angle measurements :) |
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 think this is clear enough now. The method to create the random distribution of points should probably be used in other hull methods.
As the title says + some trailing whitespaces in graham_scan.md