Skip to content

Commit

Permalink
fixed several bugs related to creation of offset line in new symbolog…
Browse files Browse the repository at this point in the history
…y (including #2545)

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13166 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Mar 26, 2010
1 parent 6d4dfaf commit 01f8f24
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Expand Up @@ -286,15 +286,17 @@ static bool lineInfo( QPointF p1, QPointF p2, double& angle, double& t )
return false;

// tangent
t = ( x1 == x2 ? t = DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );
t = ( x1 == x2 ? DBL_MAX : ( y2 - y1 ) / ( x2 - x1 ) );

// angle
if ( t == DBL_MAX )
angle = ( y2 >= y1 ? M_PI / 2 : M_PI * 3 / 2 ); // angle is 90 or 270
angle = ( y2 > y1 ? M_PI / 2 : M_PI * 3 / 2 ); // angle is 90 or 270
else if ( t == 0 )
angle = ( x2 > x1 ? 0 : M_PI ); // angle is 0 or 180
else if ( t >= 0 )
angle = ( y2 >= y1 ? atan( t ) : M_PI + atan( t ) );
angle = ( y2 > y1 ? atan( t ) : M_PI + atan( t ) );
else // t < 0
angle = ( y2 >= y1 ? M_PI + atan( t ) : M_PI * 2 + atan( t ) );
angle = ( y2 > y1 ? M_PI + atan( t ) : atan( t ) );

return true;
}
Expand All @@ -308,8 +310,8 @@ static QPointF offsetPoint( QPointF pt, double angle, double dist )
// calc intersection of two (infinite) lines defined by one point and tangent
static QPointF linesIntersection( QPointF p1, double t1, QPointF p2, double t2 )
{
// parallel lines?
if (( t1 == DBL_MAX && t2 == DBL_MAX ) || t1 == t2 )
// parallel lines? (or the difference between angles is less than cca 0.1 degree)
if (( t1 == DBL_MAX && t2 == DBL_MAX ) || fabs( t1 - t2 ) < 0.001 )
return QPointF();

double x, y;
Expand Down Expand Up @@ -346,6 +348,7 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )
double angle = 0.0, t_new, t_old = 0;
QPointF pt_old, pt_new;
QPointF p1 = polyline[0], p2;
bool first_point = true;

for ( int i = 1; i < polyline.count(); i++ )
{
Expand All @@ -356,18 +359,20 @@ QPolygonF offsetLine( QPolygonF polyline, double dist )

pt_new = offsetPoint( p1, angle + M_PI / 2, dist );

if ( i != 1 )
if ( ! first_point )
{
// if it's not the first line segment
// calc intersection with last line (with offset)
pt_new = linesIntersection( pt_old, t_old, pt_new, t_new );
QPointF pt_tmp = linesIntersection( pt_old, t_old, pt_new, t_new );
if ( !pt_tmp.isNull() ) pt_new = pt_tmp;
}

newLine.append( pt_new );

pt_old = pt_new;
t_old = t_new;
p1 = p2;
first_point = false;
}

// last line segment:
Expand Down

0 comments on commit 01f8f24

Please sign in to comment.