forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindingorder.go
66 lines (55 loc) · 1.36 KB
/
windingorder.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
package maths
import (
"github.com/go-spatial/tegola"
)
// WindingOrder the direction the line strings.
type WindingOrder uint8
const (
Clockwise WindingOrder = iota
CounterClockwise
)
func (w WindingOrder) String() string {
switch w {
case Clockwise:
return "clockwise"
case CounterClockwise:
return "counter clockwise"
}
return "unknown"
}
func (w WindingOrder) IsClockwise() bool { return w == Clockwise }
func (w WindingOrder) IsCounterClockwise() bool { return w == CounterClockwise }
func (w WindingOrder) Not() WindingOrder {
if w == Clockwise {
return CounterClockwise
}
return Clockwise
}
func WindingOrderOfPts(pts []Pt) WindingOrder {
sum := 0.0
li := len(pts) - 1
for i := range pts[:li] {
sum += (pts[i].X * pts[i+1].Y) - (pts[i+1].X * pts[i].Y)
}
sum += (pts[li].X * pts[0].Y) - (pts[0].X * pts[li].Y)
//log.Println("For pts:", pts, "sum", sum)
if sum < 0 {
return CounterClockwise
}
return Clockwise
}
func WindingOrderOf(sub []float64) WindingOrder {
pts := make([]Pt, 0, len(sub)/2)
for x, y := 0, 1; y < len(sub); x, y = x+2, y+2 {
pts = append(pts, Pt{sub[x], sub[y]})
}
return WindingOrderOfPts(pts)
}
func WindingOrderOfLine(l tegola.LineString) WindingOrder {
lpts := l.Subpoints()
pts := make([]Pt, 0, len(lpts))
for _, pt := range lpts {
pts = append(pts, Pt{pt.X(), pt.Y()})
}
return WindingOrderOfPts(pts)
}