25
25
#include " qgslinestring.h"
26
26
#include " qgsmultipolygon.h"
27
27
#include " qgsspinbox.h"
28
+ #include " qgsgeometryutils.h"
28
29
#include < memory>
29
30
#include < QMouseEvent>
30
31
@@ -61,9 +62,12 @@ void QgsMapToolCircle2TangentsPoint::cadCanvasReleaseEvent( QgsMapMouseEvent *e
61
62
}
62
63
if ( mPoints .size () == 4 )
63
64
{
64
- QgsPointXY ptInter = intersect ( QgsPointXY ( mPoints .at ( 0 ) ), QgsPointXY ( mPoints .at ( 1 ) ),
65
- QgsPointXY ( mPoints .at ( 2 ) ), QgsPointXY ( mPoints .at ( 3 ) ) );
66
- if ( ptInter == QgsPointXY () )
65
+ bool isIntersect;
66
+ const double epsilon = 1e-8 ;
67
+ QgsPoint ptInter;
68
+ QgsGeometryUtils::segmentIntersection ( mPoints .at ( 0 ), mPoints .at ( 1 ),
69
+ mPoints .at ( 2 ), mPoints .at ( 3 ), ptInter, isIntersect, epsilon );
70
+ if ( !isIntersect )
67
71
{
68
72
QgisApp::instance ()->messageBar ()->pushMessage ( tr ( " Error" ), tr ( " Segments are parallels" ),
69
73
QgsMessageBar::CRITICAL, QgisApp::instance ()->messageTimeout () );
@@ -144,64 +148,6 @@ void QgsMapToolCircle2TangentsPoint::cadCanvasMoveEvent( QgsMapMouseEvent *e )
144
148
}
145
149
}
146
150
147
- QgsPointXY QgsMapToolCircle2TangentsPoint::intersect ( QgsPointXY seg1_pt1, QgsPointXY seg1_pt2, QgsPointXY seg2_pt1, QgsPointXY seg2_pt2 )
148
- {
149
- /*
150
- * Public domain function by Darel Rex Finley, 2006
151
- * http://alienryderflex.com/intersect/
152
- */
153
- QgsPointXY ptInter;
154
-
155
- double Ax = seg1_pt1.x ();
156
- double Ay = seg1_pt1.y ();
157
- double Bx = seg1_pt2.x ();
158
- double By = seg1_pt2.y ();
159
-
160
- double Cx = seg2_pt1.x ();
161
- double Cy = seg2_pt1.y ();
162
- double Dx = seg2_pt2.x ();
163
- double Dy = seg2_pt2.y ();
164
-
165
- if ( ( ( Ax == Bx ) && ( Ay == By ) ) || ( ( Cx == Dx ) && ( Cy == Dy ) ) )
166
- return ptInter;
167
-
168
- // (1) Translate the system so that point A is on the origin.
169
- Bx -= Ax;
170
- By -= Ay;
171
- Cx -= Ax;
172
- Cy -= Ay;
173
- Dx -= Ax;
174
- Dy -= Ay;
175
-
176
- // Discover the length of segment A-B
177
- double distAB = sqrt ( Bx * Bx + By * By );
178
-
179
- // (2) Rotate the system so that point B is on the positive X axis.
180
- double theCos = Bx / distAB;
181
- double theSin = By / distAB;
182
- double newX = Cx * theCos + Cy * theSin;
183
- Cy = Cy * theCos - Cx * theSin;
184
- Cx = newX;
185
- newX = Dx * theCos + Dy * theSin;
186
- Dy = Dy * theCos - Dx * theSin;
187
- Dx = newX;
188
-
189
- // Fail if the lines are parallel.
190
- if ( Cy == Dy )
191
- return ptInter;
192
-
193
- // (3) Discover the position of the intersection point along line A-B.
194
- double ABpos = Dx + ( Cx - Dx ) * Dy / ( Dy - Cy );
195
-
196
- // (4) Apply the discovered position to line A-B
197
- // in the original coordinate system.
198
- ptInter.setX ( Ax + ABpos * theCos );
199
- ptInter.setY ( Ay + ABpos * theSin );
200
-
201
- // Success
202
- return ptInter;
203
- }
204
-
205
151
void QgsMapToolCircle2TangentsPoint::getPossibleCenter ( )
206
152
{
207
153
@@ -226,20 +172,21 @@ void QgsMapToolCircle2TangentsPoint::getPossibleCenter( )
226
172
QgsGeometry line2m = line2.offsetCurve ( - mRadius , 8 , QgsGeometry::JoinStyleBevel, 5 );
227
173
QgsGeometry line2p = line2.offsetCurve ( + mRadius , 8 , QgsGeometry::JoinStyleBevel, 5 );
228
174
229
- QgsPointXY p1 = intersect ( line1m.asPolyline ().at ( 0 ), line1m.asPolyline ().at ( 1 ),
230
- line2m.asPolyline ().at ( 0 ), line2m.asPolyline ().at ( 1 ) );
231
- QgsPointXY p2 = intersect ( line1m.asPolyline ().at ( 0 ), line1m.asPolyline ().at ( 1 ),
232
- line2p.asPolyline ().at ( 0 ), line2p.asPolyline ().at ( 1 ) );
233
- QgsPointXY p3 = intersect ( line1p.asPolyline ().at ( 0 ), line1p.asPolyline ().at ( 1 ),
234
- line2m.asPolyline ().at ( 0 ), line2m.asPolyline ().at ( 1 ) );
235
- QgsPointXY p4 = intersect ( line1p.asPolyline ().at ( 0 ), line1p.asPolyline ().at ( 1 ),
236
- line2p.asPolyline ().at ( 0 ), line2p.asPolyline ().at ( 1 ) );
237
-
238
- mCenters .append ( p1 );
239
- mCenters .append ( p2 );
240
- mCenters .append ( p3 );
241
- mCenters .append ( p4 );
242
-
175
+ bool isIntersect;
176
+ const double epsilon = 1e-8 ;
177
+ QgsPoint inter;
178
+ QgsGeometryUtils::segmentIntersection ( QgsPoint ( line1m.asPolyline ().at ( 0 ) ), QgsPoint ( line1m.asPolyline ().at ( 1 ) ),
179
+ QgsPoint ( line2m.asPolyline ().at ( 0 ) ), QgsPoint ( line2m.asPolyline ().at ( 1 ) ), inter, isIntersect, epsilon );
180
+ mCenters .append ( QgsPointXY ( inter ) );
181
+ QgsGeometryUtils::segmentIntersection ( QgsPoint ( line1m.asPolyline ().at ( 0 ) ), QgsPoint ( line1m.asPolyline ().at ( 1 ) ),
182
+ QgsPoint ( line2p.asPolyline ().at ( 0 ) ), QgsPoint ( line2p.asPolyline ().at ( 1 ) ), inter, isIntersect, epsilon );
183
+ mCenters .append ( QgsPointXY ( inter ) );
184
+ QgsGeometryUtils::segmentIntersection ( QgsPoint ( line1p.asPolyline ().at ( 0 ) ), QgsPoint ( line1p.asPolyline ().at ( 1 ) ),
185
+ QgsPoint ( line2m.asPolyline ().at ( 0 ) ), QgsPoint ( line2m.asPolyline ().at ( 1 ) ), inter, isIntersect, epsilon );
186
+ mCenters .append ( QgsPointXY ( inter ) );
187
+ QgsGeometryUtils::segmentIntersection ( QgsPoint ( line1p.asPolyline ().at ( 0 ) ), QgsPoint ( line1p.asPolyline ().at ( 1 ) ),
188
+ QgsPoint ( line2p.asPolyline ().at ( 0 ) ), QgsPoint ( line2p.asPolyline ().at ( 1 ) ), inter, isIntersect, epsilon );
189
+ mCenters .append ( QgsPointXY ( inter ) );
243
190
}
244
191
}
245
192
0 commit comments