Skip to content

Commit 2e44c11

Browse files
committedApr 6, 2016
Some QgsPoint improvements
- Modernize QgsVector, improve docs, add some methods missing from Python bindings - Add method to QgsPoint to project a point by a specified distance and bearing - Add distance methods to complement existing sqrDist squared distance methods - Rename QgsVector::normal to normalized (avoid confusion with normal vectors) - Add more QgsPoint operators - Add some more QgsPoint and QgsVector tests
1 parent 3a1f6c4 commit 2e44c11

File tree

6 files changed

+388
-56
lines changed

6 files changed

+388
-56
lines changed
 

‎python/core/qgspoint.sip

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,76 @@ class QgsVector
1010
%End
1111

1212
public:
13+
14+
/** Default constructor for QgsVector. Creates a vector with length of 0.0.
15+
*/
1316
QgsVector();
17+
18+
/** Constructor for QgsVector taking x and y component values.
19+
* @param x x-component
20+
* @param y y-component
21+
*/
1422
QgsVector( double x, double y );
1523

16-
//! @note not available in Python bindings
17-
//QgsVector operator-( void ) const;
24+
//! Swaps the sign of the x and y components of the vector.
25+
QgsVector operator-() const;
26+
27+
/** Returns a vector where the components have been multiplied by a scalar value.
28+
* @param scalar factor to multiply by
29+
*/
1830
QgsVector operator*( double scalar ) const;
31+
32+
/** Returns a vector where the components have been divided by a scalar value.
33+
* @param scalar factor to divide by
34+
*/
1935
QgsVector operator/( double scalar ) const;
36+
37+
/** Returns the sum of the x component of this vector multiplied by the x component of another
38+
* vector plus the y component of this vector multipled by the y component of another vector.
39+
*/
2040
double operator*( QgsVector v ) const;
41+
42+
/** Returns the length of the vector.
43+
*/
2144
double length() const;
2245

46+
/** Returns the vector's x-component.
47+
* @see y()
48+
*/
2349
double x() const;
50+
51+
/** Returns the vector's y-component.
52+
* @see x()
53+
*/
2454
double y() const;
2555

26-
// perpendicular vector (rotated 90 degrees counter-clockwise)
56+
/** Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
57+
*/
2758
QgsVector perpVector() const;
2859

29-
//! @note not available in Python bindings
30-
//double angle( void ) const;
60+
/** Returns the angle of the vector in radians.
61+
*/
62+
double angle() const;
63+
64+
/** Returns the angle between this vector and another vector in radians.
65+
*/
3166
double angle( QgsVector v ) const;
67+
68+
/** Rotates the vector by a specified angle.
69+
* @param rot angle in radians
70+
*/
3271
QgsVector rotateBy( double rot ) const;
33-
QgsVector normal() const;
3472

73+
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
74+
* if called on a vector with length of 0.
75+
* @deprecated use normalized() instead
76+
*/
77+
QgsVector normal() const /Deprecated/;
78+
79+
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
80+
* if called on a vector with length of 0.
81+
*/
82+
QgsVector normalized() const;
3583
};
3684

3785

@@ -138,17 +186,44 @@ class QgsPoint
138186
*/
139187
QString wellKnownText() const;
140188

141-
/** Returns the squared distance between this point and x,y*/
189+
/** Returns the squared distance between this point a specified x, y coordinate.
190+
* @see distance()
191+
*/
142192
double sqrDist( double x, double y ) const;
143193

144-
/** Returns the squared distance between this and other point*/
194+
/** Returns the squared distance between this point another point.
195+
* @see distance()
196+
*/
145197
double sqrDist( const QgsPoint& other ) const;
146198

