Skip to content

Commit

Permalink
Add QgsClipper::clippedLine variant which accepts a QPolygonF line
Browse files Browse the repository at this point in the history
input
  • Loading branch information
nyalldawson committed Sep 21, 2020
1 parent 1c1febc commit 9990b84
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsclipper.sip.in
Expand Up @@ -75,6 +75,13 @@ Takes a linestring and clips it to clipExtent
:param clipExtent: clipping bounds

:return: clipped line coordinates
%End

static QPolygonF clippedLine( const QPolygonF &curve, const QgsRectangle &clipExtent );
%Docstring
Takes a ``curve`` and clips it to clipExtent.

.. versionadded:: 3.16
%End

};
Expand Down
19 changes: 13 additions & 6 deletions src/core/qgsclipper.cpp
Expand Up @@ -40,7 +40,12 @@ const double QgsClipper::SMALL_NUM = 1e-12;

QPolygonF QgsClipper::clippedLine( const QgsCurve &curve, const QgsRectangle &clipExtent )
{
const int nPoints = curve.numPoints();
return clippedLine( curve.asQPolygonF(), clipExtent );
}

QPolygonF QgsClipper::clippedLine( const QPolygonF &curve, const QgsRectangle &clipExtent )
{
const int nPoints = curve.size();

double p0x, p0y, p1x = 0.0, p1y = 0.0; //original coordinates
double p1x_c, p1y_c; //clipped end coordinates
Expand All @@ -49,21 +54,22 @@ QPolygonF QgsClipper::clippedLine( const QgsCurve &curve, const QgsRectangle &cl
QPolygonF line;
line.reserve( nPoints + 1 );

const QPointF *curveData = curve.data();

for ( int i = 0; i < nPoints; ++i )
{
if ( i == 0 )
{
p1x = curve.xAt( i );
p1y = curve.yAt( i );
continue;
p1x = curveData->x();
p1y = curveData->y();
}
else
{
p0x = p1x;
p0y = p1y;

p1x = curve.xAt( i );
p1y = curve.yAt( i );
p1x = curveData->x();
p1y = curveData->y();

p1x_c = p1x;
p1y_c = p1y;
Expand All @@ -88,6 +94,7 @@ QPolygonF QgsClipper::clippedLine( const QgsCurve &curve, const QgsRectangle &cl
line << QPointF( p1x_c, p1y_c );
}
}
curveData++;
}
return line;
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsclipper.h
Expand Up @@ -108,6 +108,13 @@ class CORE_EXPORT QgsClipper
*/
static QPolygonF clippedLine( const QgsCurve &curve, const QgsRectangle &clipExtent );

/**
* Takes a \a curve and clips it to clipExtent.
*
* \since QGIS 3.16
*/
static QPolygonF clippedLine( const QPolygonF &curve, const QgsRectangle &clipExtent );

private:

// Used when testing for equivalence to 0.0
Expand Down

0 comments on commit 9990b84

Please sign in to comment.