-
Notifications
You must be signed in to change notification settings - Fork 2
/
segment.go
124 lines (115 loc) · 2.76 KB
/
segment.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
// Copyright 2021 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package geometry
// Segment is a two point line
type Segment struct {
A, B Point
}
// Move a segment by delta
func (seg Segment) Move(deltaX, deltaY float64) Segment {
return Segment{
A: Point{X: seg.A.X + deltaX, Y: seg.A.Y + deltaY},
B: Point{X: seg.B.X + deltaX, Y: seg.B.Y + deltaY},
}
}
// Rect is the outer boundaries of the segment.
func (seg Segment) Rect() Rect {
var rect Rect
rect.Min = seg.A
rect.Max = seg.B
if rect.Min.X > rect.Max.X {
rect.Min.X, rect.Max.X = rect.Max.X, rect.Min.X
}
if rect.Min.Y > rect.Max.Y {
rect.Min.Y, rect.Max.Y = rect.Max.Y, rect.Min.Y
}
return rect
}
func (seg Segment) CollinearPoint(point Point) bool {
cmpx, cmpy := point.X-seg.A.X, point.Y-seg.A.Y
rx, ry := seg.B.X-seg.A.X, seg.B.Y-seg.A.Y
cmpxr := cmpx*ry - cmpy*rx
return cmpxr == 0
}
func (seg Segment) ContainsPoint(point Point) bool {
return seg.Raycast(point).On
}
// IntersectsSegment detects if segment intersects with other segment
func (seg Segment) IntersectsSegment(other Segment) bool {
a, b, c, d := seg.A, seg.B, other.A, other.B
// do the bounding boxes intersect?
if a.Y > b.Y {
if c.Y > d.Y {
if b.Y > c.Y || a.Y < d.Y {
return false
}
} else {
if b.Y > d.Y || a.Y < c.Y {
return false
}
}
} else {
if c.Y > d.Y {
if a.Y > c.Y || b.Y < d.Y {
return false
}
} else {
if a.Y > d.Y || b.Y < c.Y {
return false
}
}
}
if a.X > b.X {
if c.X > d.X {
if b.X > c.X || a.X < d.X {
return false
}
} else {
if b.X > d.X || a.X < c.X {
return false
}
}
} else {
if c.X > d.X {
if a.X > c.X || b.X < d.X {
return false
}
} else {
if a.X > d.X || b.X < c.X {
return false
}
}
}
if seg.A == other.A || seg.A == other.B ||
seg.B == other.A || seg.B == other.B {
return true
}
// the following code is from http://ideone.com/PnPJgb
cmpx, cmpy := c.X-a.X, c.Y-a.Y
rx, ry := b.X-a.X, b.Y-a.Y
cmpxr := cmpx*ry - cmpy*rx
if cmpxr == 0 {
// Lines are collinear, and so intersect if they have any overlap
if !(((c.X-a.X <= 0) != (c.X-b.X <= 0)) ||
((c.Y-a.Y <= 0) != (c.Y-b.Y <= 0))) {
return seg.Raycast(other.A).On || seg.Raycast(other.B).On
//return false
}
return true
}
sx, sy := d.X-c.X, d.Y-c.Y
cmpxs := cmpx*sy - cmpy*sx
rxs := rx*sy - ry*sx
if rxs == 0 {
return false // segments are parallel.
}
rxsr := 1 / rxs
t := cmpxs * rxsr
u := cmpxr * rxsr
return (t >= 0) && (t <= 1) && (u >= 0) && (u <= 1)
}
// ContainsSegment returns true if segment contains other segment
func (seg Segment) ContainsSegment(other Segment) bool {
return seg.Raycast(other.A).On && seg.Raycast(other.B).On
}