Skip to content
Permalink
Browse files

Add left of line test to QgsLineSegment2D

  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent 486c3a2 commit 646b4af057a2a9aba276f1fd7c53bdba1303cec7
@@ -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;
@@ -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
@@ -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() );
}

@@ -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
{
@@ -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.
You can’t perform that action at this time.