Skip to content

Commit 2fc1d8f

Browse files
committed
Jarvis March in Haskell
1 parent 388799c commit 2fc1d8f

File tree

2 files changed

+26
-53
lines changed

2 files changed

+26
-53
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Data.List (sort, maximumBy)
2+
import Data.Function (on)
3+
4+
type Point = (Double, Double)
5+
6+
angle :: Point -> Point -> Point -> Double
7+
angle a@(xa, ya) b@(xb, yb) c@(xc, yc)
8+
| a==b || c==b = 0
9+
| theta<0 = theta+2*pi
10+
| otherwise = theta
11+
where thetaA = atan2 (ya-yb) (xa-xb)
12+
thetaC = atan2 (yc-yb) (xc-xb)
13+
theta = thetaC - thetaA
14+
15+
jarvisMarch :: [Point] -> [Point]
16+
jarvisMarch [] = []
17+
jarvisMarch pts = p0 : wrap (x, y-1) p0
18+
where p0@(x, y)= minimum pts
19+
wrap p1 p2
20+
| pm == p0 = []
21+
| otherwise = pm : wrap p2 pm
22+
where pm = maximumBy (compare `on` angle p1 p2) pts
23+
24+
main = do
25+
let pts = filter (\(x,y) -> x^2+y^2<=5^2) [(x,y)|x<-[-5..5], y<-[-5..5]]
26+
print $ jarvisMarch pts

chapters/computational_geometry/gift_wrapping/jarvis_march/jarvis_march.md

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)