-
Notifications
You must be signed in to change notification settings - Fork 105
/
angle.go
171 lines (151 loc) · 4.84 KB
/
angle.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
package xy
import (
"math"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/xy/orientation"
)
const piTimes2 = math.Pi * 2
// Angle calculates the angle of the vector from p0 to p1,
// relative to the positive X-axis.
// The angle is normalized to be in the range [ -Pi, Pi ].
func Angle(p0, p1 geom.Coord) float64 {
dx := p1[0] - p0[0]
dy := p1[1] - p0[1]
return math.Atan2(dy, dx)
}
// AngleFromOrigin calculates the angle that the vector from (0,0) to p,
// relative to the positive X-axis.
// The angle is normalized to be in the range ( -Pi, Pi ].
func AngleFromOrigin(p geom.Coord) float64 {
return math.Atan2(p[1], p[0])
}
// IsAcute tests whether the angle between endpoint1-base-endpoint2 is acute.
// An angle is acute if it is less than 90 degrees.
//
// Note: this implementation is not precise (deterministic) for angles very close to 90 degrees
func IsAcute(endpoint1, base, endpoint2 geom.Coord) bool {
// relies on fact that A dot B is positive iff A ang B is acute
dx0 := endpoint1[0] - base[0]
dy0 := endpoint1[1] - base[1]
dx1 := endpoint2[0] - base[0]
dy1 := endpoint2[1] - base[1]
dotprod := dx0*dx1 + dy0*dy1
return dotprod > 0
}
// IsObtuse tests whether the angle between endpoint1-base-endpoint2 is obtuse.
// An angle is obtuse if it is greater than 90 degrees.
//
// Note: this implementation is not precise (deterministic) for angles very close to 90 degrees
func IsObtuse(endpoint1, base, endpoint2 geom.Coord) bool {
// relies on fact that A dot B is negative iff A ang B is obtuse
dx0 := endpoint1[0] - base[0]
dy0 := endpoint1[1] - base[1]
dx1 := endpoint2[0] - base[0]
dy1 := endpoint2[1] - base[1]
dotprod := dx0*dx1 + dy0*dy1
return dotprod < 0
}
// AngleBetween calculates the un-oriented smallest angle between two vectors.
// The computed angle will be in the range [0, Pi).
//
// Param tip1 - the tip of one vector
// param tail - the tail of each vector
// param tip2 - the tip of the other vector
func AngleBetween(tip1, tail, tip2 geom.Coord) float64 {
a1 := Angle(tail, tip1)
a2 := Angle(tail, tip2)
return Diff(a1, a2)
}
// AngleBetweenOriented calculates the oriented smallest angle between two vectors.
// The computed angle will be in the range (-Pi, Pi].
// A positive result corresponds to a counterclockwise (CCW) rotation from v1 to v2;
// A negative result corresponds to a clockwise (CW) rotation;
// A zero result corresponds to no rotation.
func AngleBetweenOriented(tip1, tail, tip2 geom.Coord) float64 {
a1 := Angle(tail, tip1)
a2 := Angle(tail, tip2)
angDel := a2 - a1
return Normalize(angDel)
}
// InteriorAngle cmputes the interior angle between two segments of a ring. The ring is
// assumed to be oriented in a clockwise direction. The computed angle will be
// in the range [0, 2Pi]
func InteriorAngle(p0, p1, p2 geom.Coord) float64 {
anglePrev := Angle(p1, p0)
angleNext := Angle(p1, p2)
return math.Abs(angleNext - anglePrev)
}
// AngleOrientation returns whether an angle must turn clockwise or counterclockwise
// overlap another angle.
func AngleOrientation(ang1, ang2 float64) orientation.Type {
crossproduct := math.Sin(ang2 - ang1)
switch {
case crossproduct > 0:
return orientation.CounterClockwise
case crossproduct < 0:
return orientation.Clockwise
default:
return orientation.Collinear
}
}
// Normalize computes the normalized value of an angle, which is the
// equivalent angle in the range ( -Pi, Pi ].
func Normalize(angle float64) float64 {
for angle > math.Pi {
angle -= piTimes2
}
for angle <= -math.Pi {
angle += piTimes2
}
return angle
}
// NormalizePositive computes the normalized positive value of an angle, which is the
// equivalent angle in the range [ 0, 2*Pi ).
// E.g.:
// * normalizePositive(0.0) = 0.0
// * normalizePositive(-PI) = PI
// * normalizePositive(-2PI) = 0.0
// * normalizePositive(-3PI) = PI
// * normalizePositive(-4PI) = 0
// * normalizePositive(PI) = PI
// * normalizePositive(2PI) = 0.0
// * normalizePositive(3PI) = PI
// * normalizePositive(4PI) = 0.0
func NormalizePositive(angle float64) float64 {
if angle < 0.0 {
for angle < 0.0 {
angle += piTimes2
}
// in case round-off error bumps the value over
if angle >= piTimes2 {
angle = 0.0
}
} else {
for angle >= piTimes2 {
angle -= piTimes2
}
// in case round-off error bumps the value under
if angle < 0.0 {
angle = 0.0
}
}
return angle
}
// Diff computes the un-oriented smallest difference between two angles.
// The angles are assumed to be normalized to the range [-Pi, Pi].
// The result will be in the range [0, Pi].
//
// Param ang1 - the angle of one vector (in [-Pi, Pi] )
// Param ang2 - the angle of the other vector (in range [-Pi, Pi] )
func Diff(ang1, ang2 float64) float64 {
var delAngle float64
if ang1 < ang2 {
delAngle = ang2 - ang1
} else {
delAngle = ang1 - ang2
}
if delAngle > math.Pi {
delAngle = piTimes2 - delAngle
}
return delAngle
}