Skip to content

Commit

Permalink
Add utility function to detect sharp corners
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 24, 2020
1 parent ca5901b commit 0641f3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Expand Up @@ -734,6 +734,13 @@ from the end of the line.

May return an empty linestring if the substring is zero length.

.. versionadded:: 3.16
%End

static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
%Docstring
Returns ``True`` if the angle formed by the line ``p1`` - ``p2`` - ``p3`` forms a "sharp" corner.

.. versionadded:: 3.16
%End

Expand Down
9 changes: 9 additions & 0 deletions src/core/symbology/qgssymbollayerutils.cpp
Expand Up @@ -4156,6 +4156,15 @@ QPolygonF QgsSymbolLayerUtils::polylineSubstring( const QPolygonF &polyline, dou
return substringPoints;
}

bool QgsSymbolLayerUtils::isSharpCorner( QPointF p1, QPointF p2, QPointF p3 )
{
double vertexAngle = M_PI - ( std::atan2( p3.y() - p2.y(), p3.x() - p2.x() ) - std::atan2( p2.y() - p1.y(), p2.x() - p1.x() ) );
vertexAngle = QgsGeometryUtils::normalizedAngle( vertexAngle );

// extreme angles form more than 45 degree angle at a node
return vertexAngle < M_PI * 135.0 / 180.0 || vertexAngle > M_PI * 225.0 / 180.0;
}

QgsExpression *QgsSymbolLayerUtils::fieldOrExpressionToExpression( const QString &fieldOrExpression )
{
if ( fieldOrExpression.isEmpty() )
Expand Down
7 changes: 7 additions & 0 deletions src/core/symbology/qgssymbollayerutils.h
Expand Up @@ -662,6 +662,13 @@ class CORE_EXPORT QgsSymbolLayerUtils
*/
static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );

/**
* Returns TRUE if the angle formed by the line \a p1 - \a p2 - \a p3 forms a "sharp" corner.
*
* \since QGIS 3.16
*/
static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );

/**
* Returns a new valid expression instance for given field or expression string.
* If the input is not a valid expression, it is assumed that it is a field name and gets properly quoted.
Expand Down

0 comments on commit 0641f3f

Please sign in to comment.