Skip to content

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 5 commits into from
Jun 8, 2018

Conversation

jiegillet
Copy link
Member

As the title says + some trailing whitespaces in graham_scan.md

@Gathros Gathros added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Chapter Edit This changes the archive's chapters. (md files are edited.) labels May 14, 2018

type Point = (Double, Double)

angle :: Point -> Point -> Point -> Double
Copy link
Contributor

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?

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

@Gathros Gathros May 21, 2018

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.

Copy link
Member Author

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.

@jiegillet
Copy link
Member Author

Got rid of the angle function altogether, I'm simply using atan2 inside the sort function now.

@zsparal
Copy link
Contributor

zsparal commented May 22, 2018

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.

@leios
Copy link
Member

leios commented May 22, 2018

@Gustorn Fair point. I'll make the appropriate changes.

@jiegillet
Copy link
Member Author

Suggestion for later: write a chapter where you cover angle measurements :)
For now, I don't think it matters so much, very little of the code in the AAA is optimized anyway. I think I'll leave it like this, you read atan2, you know it's an angle. Discussing optimization possibilities in the chapter is good though.

Copy link
Member

@leios leios left a 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.

@leios leios merged commit c7bed7d into algorithm-archivists:master Jun 8, 2018
@june128 june128 removed the Chapter Edit This changes the archive's chapters. (md files are edited.) label Jun 11, 2018
@jiegillet jiegillet deleted the grahamScan branch August 4, 2018 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants