/
qgscircle.cpp
306 lines (260 loc) · 8.54 KB
/
qgscircle.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
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
/***************************************************************************
qgscircle.cpp
--------------
begin : March 2017
copyright : (C) 2017 by Loîc Bartoletti
email : lbartoletti at tuxfamily dot org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscircle.h"
#include "qgslinestring.h"
#include "qgsgeometryutils.h"
#include "qgstriangle.h"
#include <memory>
QgsCircle::QgsCircle() :
QgsEllipse( QgsPoint(), 0.0, 0.0, 0.0 )
{
}
QgsCircle::QgsCircle( const QgsPoint ¢er, double radius, double azimuth ) :
QgsEllipse( center, radius, radius, azimuth )
{
}
QgsCircle QgsCircle::from2Points( const QgsPoint &pt1, const QgsPoint &pt2 )
{
QgsPoint center = QgsGeometryUtils::midpoint( pt1, pt2 );
double azimuth = QgsGeometryUtils::lineAngle( pt1.x(), pt1.y(), pt2.x(), pt2.y() ) * 180.0 / M_PI;
double radius = pt1.distance( pt2 ) / 2.0;
return QgsCircle( center, radius, azimuth );
}
static bool isPerpendicular( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon )
{
// check the given point are perpendicular to x or y axis
double yDelta_a = pt2.y() - pt1.y();
double xDelta_a = pt2.x() - pt1.x();
double yDelta_b = pt3.y() - pt2.y();
double xDelta_b = pt3.x() - pt2.x();
if ( ( std::fabs( xDelta_a ) <= epsilon ) && ( std::fabs( yDelta_b ) <= epsilon ) )
{
return false;
}
if ( std::fabs( yDelta_a ) <= epsilon )
{
return true;
}
else if ( std::fabs( yDelta_b ) <= epsilon )
{
return true;
}
else if ( std::fabs( xDelta_a ) <= epsilon )
{
return true;
}
else if ( std::fabs( xDelta_b ) <= epsilon )
{
return true;
}
return false;
}
QgsCircle QgsCircle::from3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon )
{
QgsPoint p1, p2, p3;
if ( !isPerpendicular( pt1, pt2, pt3, epsilon ) )
{
p1 = pt1;
p2 = pt2;
p3 = pt3;
}
else if ( !isPerpendicular( pt1, pt3, pt2, epsilon ) )
{
p1 = pt1;
p2 = pt3;
p3 = pt2;
}
else if ( !isPerpendicular( pt2, pt1, pt3, epsilon ) )
{
p1 = pt2;
p2 = pt1;
p3 = pt3;
}
else if ( !isPerpendicular( pt2, pt3, pt1, epsilon ) )
{
p1 = pt2;
p2 = pt3;
p3 = pt1;
}
else if ( !isPerpendicular( pt3, pt2, pt1, epsilon ) )
{
p1 = pt3;
p2 = pt2;
p3 = pt1;
}
else if ( !isPerpendicular( pt3, pt1, pt2, epsilon ) )
{
p1 = pt3;
p2 = pt1;
p3 = pt2;
}
else
{
return QgsCircle();
}
QgsPoint center = QgsPoint();
double radius = -0.0;
// Paul Bourke's algorithm
double yDelta_a = p2.y() - p1.y();
double xDelta_a = p2.x() - p1.x();
double yDelta_b = p3.y() - p2.y();
double xDelta_b = p3.x() - p2.x();
if ( qgsDoubleNear( xDelta_a, 0.0, epsilon ) || qgsDoubleNear( xDelta_b, 0.0, epsilon ) )
{
return QgsCircle();
}
double aSlope = yDelta_a / xDelta_a;
double bSlope = yDelta_b / xDelta_b;
if ( ( std::fabs( xDelta_a ) <= epsilon ) && ( std::fabs( yDelta_b ) <= epsilon ) )
{
center.setX( 0.5 * ( p2.x() + p3.x() ) );
center.setY( 0.5 * ( p1.y() + p2.y() ) );
radius = center.distance( pt1 );
return QgsCircle( center, radius );
}
if ( std::fabs( aSlope - bSlope ) <= epsilon )
{
return QgsCircle();
}
center.setX(
( aSlope * bSlope * ( p1.y() - p3.y() ) +
bSlope * ( p1.x() + p2.x() ) -
aSlope * ( p2.x() + p3.x() ) ) /
( 2.0 * ( bSlope - aSlope ) )
);
center.setY(
-1.0 * ( center.x() - ( p1.x() + p2.x() ) / 2.0 ) /
aSlope + ( p1.y() + p2.y() ) / 2.0
);
radius = center.distance( p1 );
return QgsCircle( center, radius );
}
QgsCircle QgsCircle::fromCenterDiameter( const QgsPoint ¢er, double diameter, double azimuth )
{
return QgsCircle( center, diameter / 2.0, azimuth );
}
QgsCircle QgsCircle::fromCenterPoint( const QgsPoint ¢er, const QgsPoint &pt1 )
{
double azimuth = QgsGeometryUtils::lineAngle( center.x(), center.y(), pt1.x(), pt1.y() ) * 180.0 / M_PI;
return QgsCircle( center, center.distance( pt1 ), azimuth );
}
QgsCircle QgsCircle::from3Tangents( const QgsPoint &pt1_tg1, const QgsPoint &pt2_tg1, const QgsPoint &pt1_tg2, const QgsPoint &pt2_tg2, const QgsPoint &pt1_tg3, const QgsPoint &pt2_tg3, double epsilon )
{
QgsPoint p1, p2, p3;
bool isIntersect = false;
QgsGeometryUtils::segmentIntersection( pt1_tg1, pt2_tg1, pt1_tg2, pt2_tg2, p1, isIntersect, epsilon );
if ( !isIntersect )
return QgsCircle();
QgsGeometryUtils::segmentIntersection( pt1_tg1, pt2_tg1, pt1_tg3, pt2_tg3, p2, isIntersect, epsilon );
if ( !isIntersect )
return QgsCircle();
QgsGeometryUtils::segmentIntersection( pt1_tg2, pt2_tg2, pt1_tg3, pt2_tg3, p3, isIntersect, epsilon );
if ( !isIntersect )
return QgsCircle();
return QgsTriangle( p1, p2, p3 ).inscribedCircle();
}
QgsCircle QgsCircle::minimalCircleFrom3Points( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double epsilon )
{
double l1 = pt2.distance( pt3 );
double l2 = pt3.distance( pt1 );
double l3 = pt1.distance( pt2 );
if ( ( l1 * l1 ) - ( l2 * l2 + l3 * l3 ) >= epsilon )
return QgsCircle().from2Points( pt2, pt3 );
else if ( ( l2 * l2 ) - ( l1 * l1 + l3 * l3 ) >= epsilon )
return QgsCircle().from2Points( pt3, pt1 );
else if ( ( l3 * l3 ) - ( l1 * l1 + l2 * l2 ) >= epsilon )
return QgsCircle().from2Points( pt1, pt2 );
else
return QgsCircle().from3Points( pt1, pt2, pt3, epsilon );
}
QgsCircle QgsCircle::fromExtent( const QgsPoint &pt1, const QgsPoint &pt2 )
{
double delta_x = std::fabs( pt1.x() - pt2.x() );
double delta_y = std::fabs( pt1.x() - pt2.y() );
if ( !qgsDoubleNear( delta_x, delta_y ) )
{
return QgsCircle();
}
return QgsCircle( QgsGeometryUtils::midpoint( pt1, pt2 ), delta_x / 2.0, 0 );
}
double QgsCircle::area() const
{
return M_PI * mSemiMajorAxis * mSemiMajorAxis;
}
double QgsCircle::perimeter() const
{
return 2.0 * M_PI * mSemiMajorAxis;
}
void QgsCircle::setSemiMajorAxis( const double semiMajorAxis )
{
mSemiMajorAxis = std::fabs( semiMajorAxis );
mSemiMinorAxis = mSemiMajorAxis;
}
void QgsCircle::setSemiMinorAxis( const double semiMinorAxis )
{
mSemiMajorAxis = std::fabs( semiMinorAxis );
mSemiMinorAxis = mSemiMajorAxis;
}
QVector<QgsPoint> QgsCircle::northQuadrant() const
{
QVector<QgsPoint> quad;
quad.append( QgsPoint( mCenter.x(), mCenter.y() + mSemiMajorAxis ) );
quad.append( QgsPoint( mCenter.x() + mSemiMajorAxis, mCenter.y() ) );
quad.append( QgsPoint( mCenter.x(), mCenter.y() - mSemiMajorAxis ) );
quad.append( QgsPoint( mCenter.x() - mSemiMajorAxis, mCenter.y() ) );
return quad;
}
QgsCircularString *QgsCircle::toCircularString( bool oriented ) const
{
std::unique_ptr<QgsCircularString> circString( new QgsCircularString() );
QgsPointSequence points;
QVector<QgsPoint> quad;
if ( oriented )
{
quad = quadrant();
}
else
{
quad = northQuadrant();
}
quad.append( quad.at( 0 ) );
for ( QVector<QgsPoint>::const_iterator it = quad.constBegin(); it != quad.constEnd(); ++it )
{
points.append( *it );
}
circString->setPoints( points );
return circString.release();
}
bool QgsCircle::contains( const QgsPoint &point, double epsilon ) const
{
return ( mCenter.distance( point ) <= mSemiMajorAxis + epsilon );
}
QgsRectangle QgsCircle::boundingBox() const
{
return QgsRectangle( mCenter.x() - mSemiMajorAxis, mCenter.y() - mSemiMajorAxis, mCenter.x() + mSemiMajorAxis, mCenter.y() + mSemiMajorAxis );
}
QString QgsCircle::toString( int pointPrecision, int radiusPrecision, int azimuthPrecision ) const
{
QString rep;
if ( isEmpty() )
rep = QStringLiteral( "Empty" );
else
rep = QStringLiteral( "Circle (Center: %1, Radius: %2, Azimuth: %3)" )
.arg( mCenter.asWkt( pointPrecision ), 0, 's' )
.arg( qgsDoubleToString( mSemiMajorAxis, radiusPrecision ), 0, 'f' )
.arg( qgsDoubleToString( mAzimuth, azimuthPrecision ), 0, 'f' );
return rep;
}