Add left of line test to QgsLineSegment2D

This commit is contained in:
Nyall Dawson 2018-04-13 11:30:12 +10:00
parent 486c3a2d6f
commit 646b4af057
5 changed files with 53 additions and 0 deletions

View File

@ -162,6 +162,19 @@ Sets the segment's ``end`` point.
.. seealso:: :py:func:`setEndY`
.. seealso:: :py:func:`setStart`
%End
int pointLeftOfLine( const QgsPointXY &point ) const;
%Docstring
Tests if a ``point`` is to the left of the line segment.
Returns -1 if the point falls to the left of the line, or +1 if the point
is to the right.
If the return value is 0, then the test was unsuccessful (e.g. due to testing a point exactly
on the line, or exactly in line with the segment) and the result is undefined.
.. seealso:: :py:func:`QgsGeometryUtils.leftOfLine`
%End
bool operator==( const QgsLineSegment2D &other ) const;

View File

@ -463,6 +463,7 @@ SET(QGIS_CORE_SRCS
geometry/qgsgeometryutils.cpp
geometry/qgsgeos.cpp
geometry/qgsinternalgeometryengine.cpp
geometry/qgslinesegment.cpp
geometry/qgslinestring.cpp
geometry/qgsmulticurve.cpp
geometry/qgsmultilinestring.cpp

View File

@ -0,0 +1,16 @@
/***************************************************************************
qgslinesegment.cpp
-----------------
begin : April 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
#include "qgslinesegment.h"
#include "qgsgeometryutils.h"
int QgsLineSegment2D::pointLeftOfLine( const QgsPointXY &point ) const
{
return QgsGeometryUtils::leftOfLine( point.x(), point.y(), mStart.x(), mStart.y(), mEnd.x(), mEnd.y() );
}

View File

@ -176,6 +176,19 @@ class CORE_EXPORT QgsLineSegment2D
mEnd = end;
}
/**
* Tests if a \a point is to the left of the line segment.
*
* Returns -1 if the point falls to the left of the line, or +1 if the point
* is to the right.
*
* If the return value is 0, then the test was unsuccessful (e.g. due to testing a point exactly
* on the line, or exactly in line with the segment) and the result is undefined.
*
* \see QgsGeometryUtils::leftOfLine()
*/
int pointLeftOfLine( const QgsPointXY &point ) const;
//! Equality operator
bool operator==( const QgsLineSegment2D &other ) const
{

View File

@ -102,6 +102,16 @@ class TestQgsLineSegment2D(unittest.TestCase):
self.assertAlmostEqual(segment.length(), 3.60555127546, 5)
self.assertEqual(segment.lengthSquared(), 13)
def testPointLeftOfLine(self):
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 5))
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, 6)), -1)
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, -6)), 1)
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(5, 8)), 0)
segment = QgsLineSegment2D(QgsPointXY(3, 5), QgsPointXY(1, 2))
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, 6)), 1)
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, -6)), -1)
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(5, 8)), 0)
if __name__ == '__main__':
unittest.main()