-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmultilinestring.go
84 lines (74 loc) · 1.74 KB
/
multilinestring.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package geom
import (
"math"
polyclip "github.com/ctessum/polyclip-go"
)
// MultiLineString is a holder for multiple related LineStrings.
type MultiLineString []LineString
// Bounds gives the rectangular extents of the MultiLineString.
func (ml MultiLineString) Bounds() *Bounds {
b := NewBounds()
for _, l := range ml {
b.Extend(l.Bounds())
}
return b
}
// Length calculates the combined length of the linestrings in ml.
func (ml MultiLineString) Length() float64 {
length := 0.
for _, l := range ml {
length += l.Length()
}
return length
}
// Within calculates whether ml is completely within p or on its edge.
func (ml MultiLineString) Within(p Polygonal) WithinStatus {
for _, l := range ml {
if l.Within(p) == Outside {
return Outside
}
}
return Inside
}
// Distance calculates the shortest distance from p to the MultiLineString.
func (ml MultiLineString) Distance(p Point) float64 {
d := math.Inf(1)
for _, l := range ml {
lDist := l.Distance(p)
d = math.Min(d, lDist)
}
return d
}
// Clip returns the part of the receiver that falls within the given polygon.
func (ml MultiLineString) Clip(p Polygonal) Linear {
pTemp := make(Polygon, len(ml))
for i, l := range ml {
pTemp[i] = Path(l)
}
pTemp = pTemp.op(p, polyclip.CLIPLINE)
o := make(MultiLineString, len(pTemp))
for i, pp := range pTemp {
o[i] = LineString(pp[0 : len(pp)-1])
}
return o
}
// Len returns the number of points in the receiver.
func (ml MultiLineString) Len() int {
var i int
for _, l := range ml {
i += len(l)
}
return i
}
// Points returns an iterator for the points in the receiver.
func (ml MultiLineString) Points() func() Point {
var i, j int
return func() Point {
if i == len(ml[j]) {
j++
i = 0
}
i++
return ml[j][i-1]
}
}