Add reverse method to QgsLineSegment2D

This commit is contained in:
Nyall Dawson 2018-04-13 12:09:03 +10:00
parent 646b4af057
commit 8f1d1a31c9
3 changed files with 19 additions and 0 deletions

View File

@ -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;

View File

@ -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
{

View File

@ -112,6 +112,12 @@ class TestQgsLineSegment2D(unittest.TestCase):
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()