199+
/** Returns the distance between this point and a specified x, y coordinate.
200+
* @param x x-coordniate
201+
* @param y y-coordinate
202+
* @see sqrDist()
203+
* @note added in QGIS 2.16
204+
*/
205+
double distance( double x, double y ) const;
206+
207+
/** Returns the distance between this point and another point.
208+
* @param other other point
209+
* @see sqrDist()
210+
* @note added in QGIS 2.16
211+
*/
212+
double distance( const QgsPoint& other ) const;
213+
147214
/** Returns the minimum distance between this point and a segment */
148215
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint /Out/, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
149216

150217
/** Calculates azimuth between this point and other one (clockwise in degree, starting from north) */
151-
double azimuth( const QgsPoint& other );
218+
double azimuth( const QgsPoint& other ) const;
219+
220+
/** Returns a new point which correponds to this point projected by a specified distance
221+
* in a specified bearing.
222+
* @param distance distance to project
223+
* @param bearing angle to project in, clockwise in degrees starting from north
224+
* @note added in QGIS 2.16
225+
*/
226+
QgsPoint project( double distance, double bearing ) const;
152227

153228
/** Compares this point with another point with a fuzzy tolerance
154229
* @param other point to compare with
@@ -173,6 +248,33 @@ class QgsPoint
173248
//! 3 if point is on open ray b.
174249
int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
175250

251+
//! Calculates the vector obtained by subtracting a point from this point
252+
QgsVector operator-( const QgsPoint& p ) const;
253+
254+
//! Adds a vector to this point in place
255+
QgsPoint &operator+=( QgsVector v );
256+
257+
//! Subtracts a vector from this point in place
258+
QgsPoint &operator-=( QgsVector v );
259+
260+
//! Adds a vector to this point
261+
QgsPoint operator+( QgsVector v ) const;
262+
263+
//! Subtracts a vector from this point
264+
QgsPoint operator-( QgsVector v ) const;
265+
266+
//! Multiplies the coordinates in this point by a scalar quantity
267+
QgsPoint operator*( double scalar ) const;
268+
269+
//! Divides the coordinates in this point by a scalar quantity
270+
QgsPoint operator/( double scalar ) const;
271+
272+
//! Multiplies the coordinates in this point by a scalar quantity in place
273+
QgsPoint &operator*=( double scalar );
274+
275+
//! Divides the coordinates in this point by a scalar quantity in place
276+
QgsPoint &operator/=( double scalar );
277+
176278
SIP_PYOBJECT __repr__();
177279
%MethodCode
178280
QString str = "(" + QString::number(sipCpp->x()) + "," + QString::number(sipCpp->y()) + ")";

‎src/core/qgspoint.cpp

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,26 @@
2828
// QgsVector
2929
//
3030

31-
QgsVector::QgsVector() : m_x( 0.0 ), m_y( 0.0 )
31+
QgsVector::QgsVector()
32+
: mX( 0.0 )
33+
, mY( 0.0 )
3234
{
3335
}
3436

35-
QgsVector::QgsVector( double x, double y ) : m_x( x ), m_y( y )
37+
QgsVector::QgsVector( double x, double y )
38+
: mX( x )
39+
, mY( y )
3640
{
3741
}
3842

39-
QgsVector QgsVector::operator-( void ) const
43+
QgsVector QgsVector::operator-() const
4044
{
41-
return QgsVector( -m_x, -m_y );
45+
return QgsVector( -mX, -mY );
4246
}
4347

4448
QgsVector QgsVector::operator*( double scalar ) const
4549
{
46-
return QgsVector( m_x * scalar, m_y * scalar );
50+
return QgsVector( mX * scalar, mY * scalar );
4751
}
4852

4953
QgsVector QgsVector::operator/( double scalar ) const
@@ -53,33 +57,32 @@ QgsVector QgsVector::operator/( double scalar ) const
5357

5458
double QgsVector::operator*( QgsVector v ) const
5559
{
56-
return m_x * v.m_x + m_y * v.m_y;
60+
return mX * v.mX + mY * v.mY;
5761
}
5862

5963
double QgsVector::length() const
6064
{
61-
return sqrt( m_x * m_x + m_y * m_y );
65+
return sqrt( mX * mX + mY * mY );
6266
}
6367

6468
double QgsVector::x() const
6569
{
66-
return m_x;
70+
return mX;
6771
}
6872

6973
double QgsVector::y() const
7074
{
71-
return m_y;
75+
return mY;
7276
}
7377

74-
// perpendicular vector (rotated 90 degrees counter-clockwise)
7578
QgsVector QgsVector::perpVector() const
7679
{
77-
return QgsVector( -m_y, m_x );
80+
return QgsVector( -mY, mX );
7881
}
7982

80-
double QgsVector::angle( void ) const
83+
double QgsVector::angle() const
8184
{
82-
double ang = atan2( m_y, m_x );
85+
double ang = atan2( mY, mX );
8386
return ang < 0.0 ? ang + 2.0 * M_PI : ang;
8487
}
8588

@@ -90,18 +93,23 @@ double QgsVector::angle( QgsVector v ) const
9093

9194
QgsVector QgsVector::rotateBy( double rot ) const
9295
{
93-
double ang = atan2( m_y, m_x ) + rot;
96+
double ang = atan2( mY, mX ) + rot;
9497
double len = length();
9598
return QgsVector( len * cos( ang ), len * sin( ang ) );
9699
}
97100

98101
QgsVector QgsVector::normal() const
102+
{
103+
return normalized();
104+
}
105+
106+
QgsVector QgsVector::normalized() const
99107
{
100108
double len = length();
101109

102110
if ( len == 0.0 )
103111
{
104-
throw QgsException( "normal vector of null vector undefined" );
112+
throw QgsException( "normalized vector of null vector undefined" );
105113
}
106114

107115
return *this / len;
@@ -352,13 +360,31 @@ double QgsPoint::sqrDist( const QgsPoint& other ) const
352360
return sqrDist( other.x(), other.y() );
353361
}
354362

355-
double QgsPoint::azimuth( const QgsPoint& other )
363+
double QgsPoint::distance( double x, double y ) const
364+
{
365+
return sqrt( sqrDist( x, y ) );
366+
}
367+
368+
double QgsPoint::distance( const QgsPoint& other ) const
369+
{
370+
return sqrt( sqrDist( other ) );
371+
}
372+
373+
double QgsPoint::azimuth( const QgsPoint& other ) const
356374
{
357375
double dx = other.x() - m_x;
358376
double dy = other.y() - m_y;
359377
return ( atan2( dx, dy ) * 180.0 / M_PI );
360378
}
361379

380+
QgsPoint QgsPoint::project( double distance, double bearing ) const
381+
{
382+
double rads = bearing * M_PI / 180.0;
383+
double dx = distance * sin( rads );
384+
double dy = distance * cos( rads );
385+
return QgsPoint( m_x + dx, m_y + dy );
386+
}
387+
362388
bool QgsPoint::compare( const QgsPoint &other, double epsilon ) const
363389
{
364390
return ( qgsDoubleNear( m_x, other.x(), epsilon ) && qgsDoubleNear( m_y, other.y(), epsilon ) );

‎src/core/qgspoint.h

Lines changed: 110 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,82 @@
3131

3232
class CORE_EXPORT QgsVector
3333
{
34-
double m_x, m_y;
3534

3635
public:
36+
37+
/** Default constructor for QgsVector. Creates a vector with length of 0.0.
38+
*/
3739
QgsVector();
40+
41+
/** Constructor for QgsVector taking x and y component values.
42+
* @param x x-component
43+
* @param y y-component
44+
*/
3845
QgsVector( double x, double y );
3946

