Skip to content

Commit

Permalink
Optimize loop
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 30, 2017
1 parent 9a57bae commit 81586d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/gui/qgshighlight.cpp
Expand Up @@ -221,32 +221,35 @@ void QgsHighlight::paintLine( QPainter *p, QgsPolylineXY line )
p->drawPolyline( polygon );
}

void QgsHighlight::paintPolygon( QPainter *p, QgsPolygonXY polygon )
void QgsHighlight::paintPolygon( QPainter *p, const QgsPolygonXY &polygon )
{
// OddEven fill rule by default
QPainterPath path;

p->setPen( mPen );
p->setBrush( mBrush );

for ( int i = 0; i < polygon.size(); i++ )
for ( const auto &sourceRing : polygon )
{
if ( polygon[i].empty() ) continue;
if ( sourceRing.empty() )
continue;

QPolygonF ring;
ring.reserve( polygon[i].size() + 1 );
ring.reserve( sourceRing.size() + 1 );

for ( int j = 0; j < polygon[i].size(); j++ )
QPointF lastVertex;
for ( const auto &sourceVertex : sourceRing )
{
//adding point only if it is more than a pixel apart from the previous one
const QPointF cur = toCanvasCoordinates( polygon[i][j] ) - pos();
if ( 0 == j || std::abs( ring.back().x() - cur.x() ) > 1 || std::abs( ring.back().y() - cur.y() ) > 1 )
const QPointF curVertex = toCanvasCoordinates( sourceVertex ) - pos();
if ( std::abs( ring.back().x() - curVertex.x() ) > 1 || std::abs( ring.back().y() - curVertex.y() ) > 1 )
{
ring.push_back( cur );
ring.push_back( curVertex );
}
lastVertex = curVertex;
}

ring.push_back( ring[ 0 ] );
ring.push_back( ring.at( 0 ) );

path.addPolygon( ring );
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgshighlight.h
Expand Up @@ -109,7 +109,7 @@ class GUI_EXPORT QgsHighlight: public QgsMapCanvasItem
QgsFeatureRenderer *getRenderer( QgsRenderContext &context, const QColor &color, const QColor &fillColor );
void paintPoint( QPainter *p, const QgsPointXY &point );
void paintLine( QPainter *p, QgsPolylineXY line );
void paintPolygon( QPainter *p, QgsPolygonXY polygon );
void paintPolygon( QPainter *p, const QgsPolygonXY &polygon );

QBrush mBrush;
QPen mPen;
Expand Down

0 comments on commit 81586d2

Please sign in to comment.