Skip to content

Commit

Permalink
Add left of line test to QgsLineSegment2D
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent 486c3a2 commit 646b4af
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/core/geometry/qgslinesegment.sip.in
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/core/geometry/qgslinesegment.cpp
@@ -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() );
}

13 changes: 13 additions & 0 deletions src/core/geometry/qgslinesegment.h
Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_qgslinesegment.py
Expand Up @@ -102,6 +102,16 @@ def testLength(self):
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()

0 comments on commit 646b4af

Please sign in to comment.