40-
//! @note not available in Python bindings
41-
QgsVector operator-( void ) const;
47+
//! Swaps the sign of the x and y components of the vector.
48+
QgsVector operator-() const;
49+
50+
/** Returns a vector where the components have been multiplied by a scalar value.
51+
* @param scalar factor to multiply by
52+
*/
4253
QgsVector operator*( double scalar ) const;
54+
55+
/** Returns a vector where the components have been divided by a scalar value.
56+
* @param scalar factor to divide by
57+
*/
4358
QgsVector operator/( double scalar ) const;
59+
60+
/** Returns the sum of the x component of this vector multiplied by the x component of another
61+
* vector plus the y component of this vector multipled by the y component of another vector.
62+
*/
4463
double operator*( QgsVector v ) const;
64+
65+
/** Returns the length of the vector.
66+
*/
4567
double length() const;
4668

69+
/** Returns the vector's x-component.
70+
* @see y()
71+
*/
4772
double x() const;
73+
74+
/** Returns the vector's y-component.
75+
* @see x()
76+
*/
4877
double y() const;
4978

50-
// perpendicular vector (rotated 90 degrees counter-clockwise)
79+
/** Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
80+
*/
5181
QgsVector perpVector() const;
5282

53-
//! @note not available in Python bindings
54-
double angle( void ) const;
83+
/** Returns the angle of the vector in radians.
84+
*/
85+
double angle() const;
86+
87+
/** Returns the angle between this vector and another vector in radians.
88+
*/
5589
double angle( QgsVector v ) const;
90+
91+
/** Rotates the vector by a specified angle.
92+
* @param rot angle in radians
93+
*/
5694
QgsVector rotateBy( double rot ) const;
57-
QgsVector normal() const;
95+
96+
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
97+
* if called on a vector with length of 0.
98+
* @deprecated use normalized() instead
99+
*/
100+
Q_DECL_DEPRECATED QgsVector normal() const;
101+
102+
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
103+
* if called on a vector with length of 0.
104+
*/
105+
QgsVector normalized() const;
106+
107+
private:
108+
109+
double mX, mY;
58110

