forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jarvis.go
68 lines (52 loc) · 1.23 KB
/
jarvis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
)
type point struct {
x, y float64
}
func leftMostPoint(points []point) point {
ret := points[0]
for _, p := range points {
if (p.x < ret.x) || (p.x == ret.x && p.y < ret.y) {
ret = p
}
}
return ret
}
func (p point) equal(o point) bool {
return p.x == o.x && p.y == o.x
}
func counterClockWise(p1, p2, p3 point) bool {
return (p3.y-p1.y)*(p2.x-p1.x) >= (p2.y-p1.y)*(p3.x-p1.x)
}
func jarvisMarch(points []point) []point {
hullPoints := make([]point, 0)
hullPoint := leftMostPoint(points)
hullPoints = append(hullPoints, hullPoint)
for {
endPoint := points[0]
for _, p := range points[1:] {
if endPoint.equal(hullPoint) || !counterClockWise(p, hullPoints[len(hullPoints)-1], endPoint) {
endPoint = p
}
}
hullPoint = endPoint
if endPoint.equal(hullPoints[0]) {
break
}
hullPoints = append(hullPoints, hullPoint)
}
return hullPoints
}
func main() {
points := []point{{-5, 2}, {5, 7}, {-6, -12}, {-14, -14}, {9, 9},
{-1, -1}, {-10, 11}, {-6, 15}, {-6, -8}, {15, -9},
{7, -7}, {-2, -9}, {6, -5}, {0, 14}, {2, 8},
}
hullPoints := jarvisMarch(points)
fmt.Println("The hull points are:")
for _, p := range hullPoints {
fmt.Printf("x=%f y=%f\n", p.x, p.y)
}
}