-
Notifications
You must be signed in to change notification settings - Fork 105
/
linestring.go
99 lines (87 loc) · 2.44 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package geom
// A LineString represents a single, unbroken line, linearly interpreted
// between zero or more control points.
type LineString struct {
geom1
}
// NewLineString returns a new LineString with layout l and no control points.
func NewLineString(l Layout) *LineString {
return NewLineStringFlat(l, nil)
}
// NewLineStringFlat returns a new LineString with layout l and control points
// flatCoords.
func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString {
g := new(LineString)
g.layout = layout
g.stride = layout.Stride()
g.flatCoords = flatCoords
return g
}
// Area returns the area of g, i.e. zero.
func (g *LineString) Area() float64 {
return 0
}
// Clone returns a copy of g that does not alias g.
func (g *LineString) Clone() *LineString {
return deriveCloneLineString(g)
}
// Interpolate returns the index and delta of val in dimension dim.
func (g *LineString) Interpolate(val float64, dim int) (int, float64) {
n := len(g.flatCoords)
if n == 0 {
panic("geom: empty linestring")
}
if val <= g.flatCoords[dim] {
return 0, 0
}
if g.flatCoords[n-g.stride+dim] <= val {
return (n - 1) / g.stride, 0
}
low := 0
high := n / g.stride
for low < high {
mid := (low + high) / 2
if val < g.flatCoords[mid*g.stride+dim] {
high = mid
} else {
low = mid + 1
}
}
low--
val0 := g.flatCoords[low*g.stride+dim]
if val == val0 {
return low, 0
}
val1 := g.flatCoords[(low+1)*g.stride+dim]
return low, (val - val0) / (val1 - val0)
}
// Length returns the length of g.
func (g *LineString) Length() float64 {
return length1(g.flatCoords, 0, len(g.flatCoords), g.stride)
}
// MustSetCoords is like SetCoords but it panics on any error.
func (g *LineString) MustSetCoords(coords []Coord) *LineString {
Must(g.SetCoords(coords))
return g
}
// SetCoords sets the coordinates of g.
func (g *LineString) SetCoords(coords []Coord) (*LineString, error) {
if err := g.setCoords(coords); err != nil {
return nil, err
}
return g, nil
}
// SetSRID sets the SRID of g.
func (g *LineString) SetSRID(srid int) *LineString {
g.srid = srid
return g
}
// SubLineString returns a LineString from starts at index start and stops at
// index stop of g. The returned LineString aliases g.
func (g *LineString) SubLineString(start, stop int) *LineString {
return NewLineStringFlat(g.layout, g.flatCoords[start*g.stride:stop*g.stride])
}
// Swap swaps the values of g and g2.
func (g *LineString) Swap(g2 *LineString) {
*g, *g2 = *g2, *g
}