Skip to content

Commit 8f1d1a3

Browse files
committedApr 23, 2018
Add reverse method to QgsLineSegment2D
1 parent 646b4af commit 8f1d1a3

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
 

‎python/core/geometry/qgslinesegment.sip.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ If the return value is 0, then the test was unsuccessful (e.g. due to testing a
175175
on the line, or exactly in line with the segment) and the result is undefined.
176176

177177
.. seealso:: :py:func:`QgsGeometryUtils.leftOfLine`
178+
%End
179+
180+
void reverse();
181+
%Docstring
182+
Reverses the line segment, so that the start and end points are flipped.
178183
%End
179184

180185
bool operator==( const QgsLineSegment2D &other ) const;

‎src/core/geometry/qgslinesegment.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ class CORE_EXPORT QgsLineSegment2D
189189
*/
190190
int pointLeftOfLine( const QgsPointXY &point ) const;
191191

192+
/**
193+
* Reverses the line segment, so that the start and end points are flipped.
194+
*/
195+
void reverse()
196+
{
197+
std::swap( mStart, mEnd );
198+
}
199+
192200
//! Equality operator
193201
bool operator==( const QgsLineSegment2D &other ) const
194202
{

‎tests/src/python/test_qgslinesegment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def testPointLeftOfLine(self):
112112
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(1.5, -6)), -1)
113113
self.assertEqual(segment.pointLeftOfLine(QgsPointXY(5, 8)), 0)
114114

115+
def testReverse(self):
116+
segment = QgsLineSegment2D(QgsPointXY(1, 2), QgsPointXY(3, 4))
117+
segment.reverse()
118+
self.assertEqual(segment.start(), QgsPointXY(3, 4))
119+
self.assertEqual(segment.end(), QgsPointXY(1, 2))
120+
115121

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

0 commit comments

Comments
 (0)
Please sign in to comment.