@@ -286,15 +286,17 @@ static bool lineInfo( QPointF p1, QPointF p2, double& angle, double& t )
286286 return false ;
287287
288288 // tangent
289- t = ( x1 == x2 ? t = DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );
289+ t = ( x1 == x2 ? DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );
290290
291291 // angle
292292 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
294296 else if ( t >= 0 )
295- angle = ( y2 >= y1 ? atan ( t ) : M_PI + atan ( t ) );
297+ angle = ( y2 > y1 ? atan ( t ) : M_PI + atan ( t ) );
296298 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 ) );
298300
299301 return true ;
300302}
@@ -308,8 +310,8 @@ static QPointF offsetPoint( QPointF pt, double angle, double dist )
308310// calc intersection of two (infinite) lines defined by one point and tangent
309311static QPointF linesIntersection ( QPointF p1, double t1, QPointF p2, double t2 )
310312{
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 )
313315 return QPointF ();
314316
315317 double x, y;
@@ -346,6 +348,7 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )
346348 double angle = 0.0 , t_new, t_old = 0 ;
347349 QPointF pt_old, pt_new;
348350 QPointF p1 = polyline[0 ], p2;
351+ bool first_point = true ;
349352
350353 for ( int i = 1 ; i < polyline.count (); i++ )
351354 {
@@ -356,18 +359,20 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )
356359
357360 pt_new = offsetPoint ( p1, angle + M_PI / 2 , dist );
358361
359- if ( i != 1 )
362+ if ( ! first_point )
360363 {
361364 // if it's not the first line segment
362365 // 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;
364368 }
365369
366370 newLine.append ( pt_new );
367371
368372 pt_old = pt_new;
369373 t_old = t_new;
370374 p1 = p2;
375+ first_point = false ;
371376 }
372377
373378 // last line segment:
0 commit comments