59111
};
60112

@@ -179,17 +231,44 @@ class CORE_EXPORT QgsPoint
179231
*/
180232
QString wellKnownText() const;
181233

182-
/** Returns the squared distance between this point and x,y*/
234+
/** Returns the squared distance between this point a specified x, y coordinate.
235+
* @see distance()
236+
*/
183237
double sqrDist( double x, double y ) const;
184238

185-
/** Returns the squared distance between this and other point*/
239+
/** Returns the squared distance between this point another point.
240+
* @see distance()
241+
*/
186242
double sqrDist( const QgsPoint& other ) const;
187243

244+
/** Returns the distance between this point and a specified x, y coordinate.
245+
* @param x x-coordniate
246+
* @param y y-coordinate
247+
* @see sqrDist()
248+
* @note added in QGIS 2.16
249+
*/
250+
double distance( double x, double y ) const;
251+
252+
/** Returns the distance between this point and another point.
253+
* @param other other point
254+
* @see sqrDist()
255+
* @note added in QGIS 2.16
256+
*/
257+
double distance( const QgsPoint& other ) const;
258+
188259
/** Returns the minimum distance between this point and a segment */
189260
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
190261

191262
/** Calculates azimuth between this point and other one (clockwise in degree, starting from north) */
192-
double azimuth( const QgsPoint& other );
263+
double azimuth( const QgsPoint& other ) const;
264+
265+
/** Returns a new point which correponds to this point projected by a specified distance
266+
* in a specified bearing.
267+
* @param distance distance to project
268+
* @param bearing angle to project in, clockwise in degrees starting from north
269+
* @note added in QGIS 2.16
270+
*/
271+
QgsPoint project( double distance, double bearing ) const;
193272

