-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpointchain.go
128 lines (108 loc) · 2.85 KB
/
pointchain.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package martinez_rueda
import (
"github.com/paulmach/orb"
)
type PointChain struct {
segments []orb.Point
closed bool
}
func NewPointChain(initSegment Segment) *PointChain {
return &PointChain{
segments: []orb.Point{initSegment.begin(), initSegment.end()},
}
}
func (pc *PointChain) begin() orb.Point {
return pc.segments[0]
}
func (pc *PointChain) end() orb.Point {
return pc.segments[len(pc.segments)-1]
}
func (pc *PointChain) linkSegment(segment Segment) bool {
front := pc.begin()
back := pc.end()
// CASE 1
if segment.begin().Equal(front) {
if segment.end().Equal(back) {
pc.closed = true
} else {
// Prepend one elements to the beginning of an array
pc.segments = append([]orb.Point{segment.end()}, pc.segments...)
}
return true
}
// CASE 2
if segment.end().Equal(back) {
if segment.begin().Equal(front) {
pc.closed = true
} else {
pc.segments = append(pc.segments, segment.begin())
}
return true
}
// CASE 3
if segment.end().Equal(front) {
if segment.begin().Equal(back) {
pc.closed = true
} else {
// Prepend one elements to the beginning of an array
pc.segments = append([]orb.Point{segment.begin()}, pc.segments...)
}
return true
}
// CASE 4
if segment.begin().Equal(back) {
if segment.end().Equal(front) {
pc.closed = true
} else {
pc.segments = append(pc.segments, segment.end())
}
return true
}
return false
}
func (pc *PointChain) linkChain(other *PointChain) bool {
front := pc.begin()
back := pc.end()
otherFront := other.begin()
otherBack := other.end()
if otherFront.Equal(back) {
// Shift an element off the beginning of array
other.segments = other.segments[1:]
// insert at the end of $this->segments
pc.segments = append(pc.segments, other.segments...)
return true
}
if otherBack.Equal(front) {
// Shift an element off the beginning of array
pc.segments = pc.segments[1:]
// insert at the beginning of $this->segments
pc.segments = append(other.segments, pc.segments...)
return true
}
if otherFront.Equal(front) {
// Shift an element off the beginning of array
pc.segments = pc.segments[1:]
other.segments = segmentsReverse(other.segments)
// insert reversed at the beginning of $this->segments
pc.segments = append(other.segments, pc.segments...)
return true
}
if otherBack.Equal(back) {
// array_pop — Pop the element off the end of array
pc.segments = pc.segments[:(len(pc.segments) - 1)]
other.segments = segmentsReverse(other.segments)
// insert reversed at the end of $this->segments
pc.segments = append(pc.segments, other.segments...)
return true
}
return false
}
func segmentsReverse(segments []orb.Point) []orb.Point {
for i, j := 0, len(segments)-1; i < j; i, j = i+1, j-1 {
segments[i], segments[j] = segments[j], segments[i]
}
return segments
}
func (pc *PointChain) Size() int {
return len(pc.segments)
}