-
Notifications
You must be signed in to change notification settings - Fork 16
/
linestring.go
70 lines (60 loc) · 1.56 KB
/
linestring.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
package geom
import (
"math"
polyclip "github.com/ctessum/polyclip-go"
)
// LineString is a number of points that make up a path or line.
type LineString Path
// Bounds gives the rectangular extents of the LineString.
func (l LineString) Bounds() *Bounds {
b := NewBounds()
b.extendPoints(l)
return b
}
// Length calculates the length of l.
func (l LineString) Length() float64 {
length := 0.
for i := 0; i < len(l)-1; i++ {
p1 := l[i]
p2 := l[i+1]
length += math.Hypot(p2.X-p1.X, p2.Y-p1.Y)
}
return length
}
// Within calculates whether l is completely within p or touching its edge.
func (l LineString) Within(p Polygonal) WithinStatus {
for _, pp := range l {
if pointInPolygonal(pp, p) == Outside {
return Outside
}
}
return Inside
}
// Distance calculates the shortest distance from p to the LineString.
func (l LineString) Distance(p Point) float64 {
d := math.Inf(1)
for i := 0; i < len(l)-1; i++ {
segDist := distPointToSegment(p, l[i], l[i+1])
d = math.Min(d, segDist)
}
return d
}
// Clip returns the part of the receiver that falls within the given polygon.
func (l LineString) Clip(p Polygonal) Linear {
pTemp := Polygon{Path(l)}.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 (l LineString) Len() int { return len(l) }
// Points returns an iterator for the points in the receiver.
func (l LineString) Points() func() Point {
var i int
return func() Point {
i++
return l[i-1]
}
}