mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
Add left of line test to QgsLineSegment2D
This commit is contained in:
parent
486c3a2d6f
commit
646b4af057
@ -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
|
||||
|
16
src/core/geometry/qgslinesegment.cpp
Normal file
16
src/core/geometry/qgslinesegment.cpp
Normal 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() );
|
||||
}
|
||||
|
@ -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 @@ 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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user