-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
line.go
198 lines (169 loc) · 5.21 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
package maths
import "math"
type Line [2]Pt
func NewLine(x1, y1, x2, y2 float64) Line {
return Line{
Pt{x1, y1},
Pt{x2, y2},
}
}
func NewLineFloat64(ln [2][2]float64) Line {
return Line{
Pt{ln[0][0], ln[0][1]},
Pt{ln[1][0], ln[1][1]},
}
}
func NewLinesFloat64(ln ...[2][2]float64) (lns []Line) {
for i := range ln {
lns = append(lns, Line{
Pt{ln[i][0][0], ln[i][0][1]},
Pt{ln[i][1][0], ln[i][1][1]},
})
}
return lns
}
// NewLineWith2Float64 is a transistional function till I have had a change to move
// over the geom package and covert the maths functions to use it or they have migrated over
// to it.
// TODO: gdey – remove this function one the transition is over.
func NewLineWith2Float64(ln [2][2]float64) Line {
return Line{
Pt{ln[0][0], ln[0][1]},
Pt{ln[1][0], ln[1][1]},
}
}
// InBetween will check to see if the given point lies on the line provided between the endpoints.
func (l Line) InBetween(pt Pt) bool {
lx, gx := l[0].X, l[1].X
if l[0].X > l[1].X {
lx, gx = l[1].X, l[0].X
}
ly, gy := l[0].Y, l[1].Y
if l[0].Y > l[1].Y {
ly, gy = l[1].Y, l[0].Y
}
return lx <= pt.X && pt.X <= gx && ly <= pt.Y && pt.Y <= gy
}
func (l Line) ExInBetween(pt Pt) bool {
lx, gx := l[0].X, l[1].X
if l[0].X > l[1].X {
lx, gx = l[1].X, l[0].X
}
ly, gy := l[0].Y, l[1].Y
if l[0].Y > l[1].Y {
ly, gy = l[1].Y, l[0].Y
}
goodx, goody := lx < pt.X && pt.X < gx, ly < pt.Y && pt.Y < gy
if gx-lx == 0 {
goodx = true
}
if gy-ly == 0 {
goody = true
}
//log.Println(l, pt, ":", lx, "<", pt.X, "&&", pt.X, "<", gx, "&&", ly, "<", pt.Y, "&&", pt.Y, "<", gy, goodx, goody)
return goodx && goody
}
func (l Line) IsVertical() bool {
return l[0].X == l[1].X
}
func (l Line) IsHorizontal() bool {
return l[0].Y == l[1].Y
}
//Clamp will return a point that is on the line based on pt. It will do this by restricting each of the coordinates to the line.
func (l Line) Clamp(pt Pt) (p Pt) {
p = pt
lx, gx := l[0].X, l[1].X
if l[0].X > l[1].X {
lx, gx = l[1].X, l[0].X
}
ly, gy := l[0].Y, l[1].Y
if l[0].Y > l[1].Y {
ly, gy = l[1].Y, l[0].Y
}
if pt.X < lx {
p.X = lx
}
if pt.X > gx {
p.X = gx
}
if pt.Y < ly {
p.Y = ly
}
if pt.Y > gy {
p.Y = gy
}
return p
}
//InsideEx will return true if the given point is consider to be on the inside of the line, for the given winding order. The inside region excludes the line.
// For clockwise the inside edge for a line heading in the upward direction is any point to the right of the line.
// For clockwise the inside edge for a line heading in the downward direction is any point to the left of the line.
// For clockwise the inside edge for a line heading in the rightward direction is any point to the bottom of the line.
// For clockwise the inside edge for a line heading in the leftward direction is any point above the line.
/*
func (l Line) InsideEx(w WindingOrder, pt Pt) bool {
insideX, insideY := true, true
clockwise = w.IsClockwise()
deltaX := l[1].X - l[0].X
deltaY := l[1].Y - l[0].Y
// Dealing with a horizontal line.
if deltaX == 0 {
ptDelta := l[0].X - pt.X
if clockwise {
}
}
switch {
case w.IsClockwise() && deltaX > 0: // right ward.
goody =
}
}
*/
// DistanceFromPoint will return the perpendicular distance from the point.
func (l Line) DistanceFromPoint(pt Pt) float64 {
deltaX := l[1].X - l[0].X
deltaY := l[1].Y - l[0].Y
//log.Println("delta X/Y : pt - line", deltaX, deltaY, pt, l)
denom := math.Abs((deltaY * pt.X) - (deltaX * pt.Y) + (l[1].X * l[0].Y) - (l[1].Y * l[0].X))
num := math.Sqrt(math.Pow(deltaY, 2) + math.Pow(deltaX, 2))
//log.Println("denim/num", denom, num)
if num == 0 {
return 0
}
return denom / num
}
// SlopeIntercept will find the slop (if there is one) and the intercept of the line. If there isn't a slope because the line is verticle, the defined will be false.
func (l Line) SlopeIntercept() (m, b float64, defined bool) {
dx := l[1].X - l[0].X
dy := l[1].Y - l[0].Y
if dx == 0 || dy == 0 {
// if dx == 0 then m == 0; and the intercept is y.
// However if the lines are verticle then the slope is not defined.
return 0, l[0].Y, dx != 0
}
m = dy / dx
// b = y - mx
b = l[0].Y - (m * l[0].X)
return m, b, true
}
// DeltaX returns the difference between the x coordinates of point2 and point1.
func (l Line) DeltaX() float64 { return l[1].X - l[0].X }
// DeltaY returns the difference between the y coordinates of point2 and point1.
func (l Line) DeltaY() float64 { return l[1].Y - l[0].Y }
// IsLeft tests if point P2 is Left|On|Right of the line P0 to P1.
// returns: >0 for left, 0 for on, and <0 for right of the line.
func (l Line) IsLeft(pt Pt) float64 {
return (l.DeltaX() * (pt.Y - l[0].Y)) - ((pt.X - l[0].X) * l.DeltaY())
}
// LeftRightMostPt returns the left and right most points of the vertexes of the line.
func (l Line) LeftRightMostPts() (Pt, Pt) {
if XYOrder(l[0], l[1]) < 0 {
return l[0], l[1]
}
return l[1], l[0]
}
// LeftRightMostAsLine returns the left most and right most points as a line going from the left to the right.
func (l1 Line) LeftRightMostAsLine() Line {
l, r := l1.LeftRightMostPts()
return Line{l, r}
}
// Ring defines a set of points that are all connected. The last point and the first point should not be duplicated.
type Ring []Pt