-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0044a37
Added Graham Scan in Haskell
jiegillet 1011c2a
Fixed merge issue with c code, modified angle function
jiegillet c6e153b
Added forgotten title for C
jiegillet 1134af1
Added comments
jiegillet 6f61ffe
Merge branch 'master' into grahamScan
leios File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
chapters/computational_geometry/gift_wrapping/graham_scan/code/haskell/grahamScan.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Data.List (sortOn, minimumBy) | ||
import Data.Function (on) | ||
|
||
type Point = (Double, Double) | ||
|
||
angle :: Point -> Point -> Point -> Double | ||
angle a@(xa, ya) b@(xb, yb) c@(xc, yc) | ||
| a==b || c==b = 0 | ||
| theta<0 = theta+2*pi | ||
| otherwise = theta | ||
where thetaA = atan2 (ya-yb) (xa-xb) | ||
thetaC = atan2 (yc-yb) (xc-xb) | ||
theta = thetaC - thetaA | ||
|
||
ccw :: Point -> Point -> Point -> Double | ||
ccw (xa, ya) (xb, yb) (xc, yc) = (xb - xa) * (yc - ya) - (yb - ya) * (xc - xa) | ||
|
||
grahamScan :: [Point] -> [Point] | ||
grahamScan [] = [] | ||
grahamScan pts = wrap sortedPts [p0] | ||
where p0@(x, y)= minimumBy (compare `on` snd) pts | ||
sortedPts = init $ sortOn (negate . angle (x, y-1) p0) pts | ||
wrap [] p = p | ||
wrap (s:ss) [p] = wrap ss [s, p] | ||
wrap (s:ss) (p1:p2:ps) | ||
| ccw s p1 p2 < 0 = wrap (s:ss) (p2:ps) | ||
| otherwise = wrap ss (s:p1:p2:ps) | ||
|
||
main = do | ||
let pts = [(x,y) | x<-[-5..5], y<-[-5..5], x^2+y^2<=5^2] | ||
print $ grahamScan pts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Uh oh!
There was an error while loading. Please reload this page.
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?
and
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.