/
Angle.cpp
206 lines (181 loc) · 4.47 KB
/
Angle.cpp
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
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2009-2011 Sandro Santilli <strk@kbt.io>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************
*
* Last port: algorithm/Angle.java r378 (JTS-1.12)
*
**********************************************************************/
#include <cmath>
#include <geos/algorithm/Angle.h>
#include <geos/geom/Coordinate.h>
namespace geos {
namespace algorithm { // geos.algorithm
/* public static */
double
Angle::toDegrees(double radians)
{
return (radians * 180) / (MATH_PI);
}
/* public static */
double
Angle::toRadians(double angleDegrees)
{
return (angleDegrees * MATH_PI) / 180.0;
}
/* public static */
double
Angle::angle(const geom::CoordinateXY& p0,
const geom::CoordinateXY& p1)
{
double dx = p1.x - p0.x;
double dy = p1.y - p0.y;
return atan2(dy, dx);
}
/* public static */
double
Angle::angle(const geom::CoordinateXY& p)
{
return atan2(p.y, p.x);
}
/* public static */
bool
Angle::isAcute(const geom::CoordinateXY& p0,
const geom::CoordinateXY& p1,
const geom::CoordinateXY& p2)
{
// relies on fact that A dot B is positive iff A ang B is acute
double dx0 = p0.x - p1.x;
double dy0 = p0.y - p1.y;
double dx1 = p2.x - p1.x;
double dy1 = p2.y - p1.y;
double dotprod = dx0 * dx1 + dy0 * dy1;
return dotprod > 0;
}
/* public static */
bool
Angle::isObtuse(const geom::CoordinateXY& p0,
const geom::CoordinateXY& p1,
const geom::CoordinateXY& p2)
{
// relies on fact that A dot B is negative iff A ang B is obtuse
double dx0 = p0.x - p1.x;
double dy0 = p0.y - p1.y;
double dx1 = p2.x - p1.x;
double dy1 = p2.y - p1.y;
double dotprod = dx0 * dx1 + dy0 * dy1;
return dotprod < 0;
}
/* public static */
double
Angle::angleBetween(const geom::CoordinateXY& tip1,
const geom::CoordinateXY& tail,
const geom::CoordinateXY& tip2)
{
double a1 = angle(tail, tip1);
double a2 = angle(tail, tip2);
return diff(a1, a2);
}
/* public static */
double
Angle::angleBetweenOriented(const geom::CoordinateXY& tip1,
const geom::CoordinateXY& tail,
const geom::CoordinateXY& tip2)
{
double a1 = angle(tail, tip1);
double a2 = angle(tail, tip2);
double angDel = a2 - a1;
// normalize, maintaining orientation
if(angDel <= -MATH_PI) {
return angDel + PI_TIMES_2;
}
if(angDel > MATH_PI) {
return angDel - PI_TIMES_2;
}
return angDel;
}
/* public static */
double
Angle::interiorAngle(const geom::CoordinateXY& p0, const geom::CoordinateXY& p1,
const geom::CoordinateXY& p2)
{
double anglePrev = angle(p1, p0);
double angleNext = angle(p1, p2);
return normalizePositive(angleNext - anglePrev);
}
/* public static */
int
Angle::getTurn(double ang1, double ang2)
{
double crossproduct = sin(ang2 - ang1);
if(crossproduct > 0) {
return COUNTERCLOCKWISE;
}
if(crossproduct < 0) {
return CLOCKWISE;
}
return NONE;
}
/* public static */
double
Angle::normalize(double angle)
{
while(angle > MATH_PI) {
angle -= PI_TIMES_2;
}
while(angle <= -MATH_PI) {
angle += PI_TIMES_2;
}
return angle;
}
/* public static */
double
Angle::normalizePositive(double angle)
{
if(angle < 0.0) {
while(angle < 0.0) {
angle += PI_TIMES_2;
}
// in case round-off error bumps the value over
if(angle >= PI_TIMES_2) {
angle = 0.0;
}
}
else {
while(angle >= PI_TIMES_2) {
angle -= PI_TIMES_2;
}
// in case round-off error bumps the value under
if(angle < 0.0) {
angle = 0.0;
}
}
return angle;
}
/* public static */
double
Angle::diff(double ang1, double ang2)
{
double delAngle;
if(ang1 < ang2) {
delAngle = ang2 - ang1;
}
else {
delAngle = ang1 - ang2;
}
if(delAngle > MATH_PI) {
delAngle = PI_TIMES_2 - delAngle;
}
return delAngle;
}
} // namespace geos.algorithm
} //namespace geos