194273
/** Compares this point with another point with a fuzzy tolerance
195274
* @param other point to compare with
@@ -217,12 +296,33 @@ class CORE_EXPORT QgsPoint
217296
//! Assignment
218297
QgsPoint & operator=( const QgsPoint &other );
219298

299+
//! Calculates the vector obtained by subtracting a point from this point
220300
QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
301+
302+
//! Adds a vector to this point in place
221303
QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
304+
305+
//! Subtracts a vector from this point in place
222306
QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
307+
308+
//! Adds a vector to this point
223309
QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
310+
311+
//! Subtracts a vector from this point
224312
QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
225313

314+
//! Multiplies the coordinates in this point by a scalar quantity
315+
QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
316+
317+
//! Divides the coordinates in this point by a scalar quantity
318+
QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
319+
320+
//! Multiplies the coordinates in this point by a scalar quantity in place
321+
QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
322+
323+
//! Divides the coordinates in this point by a scalar quantity in place
324+
QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
325+
226326
private:
227327

228328
//! x coordinate

‎src/plugins/geometry_checker/checks/qgsgeometryanglecheck.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void QgsGeometryAngleCheck::collectErrors( QList<QgsGeometryCheckError*>& errors
3838
QgsVector v21, v23;
3939
try
4040
{
41-
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normal();
42-
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normal();
41+
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normalized();
42+
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normalized();
4343
}
4444
catch ( const QgsException& )
4545
{
@@ -84,8 +84,8 @@ void QgsGeometryAngleCheck::fixError( QgsGeometryCheckError* error, int method,
8484
QgsVector v21, v23;
8585
try
8686
{
87-
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normal();
88-
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normal();
87+
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normalized();
88+
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normalized();
8989
}
9090
catch ( const QgsException& )
9191
{

‎src/plugins/geometry_checker/utils/qgsgeomutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace QgsGeomUtils
7575
QgsPointV2 p2 = geom1->vertexAt( QgsVertexId( iPart1, iRing1, jVert1 ) );
7676
double lambdap1 = 0.;
7777
double lambdap2 = qSqrt( QgsGeometryUtils::sqrDistance2D( p1, p2 ) );
78-
QgsVector d = QgsVector( p2.x() - p1.x(), p2.y() - p1.y() ).normal();
78+
QgsVector d = QgsVector( p2.x() - p1.x(), p2.y() - p1.y() ).normalized();
7979

8080
for ( int iPart2 = 0, nParts2 = geom2->partCount(); iPart2 < nParts2; ++iPart2 )
8181
{

‎tests/src/core/testqgspoint.cpp

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,19 @@ class TestQgsPoint: public QObject
3838
void gettersSetters();
3939
void constructors();
4040
void toQPointF();
41+
void operators();
4142
void toString();
4243
void toDegreesMinutesSeconds();
4344
void toDegreesMinutesSecondsNoSuffix();
4445
void toDegreesMinutesSecondsPadded();
4546
void toDegreesMinutes();
4647
void toDegreesMinutesNoSuffix();
4748
void toDegreesMinutesPadded();
48-
void wellKnownText();
4949
void sqrDist();
50-
void multiply();
51-
void onSegment();
50+
void distance();
5251
void compare();
52+
void project();
53+
void vector(); //tests for QgsVector
5354

5455
private:
5556
QgsPoint mPoint1;
@@ -129,6 +130,28 @@ void TestQgsPoint::toQPointF()
129130
QCOMPARE( result.y(), -20.0 );
130131
}
131132

133+
void TestQgsPoint::operators()
134+
{
135+
QgsPoint p( 1, 2 );
136+
QCOMPARE( p - QgsVector( 3, 5 ), QgsPoint( -2, -3 ) );
137+
p -= QgsVector( 3, 5 );
138+
QCOMPARE( p, QgsPoint( -2, -3 ) );
139+
140+
p = QgsPoint( 1, 2 );
141+
QCOMPARE( p + QgsVector( 3, 5 ), QgsPoint( 4, 7 ) );
142+
p += QgsVector( 3, 5 );
143+
QCOMPARE( p, QgsPoint( 4, 7 ) );
144+
145+
p = QgsPoint( 1, 2 );
146+
QCOMPARE( p * 3, QgsPoint( 3, 6 ) );
147+
p *= 3;
148+
QCOMPARE( p, QgsPoint( 3, 6 ) );
149+
150+
QCOMPARE( p / 3.0, QgsPoint( 1, 2 ) );
151+
p /= 3;
152+
QCOMPARE( p, QgsPoint( 1, 2 ) );
153+
}
154+
132155
void TestQgsPoint::initTestCase()
133156
{
134157
//
@@ -603,24 +626,32 @@ void TestQgsPoint::toDegreesMinutesPadded()
603626
QCOMPARE( QgsPoint( -0.000001, 0 ).toDegreesMinutes( 5, true, true ), myControlString );
604627
}
605628

606-
void TestQgsPoint::wellKnownText()
607-
{
608-
609-
}
610-
611629
void TestQgsPoint::sqrDist()
612630
{
613-
631+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 2, 2 ) ), 1.0 );
632+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 2, 2 ), 1.0 );
633+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 3, 2 ) ), 4.0 );
634+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 3, 2 ), 4.0 );
635+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 1, 3 ) ), 1.0 );
636+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 1, 3 ), 1.0 );
637+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 1, 4 ) ), 4.0 );
638+
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 1, 4 ), 4.0 );
639+
QCOMPARE( QgsPoint( 1, -2 ).sqrDist( QgsPoint( 1, -4 ) ), 4.0 );
640+
QCOMPARE( QgsPoint( 1, -2 ).sqrDist( 1, -4 ), 4.0 );
614641
}
615642

616-
void TestQgsPoint::multiply()
643+
void TestQgsPoint::distance()
617644
{
618-
619-
}
620-
621-
void TestQgsPoint::onSegment()
622-
{
623-
645+
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 2, 2 ) ), 1.0 );
646+
QCOMPARE( QgsPoint( 1, 2 ).distance( 2, 2 ), 1.0 );
647+
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 3, 2 ) ), 2.0 );
648+
QCOMPARE( QgsPoint( 1, 2 ).distance( 3, 2 ), 2.0 );
649+
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 1, 3 ) ), 1.0 );
650+
QCOMPARE( QgsPoint( 1, 2 ).distance( 1, 3 ), 1.0 );
651+
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 1, 4 ) ), 2.0 );
652+
QCOMPARE( QgsPoint( 1, 2 ).distance( 1, 4 ), 2.0 );
653+
QCOMPARE( QgsPoint( 1, -2 ).distance( QgsPoint( 1, -4 ) ), 2.0 );
654+
QCOMPARE( QgsPoint( 1, -2 ).distance( 1, -4 ), 2.0 );
624655
}
625656

626657
void TestQgsPoint::compare()
@@ -634,5 +665,78 @@ void TestQgsPoint::compare()
634665
QVERIFY( point4.compare( QgsPoint( 10 / 3.0, 12 / 7.0 ) ) );
635666
}
636667

668+
void TestQgsPoint::project()
669+
{
670+
// test projecting a point
671+
QgsPoint p( 1, 2 );
672+
QVERIFY( p.project( 1, 0 ).compare( QgsPoint( 1, 3 ), 0.0000000001 ) );
673+
QVERIFY( p.project( 1.5, 90 ).compare( QgsPoint( 2.5, 2 ), 0.0000000001 ) );
674+
QVERIFY( p.project( 2, 180 ).compare( QgsPoint( 1, 0 ), 0.0000000001 ) );
675+
QVERIFY( p.project( 5, 270 ).compare( QgsPoint( -4, 2 ), 0.0000000001 ) );
676+
QVERIFY( p.project( 6, 360 ).compare( QgsPoint( 1, 8 ), 0.0000000001 ) );
677+
QVERIFY( p.project( 5, 450 ).compare( QgsPoint( 6, 2 ), 0.0000000001 ) );
678+
QVERIFY( p.project( -1, 0 ).compare( QgsPoint( 1, 1 ), 0.0000000001 ) );
679+
QVERIFY( p.project( 1.5, -90 ).compare( QgsPoint( -0.5, 2 ), 0.0000000001 ) );
680+
}
681+
682+
void TestQgsPoint::vector()
683+
{
684+
//test constructors, x(), y() accessors
685+
QgsVector v1;
686+
QCOMPARE( v1.x(), 0.0 );
687+
QCOMPARE( v1.y(), 0.0 );
688+
QgsVector v2( 1.0, 2.0 );
689+
QCOMPARE( v2.x(), 1.0 );
690+
QCOMPARE( v2.y(), 2.0 );
691+
692+
// operator-
693+
QCOMPARE(( -v2 ).x(), -1.0 );
694+
QCOMPARE(( -v2 ).y(), -2.0 );
695+
696+
// operator*
697+
QCOMPARE(( v2 * 2.0 ).x(), 2.0 );
698+
QCOMPARE(( v2 * 2.0 ).y(), 4.0 );
699+
700+
// operator/
701+
QCOMPARE(( v2 / 2.0 ).x(), 0.5 );
702+
QCOMPARE(( v2 / 2.0 ).y(), 1.0 );
703+
704+
// QgsVector * QgsVector
705+
QCOMPARE(( v2 * v2 ), 5.0 );
706+
707+
// length
708+
QCOMPARE( v1.length(), 0.0 );
709+
QVERIFY( qgsDoubleNear( v2.length(), sqrt( 5 ), 0.000000001 ) );
710+
711+
// perpVector
712+
QCOMPARE( QgsVector( 2, 3 ).perpVector().x(), -3.0 );
713+
QCOMPARE( QgsVector( 2, 3 ).perpVector().y(), 2.0 );
714+
715+
// angle
716+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle(), M_PI_2, 0.0000001 ) );
717+
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle(), 0, 0.0000001 ) );
718+
QVERIFY( qgsDoubleNear( QgsVector( -1, 0 ).angle(), M_PI, 0.0000001 ) );
719+
QVERIFY( qgsDoubleNear( QgsVector( 0, -1 ).angle(), 3 * M_PI_2, 0.0000001 ) );
720+
QVERIFY( qgsDoubleNear( QgsVector( 0, 0 ).angle(), 0, 0.0000001 ) );
721+
722+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle( QgsVector( 0, 1 ) ), 0, 0.0000001 ) );
723+
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle( QgsVector( 0, 1 ) ), M_PI_2, 0.0000001 ) );
724+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle( QgsVector( -1, 0 ) ), M_PI_2, 0.0000001 ) );
725+
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle( QgsVector( -1, 0 ) ), M_PI, 0.0000001 ) );
726+
QVERIFY( qgsDoubleNear( QgsVector( -1, 0 ).angle( QgsVector( 0, 0 ) ), -M_PI, 0.0000001 ) );
727+
728+
// rotateBy
729+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI_2 ).x(), -1.0, 0.0000001 ) );
730+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI_2 ).y(), 0.0, 0.0000001 ) );
731+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI ).x(), 0.0, 0.0000001 ) );
732+
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI ).y(), -1.0, 0.0000001 ) );
733+
734+
// normalized
735+
QCOMPARE( QgsVector( 0, 2 ).normalized().x(), 0.0 );
736+
QCOMPARE( QgsVector( 0, 2 ).normalized().y(), 1.0 );
737+
QCOMPARE( QgsVector( 2, 0 ).normalized().x(), 1.0 );
738+
QCOMPARE( QgsVector( 2, 0 ).normalized().y(), 0.0 );
739+
}
740+
637741
QTEST_MAIN( TestQgsPoint )
638742
#include "testqgspoint.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.