Skip to content

Commit 983fe24

Browse files
committedNov 7, 2016
Port some API from QgsPoint to QgsPointV2
1 parent dae0a01 commit 983fe24

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
 

‎python/core/geometry/qgspointv2.sip

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,47 @@ class QgsPointV2: public QgsAbstractGeometry
138138
*/
139139
QPointF toQPointF() const;
140140

141+
/**
142+
* Returns the distance between this point and a specified x, y coordinate. In certain
143+
* cases it may be more appropriate to call the faster distanceSquared() method, eg
144+
* when comparing distances.
145+
* @note added in QGIS 3.0
146+
* @see distanceSquared()
147+
*/
148+
double distance( double x, double y ) const;
149+
150+
/**
151+
* Returns the 2D distance between this point and another point. In certain
152+
* cases it may be more appropriate to call the faster distanceSquared() method, eg
153+
* when comparing distances.
154+
* @note added in QGIS 3.0
155+
*/
156+
double distance( const QgsPointV2& other ) const;
157+
158+
/**
159+
* Returns the squared distance between this point a specified x, y coordinate. Calling
160+
* this is faster than calling distance(), and may be useful in use cases such as comparing
161+
* distances where the extra expense of calling distance() is not required.
162+
* @see distance()
163+
* @note added in QGIS 3.0
164+
*/
165+
double distanceSquared( double x, double y ) const;
166+
167+
/**
168+
* Returns the squared distance between this point another point. Calling
169+
* this is faster than calling distance(), and may be useful in use cases such as comparing
170+
* distances where the extra expense of calling distance() is not required.
171+
* @see distance()
172+
* @note added in QGIS 3.0
173+
*/
174+
double distanceSquared( const QgsPointV2& other ) const;
175+
176+
/**
177+
* Calculates azimuth between this point and other one (clockwise in degree, starting from north)
178+
* @note added in QGIS 3.0
179+
*/
180+
double azimuth( const QgsPointV2& other ) const;
181+
141182
//implementation of inherited methods
142183
virtual QgsRectangle boundingBox() const;
143184
virtual QString geometryType() const;

‎src/core/geometry/qgspointv2.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,30 @@ QPointF QgsPointV2::toQPointF() const
439439
{
440440
return QPointF( mX, mY );
441441
}
442+
443+
double QgsPointV2::distance( double x, double y ) const
444+
{
445+
return sqrt(( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y ) );
446+
}
447+
448+
double QgsPointV2::distance( const QgsPointV2& other ) const
449+
{
450+
return sqrt(( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) );
451+
}
452+
453+
double QgsPointV2::distanceSquared( double x, double y ) const
454+
{
455+
return ( mX - x ) * ( mX - x ) + ( mY - y ) * ( mY - y );
456+
}
457+
458+
double QgsPointV2::distanceSquared( const QgsPointV2& other ) const
459+
{
460+
return ( mX - other.x() ) * ( mX - other.x() ) + ( mY - other.y() ) * ( mY - other.y() ) ;
461+
}
462+
463+
double QgsPointV2::azimuth( const QgsPointV2& other ) const
464+
{
465+
double dx = other.x() - mX;
466+
double dy = other.y() - mY;
467+
return ( atan2( dx, dy ) * 180.0 / M_PI );
468+
}

‎src/core/geometry/qgspointv2.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,47 @@ class CORE_EXPORT QgsPointV2: public QgsAbstractGeometry
151151
*/
152152
QPointF toQPointF() const;
153153

