Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add reverse method to QgsLineSegment2D
  • Loading branch information
nyalldawson committed Apr 23, 2018
1 parent 646b4af commit 8f1d1a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/core/geometry/qgslinesegment.sip.in
Expand Up @@ -175,6 +175,11 @@ If the return value is 0, then the test was unsuccessful (e.g. due to testing a
on the line, or exactly in line with the segment) and the result is undefined.

.. seealso:: :py:func:`QgsGeometryUtils.leftOfLine`
%End

void reverse();
%Docstring
Reverses the line segment, so that the start and end points are flipped.
%End

bool operator==( const QgsLineSegment2D &other ) const;
Expand Down
8 changes: 8 additions & 0 deletions src/core/geometry/qgslinesegment.h
Expand Up @@ -189,6 +189,14 @@ class CORE_EXPORT QgsLineSegment2D
*/
int pointLeftOfLine( const QgsPointXY &point ) const;

/**
* Reverses the line segment, so that the start and end points are flipped.
*/
void reverse()
{
std::swap( mStart, mEnd );
}

//! Equality operator
bool operator==( const QgsLineSegment2D &other ) const
{
Expand Down
6 changes: 6 additions & 0 deletions tests/src/python/test_qgslinesegment.py
Expand Up @@ -112,6 +112,12 @@ def testPointLeftOfLine(self):
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, -6)), -1)
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(5, 8)), 0)

def testReverse(self):
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))
segment.reverse()
self.assertEqual(segment.start(), QgsPointXY(3, 4))
self.assertEqual(segment.end(), QgsPointXY(1, 2))


if __name__ == '__main__':
unittest.main()

0 comments on commit 8f1d1a3

Please sign in to comment.