-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
line_string.go
61 lines (51 loc) · 1.48 KB
/
line_string.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
package geom
import (
"errors"
)
// ErrNilLineString is thrown when a LineString is nil but shouldn't be
var ErrNilLineString = errors.New("geom: nil LineString")
// ErrInvalidLineString is thrown when a LineString is malformed
var ErrInvalidLineString = errors.New("geom: invalid LineString")
// LineString is a basic line type which is made up of two or more points that don't interacted.
type LineString [][2]float64
/* We need to change this to a WalkPoints function.
// Points returns a slice of XY values
func (ls LineString) Points() [][2]float64 {
return ls
}
*/
// IsRing returns true if the first and last vertices are the same
func (ls LineString) IsRing() bool {
last := len(ls) - 1
if len(ls) > 1 && ls[0][0] == ls[last][0] && ls[0][1] == ls[last][1] {
return true
}
return false
}
// Vertices returns a slice of XY values
func (ls LineString) Vertices() [][2]float64 { return ls }
// SetVertices modifies the array of 2D coordinates
func (ls *LineString) SetVertices(input [][2]float64) (err error) {
if ls == nil {
return ErrNilLineString
}
*ls = append((*ls)[:0], input...)
return
}
// AsSegments returns the line string as a slice of lines.
func (ls LineString) AsSegments() (segs []Line, err error) {
switch len(ls) {
case 0:
return nil, nil
case 1:
return nil, ErrInvalidLineString
case 2:
return []Line{{ls[0], ls[1]}}, nil
default:
segs = make([]Line, len(ls)-1)
for i := 0; i < len(ls)-1; i++ {
segs[i] = Line{ls[i], ls[i+1]}
}
return segs, nil
}
}