Skip to content
Permalink
Browse files

Optimize loop

  • Loading branch information
m-kuhn committed Oct 29, 2017
1 parent 9a57bae commit 81586d207f607ec766bdf88b05d34bde503099b8
Showing with 13 additions and 10 deletions.
  1. +12 −9 src/gui/qgshighlight.cpp
  2. +1 −1 src/gui/qgshighlight.h
@@ -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 );
}
@@ -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;

0 comments on commit 81586d2

Please sign in to comment.
You can’t perform that action at this time.