@@ -286,15 +286,17 @@ static bool lineInfo( QPointF p1, QPointF p2, double& angle, double& t )
286
286
return false ;
287
287
288
288
// tangent
289
- t = ( x1 == x2 ? t = DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );
289
+ t = ( x1 == x2 ? DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );
290
290
291
291
// angle
292
292
if ( t == DBL_MAX )
293
- angle = ( y2 >= y1 ? M_PI / 2 : M_PI * 3 / 2 ); // angle is 90 or 270
293
+ angle = ( y2 > y1 ? M_PI / 2 : M_PI * 3 / 2 ); // angle is 90 or 270
294
+ else if ( t == 0 )
295
+ angle = ( x2 > x1 ? 0 : M_PI ); // angle is 0 or 180
294
296
else if ( t >= 0 )
295
- angle = ( y2 >= y1 ? atan ( t ) : M_PI + atan ( t ) );
297
+ angle = ( y2 > y1 ? atan ( t ) : M_PI + atan ( t ) );
296
298
else // t < 0
297
- angle = ( y2 >= y1 ? M_PI + atan ( t ) : M_PI * 2 + atan ( t ) );
299
+ angle = ( y2 > y1 ? M_PI + atan ( t ) : atan ( t ) );
298
300
299
301
return true ;
300
302
}
@@ -308,8 +310,8 @@ static QPointF offsetPoint( QPointF pt, double angle, double dist )
308
310
// calc intersection of two (infinite) lines defined by one point and tangent
309
311
static QPointF linesIntersection ( QPointF p1, double t1, QPointF p2, double t2 )
310
312
{
311
- // parallel lines?
312
- if (( t1 == DBL_MAX && t2 == DBL_MAX ) || t1 == t2 )
313
+ // parallel lines? (or the difference between angles is less than cca 0.1 degree)
314
+ if (( t1 == DBL_MAX && t2 == DBL_MAX ) || fabs ( t1 - t2 ) < 0.001 )
313
315
return QPointF ();
314
316
315
317
double x, y;
@@ -346,6 +348,7 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )
346
348
double angle = 0.0 , t_new, t_old = 0 ;
347
349
QPointF pt_old, pt_new;
348
350
QPointF p1 = polyline[0 ], p2;
351
+ bool first_point = true ;
349
352
350
353
for ( int i = 1 ; i < polyline.count (); i++ )
351
354
{
@@ -356,18 +359,20 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )
356
359
357
360
pt_new = offsetPoint ( p1, angle + M_PI / 2 , dist );
358
361
359
- if ( i != 1 )
362
+ if ( ! first_point )
360
363
{
361
364
// if it's not the first line segment
362
365
// calc intersection with last line (with offset)
363
- pt_new = linesIntersection ( pt_old, t_old, pt_new, t_new );
366
+ QPointF pt_tmp = linesIntersection ( pt_old, t_old, pt_new, t_new );
367
+ if ( !pt_tmp.isNull () ) pt_new = pt_tmp;
364
368
}
365
369
366
370
newLine.append ( pt_new );
367
371
368
372
pt_old = pt_new;
369
373
t_old = t_new;
370
374
p1 = p2;
375
+ first_point = false ;
371
376
}
372
377
373
378
// last line segment:
0 commit comments