154+
/**
155+
* Returns the distance between this point and a specified x, y coordinate. In certain
156+
* cases it may be more appropriate to call the faster distanceSquared() method, eg
157+
* when comparing distances.
158+
* @note added in QGIS 3.0
159+
* @see distanceSquared()
160+
*/
161+
double distance( double x, double y ) const;
162+
163+
/**
164+
* Returns the 2D distance between this point and another point. In certain
165+
* cases it may be more appropriate to call the faster distanceSquared() method, eg
166+
* when comparing distances.
167+
* @note added in QGIS 3.0
168+
*/
169+
double distance( const QgsPointV2& other ) const;
170+
171+
/**
172+
* Returns the squared distance between this point a specified x, y coordinate. Calling
173+
* this is faster than calling distance(), and may be useful in use cases such as comparing
174+
* distances where the extra expense of calling distance() is not required.
175+
* @see distance()
176+
* @note added in QGIS 3.0
177+
*/
178+
double distanceSquared( double x, double y ) const;
179+
180+
/**
181+
* Returns the squared distance between this point another point. Calling
182+
* this is faster than calling distance(), and may be useful in use cases such as comparing
183+
* distances where the extra expense of calling distance() is not required.
184+
* @see distance()
185+
* @note added in QGIS 3.0
186+
*/
187+
double distanceSquared( const QgsPointV2& other ) const;
188+
189+
/**
190+
* Calculates azimuth between this point and other one (clockwise in degree, starting from north)
191+
* @note added in QGIS 3.0
192+
*/
193+
double azimuth( const QgsPointV2& other ) const;
194+
154195
//implementation of inherited methods
155196
virtual QgsRectangle boundingBox() const override { return QgsRectangle( mX, mY, mX, mY ); }
156197
virtual QString geometryType() const override { return QStringLiteral( "Point" ); }

‎tests/src/core/testqgsgeometry.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,36 @@ void TestQgsGeometry::point()
792792
//boundary
793793
QgsPointV2 p30( 1.0, 2.0 );
794794
QVERIFY( !p30.boundary() );
795+
796+
// distance
797+
QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 2, 2 ) ), 1.0 );
798+
QCOMPARE( QgsPointV2( 1, 2 ).distance( 2, 2 ), 1.0 );
799+
QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 3, 2 ) ), 2.0 );
800+
QCOMPARE( QgsPointV2( 1, 2 ).distance( 3, 2 ), 2.0 );
801+
QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 1, 3 ) ), 1.0 );
802+
QCOMPARE( QgsPointV2( 1, 2 ).distance( 1, 3 ), 1.0 );
803+
QCOMPARE( QgsPointV2( 1, 2 ).distance( QgsPointV2( 1, 4 ) ), 2.0 );
804+
QCOMPARE( QgsPointV2( 1, 2 ).distance( 1, 4 ), 2.0 );
805+
QCOMPARE( QgsPointV2( 1, -2 ).distance( QgsPointV2( 1, -4 ) ), 2.0 );
806+
QCOMPARE( QgsPointV2( 1, -2 ).distance( 1, -4 ), 2.0 );
807+
808+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 2, 2 ) ), 1.0 );
809+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 2, 2 ), 1.0 );
810+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 3, 2 ) ), 4.0 );
811+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 3, 2 ), 4.0 );
812+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 1, 3 ) ), 1.0 );
813+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 1, 3 ), 1.0 );
814+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( QgsPointV2( 1, 4 ) ), 4.0 );
815+
QCOMPARE( QgsPointV2( 1, 2 ).distanceSquared( 1, 4 ), 4.0 );
816+
QCOMPARE( QgsPointV2( 1, -2 ).distanceSquared( QgsPointV2( 1, -4 ) ), 4.0 );
817+
QCOMPARE( QgsPointV2( 1, -2 ).distanceSquared( 1, -4 ), 4.0 );
818+
819+
// azimuth
820+
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 2 ) ), 0.0 );
821+
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 3 ) ), 0.0 );
822+
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 2, 2 ) ), 90.0 );
823+
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 1, 0 ) ), 180.0 );
824+
QCOMPARE( QgsPointV2( 1, 2 ).azimuth( QgsPointV2( 0, 2 ) ), -90.0 );
795825
}
796826

797827
void TestQgsGeometry::lineString()

0 commit comments

Comments
 (0)
Please sign in to comment.