-
Notifications
You must be signed in to change notification settings - Fork 2
/
line.go
362 lines (286 loc) · 7.81 KB
/
line.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package cirno
import (
"fmt"
"math"
"sort"
)
// Line represents a geometric euclidian line segment
// from p to q.
type Line struct {
p Vector
q Vector
angle float64
tag
data
domain
}
// TypeName returns the name of the shape type.
func (l *Line) TypeName() string {
return "Line"
}
// Center returns the coordinates of the middle point
// between p and q.
func (l *Line) Center() Vector {
return NewVector((l.q.X+l.p.X)/2.0, (l.q.Y+l.p.Y)/2.0)
}
// Angle returns the rotation angle of the line (un degrees).
func (l *Line) Angle() float64 {
return l.angle
}
// AngleRadians returns the rotation angle of the line (un radians).
func (l *Line) AngleRadians() float64 {
return l.angle * DegToRad
}
// P returns the starting point of the line.
func (l *Line) P() Vector {
return l.p
}
// Q returns the ending point of the line.
func (l *Line) Q() Vector {
return l.q
}
// Move moves the line in the specified direction
// and returns its new position.
func (l *Line) Move(direction Vector) Vector {
l.q = l.q.Add(direction)
l.p = l.p.Add(direction)
return l.Center()
}
// SetPosition sets the position of the line
// to the given coordinates.
func (l *Line) SetPosition(pos Vector) Vector {
direction := pos.Subtract(l.Center())
return l.Move(direction)
}
// SetAngle sets the rotation angle of the line
// to the specified value (in degrees).
func (l *Line) SetAngle(angle float64) float64 {
return l.Rotate(angle - l.angle)
}
// SetAngleRadians sets the rotation angle of the line
// to the specified value (in radians).
func (l *Line) SetAngleRadians(angle float64) float64 {
return l.RotateRadians(angle - l.angle)
}
// Rotate rotates the line at the
// specified angle (in degrees).
//
// Returns the rotation angle of
// the line (in degrees).
func (l *Line) Rotate(angle float64) float64 {
center := l.Center()
pv := l.p.Subtract(center)
qv := l.q.Subtract(center)
pv = pv.Rotate(angle)
qv = qv.Rotate(angle)
l.p = center.Add(pv)
l.q = center.Add(qv)
l.angle += angle
l.angle = AdjustAngle(l.angle)
return l.angle
}
// RotateRadians rotates the line at the
// specified angle (in radians).
//
// Returns the rotation angle of
// the line (in radians).
func (l *Line) RotateRadians(angle float64) float64 {
return l.Rotate(angle*RadToDeg) * DegToRad
}
// RotateAround rotates the line around the base point.
func (l *Line) RotateAround(angle float64, base Vector) Vector {
center := l.Center()
cp := l.p.Subtract(center)
cq := l.q.Subtract(center)
center = center.RotateAround(angle, base)
l.p = center.Add(cp)
l.q = center.Add(cq)
return center
}
// RotateAroundRadians rotates the line around the base point at the
// angle in radians.
func (l *Line) RotateAroundRadians(angle float64, base Vector) Vector {
center := l.Center()
cp := l.p.Subtract(center)
cq := l.q.Subtract(center)
center = center.RotateAroundRadians(angle, base)
l.p = center.Add(cp)
l.q = center.Add(cq)
return center
}
// Length returns the length of the line.
func (l *Line) Length() float64 {
return l.q.Subtract(l.p).Magnitude()
}
// SquaredLength returns the length of the line
// in the power of 2.
func (l *Line) SquaredLength() float64 {
return l.q.Subtract(l.p).SquaredMagnitude()
}
// ContainsPoint detects if the point lies on the line.
func (l *Line) ContainsPoint(point Vector) bool {
lTmp := &Line{
p: Zero(),
q: l.q.Subtract(l.p),
}
pTmp := point.Subtract(l.p)
r := Cross(lTmp.q, pTmp)
min, max := l.GetBoundingBox()
return math.Abs(r) < Epsilon &&
point.X >= min.X &&
point.Y >= min.Y &&
point.X <= max.X &&
point.Y <= max.Y
}
// Orientation returns 0 if the point is collinear to the line,
// 1 if orientation is clockwise,
// -1 if orientation is counter-clockwise.
func (l *Line) Orientation(point Vector) int {
val := (l.q.Y-l.p.Y)*(point.X-l.q.X) -
(l.q.X-l.p.X)*(point.Y-l.q.Y)
if val == 0 {
return 0
}
if val > 0 {
return 1
}
return -1
}
// GetBoundingBox returns the bounding box for the line.
func (l *Line) GetBoundingBox() (Vector, Vector) {
min := NewVector(math.Min(l.p.X, l.q.X), math.Min(l.p.Y, l.q.Y))
max := NewVector(math.Max(l.p.X, l.q.X), math.Max(l.p.Y, l.q.Y))
return min, max
}
// CollinearTo returns true if the lines are collinear,
// and false otherwise.
func (l *Line) CollinearTo(other *Line) (bool, error) {
if other == nil {
return false, fmt.Errorf("the line is nil")
}
lVec := l.q.Subtract(l.p)
otherVec := other.q.Subtract(other.p)
return lVec.CollinearTo(otherVec)
}
// SameLineWith returns true if two line segments
// lie on the same line.
func (l *Line) SameLineWith(other *Line) (bool, error) {
if other == nil {
return false, fmt.Errorf("the line is nil")
}
projP := l.ProjectPoint(other.p)
projQ := l.ProjectPoint(other.q)
return projP.ApproximatelyEqual(other.p) &&
projQ.ApproximatelyEqual(other.q), nil
}
// ParallelTo checks if two line segments are collinear but
// don't lie on the same line.
func (l *Line) ParallelTo(other *Line) (bool, error) {
if other == nil {
return false, fmt.Errorf("the line is nil")
}
collinear, err := l.CollinearTo(other)
if err != nil {
return false, err
}
sameLine, err := l.SameLineWith(other)
if err != nil {
return false, err
}
return collinear && !sameLine, nil
}
// ProjectPoint returns the projection of the point
// onto the line.
func (l *Line) ProjectPoint(point Vector) Vector {
t := ((point.X-l.p.X)*(l.q.X-l.p.X) + (point.Y-l.p.Y)*(l.q.Y-l.p.Y)) /
((l.q.X-l.p.X)*(l.q.X-l.p.X) + (l.q.Y-l.p.Y)*(l.q.Y-l.p.Y))
return NewVector(l.p.X+t*(l.q.X-l.p.X), l.p.Y+t*(l.q.Y-l.p.Y))
}
// isPointRightOfLine returns true if the given point
// is located to the right of the line, and false otherwise.
func (l *Line) isPointRightOfLine(p Vector) (bool, error) {
lTmp, err := NewLine(Zero(), l.q.Subtract(l.p))
if err != nil {
return false, err
}
pTmp := p.Subtract(l.p)
return Cross(lTmp.q, pTmp) < 0, nil
}
// touchesOrCrosses returns true if the line touches
// or crosses the otehr line, and false otherwise.
func (l *Line) touchesOrCrosses(other *Line) (bool, error) {
if other == nil {
return false, fmt.Errorf("the line is nil")
}
pRight, err := l.isPointRightOfLine(other.p)
if err != nil {
return false, err
}
qRight, err := l.isPointRightOfLine(other.q)
if err != nil {
return false, err
}
return l.ContainsPoint(other.p) ||
l.ContainsPoint(other.q) ||
(pRight != qRight), nil
}
// LinesDistance returns the shortest distance
// between two lines.
func LinesDistance(a, b *Line) (float64, error) {
if a == nil {
return math.NaN(),
fmt.Errorf("the first line is nil")
}
if b == nil {
return math.NaN(),
fmt.Errorf("the second line is nil")
}
apProj := b.ProjectPoint(a.p)
aqProj := b.ProjectPoint(a.q)
bpProj := a.ProjectPoint(b.p)
bqProj := a.ProjectPoint(b.q)
distances := make([]float64, 0)
if !b.ContainsPoint(apProj) {
distances = append(distances,
math.Min(Distance(a.p, b.p), Distance(a.p, b.q)))
}
if !b.ContainsPoint(aqProj) {
distances = append(distances,
math.Min(Distance(a.q, b.p), Distance(a.q, b.q)))
}
if !a.ContainsPoint(bpProj) {
distances = append(distances,
math.Min(Distance(b.p, a.p), Distance(b.p, a.q)))
}
if !a.ContainsPoint(bqProj) {
distances = append(distances,
math.Min(Distance(b.q, a.p), Distance(b.q, a.q)))
}
sort.Slice(distances, func(i, j int) bool {
return distances[i] < distances[j]
})
return distances[0], nil
}
// NewLine returns a new line segment with the given parameters.
func NewLine(p Vector, q Vector) (*Line, error) {
if Distance(p, q) < Epsilon {
return nil, fmt.Errorf(
"the length of the line must be positive")
}
line := &Line{
p: p,
q: q,
}
pq := line.q.Subtract(line.p)
angle, err := Angle(pq, Right())
if err != nil {
return nil, err
}
if angle < 0 {
line.angle = 360 + angle
} else {
line.angle = angle
}
line.treeNodes = []*quadTreeNode{}
return line, nil
}