Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Fixed merge issue with c code, modified angle function
  • Loading branch information
jiegillet committed May 22, 2018
commit 1011c2aaae08605636b4636be5de95bf48a5111d
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@ 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
sortedPts = sortOn (\(px, py) -> atan2 (py-y) (px-x) ) $ filter (/=p0) pts
wrap [] ps = ps
wrap (s:ss) [p] = wrap ss [s, p]
wrap (s:ss) (p1:p2:ps)
| ccw s p1 p2 < 0 = wrap (s:ss) (p2:ps)
| ccw s p1 p2 > 0 = wrap (s:ss) (p2:ps)
| otherwise = wrap ss (s:p1:p2:ps)

main = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
{% sample lang="jl" %}
[import:30-32, lang:"julia"](code/julia/graham.jl)
{% sample lang="hs" %}
[import:15-16, lang:"haskell"](code/haskell/grahamScan.hs)
[import:6-7, lang:"haskell"](code/haskell/grahamScan.hs)
{% sample lang="c" %}
[import:24-26, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

If the output of this function is 0, the points are collinear.
Expand All @@ -33,7 +35,9 @@ In the end, the code should look something like this:
{% sample lang="jl" %}
[import:34-69, lang:"julia"](code/julia/graham.jl)
{% sample lang="hs" %}
[import:18-27, lang:"haskell"](code/haskell/grahamScan.hs)
[import:9-18, lang:"haskell"](code/haskell/grahamScan.hs)
{% sample lang="c" %}
[import:65-95, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

### Bibliography
Expand All @@ -49,6 +53,8 @@ In the end, the code should look something like this:
{% sample lang="hs" %}
### Haskell
[import, lang:"haskell"](code/haskell/grahamScan.hs)
{% sample lang="c" %}
[import, lang:"c_cpp"](code/c/graham.c)
{% endmethod %}

<script>
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.