mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
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
This commit is contained in:
parent
3a1f6c429d
commit
2e44c11914
@ -10,28 +10,76 @@ class QgsVector
|
||||
%End
|
||||
|
||||
public:
|
||||
|
||||
/** Default constructor for QgsVector. Creates a vector with length of 0.0.
|
||||
*/
|
||||
QgsVector();
|
||||
|
||||
/** Constructor for QgsVector taking x and y component values.
|
||||
* @param x x-component
|
||||
* @param y y-component
|
||||
*/
|
||||
QgsVector( double x, double y );
|
||||
|
||||
//! @note not available in Python bindings
|
||||
//QgsVector operator-( void ) const;
|
||||
//! Swaps the sign of the x and y components of the vector.
|
||||
QgsVector operator-() const;
|
||||
|
||||
/** Returns a vector where the components have been multiplied by a scalar value.
|
||||
* @param scalar factor to multiply by
|
||||
*/
|
||||
QgsVector operator*( double scalar ) const;
|
||||
|
||||
/** Returns a vector where the components have been divided by a scalar value.
|
||||
* @param scalar factor to divide by
|
||||
*/
|
||||
QgsVector operator/( double scalar ) const;
|
||||
|
||||
/** Returns the sum of the x component of this vector multiplied by the x component of another
|
||||
* vector plus the y component of this vector multipled by the y component of another vector.
|
||||
*/
|
||||
double operator*( QgsVector v ) const;
|
||||
|
||||
/** Returns the length of the vector.
|
||||
*/
|
||||
double length() const;
|
||||
|
||||
/** Returns the vector's x-component.
|
||||
* @see y()
|
||||
*/
|
||||
double x() const;
|
||||
|
||||
/** Returns the vector's y-component.
|
||||
* @see x()
|
||||
*/
|
||||
double y() const;
|
||||
|
||||
// perpendicular vector (rotated 90 degrees counter-clockwise)
|
||||
/** Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
|
||||
*/
|
||||
QgsVector perpVector() const;
|
||||
|
||||
//! @note not available in Python bindings
|
||||
//double angle( void ) const;
|
||||
double angle( QgsVector v ) const;
|
||||
QgsVector rotateBy( double rot ) const;
|
||||
QgsVector normal() const;
|
||||
/** Returns the angle of the vector in radians.
|
||||
*/
|
||||
double angle() const;
|
||||
|
||||
/** Returns the angle between this vector and another vector in radians.
|
||||
*/
|
||||
double angle( QgsVector v ) const;
|
||||
|
||||
/** Rotates the vector by a specified angle.
|
||||
* @param rot angle in radians
|
||||
*/
|
||||
QgsVector rotateBy( double rot ) const;
|
||||
|
||||
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
|
||||
* if called on a vector with length of 0.
|
||||
* @deprecated use normalized() instead
|
||||
*/
|
||||
QgsVector normal() const /Deprecated/;
|
||||
|
||||
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
|
||||
* if called on a vector with length of 0.
|
||||
*/
|
||||
QgsVector normalized() const;
|
||||
};
|
||||
|
||||
|
||||
@ -138,17 +186,44 @@ class QgsPoint
|
||||
*/
|
||||
QString wellKnownText() const;
|
||||
|
||||
/** Returns the squared distance between this point and x,y*/
|
||||
/** Returns the squared distance between this point a specified x, y coordinate.
|
||||
* @see distance()
|
||||
*/
|
||||
double sqrDist( double x, double y ) const;
|
||||
|
||||
/** Returns the squared distance between this and other point*/
|
||||
/** Returns the squared distance between this point another point.
|
||||
* @see distance()
|
||||
*/
|
||||
double sqrDist( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns the distance between this point and a specified x, y coordinate.
|
||||
* @param x x-coordniate
|
||||
* @param y y-coordinate
|
||||
* @see sqrDist()
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
double distance( double x, double y ) const;
|
||||
|
||||
/** Returns the distance between this point and another point.
|
||||
* @param other other point
|
||||
* @see sqrDist()
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
double distance( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns the minimum distance between this point and a segment */
|
||||
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint /Out/, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
|
||||
|
||||
/** Calculates azimuth between this point and other one (clockwise in degree, starting from north) */
|
||||
double azimuth( const QgsPoint& other );
|
||||
double azimuth( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns a new point which correponds to this point projected by a specified distance
|
||||
* in a specified bearing.
|
||||
* @param distance distance to project
|
||||
* @param bearing angle to project in, clockwise in degrees starting from north
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
QgsPoint project( double distance, double bearing ) const;
|
||||
|
||||
/** Compares this point with another point with a fuzzy tolerance
|
||||
* @param other point to compare with
|
||||
@ -173,6 +248,33 @@ class QgsPoint
|
||||
//! 3 if point is on open ray b.
|
||||
int onSegment( const QgsPoint& a, const QgsPoint& b ) const;
|
||||
|
||||
//! Calculates the vector obtained by subtracting a point from this point
|
||||
QgsVector operator-( const QgsPoint& p ) const;
|
||||
|
||||
//! Adds a vector to this point in place
|
||||
QgsPoint &operator+=( QgsVector v );
|
||||
|
||||
//! Subtracts a vector from this point in place
|
||||
QgsPoint &operator-=( QgsVector v );
|
||||
|
||||
//! Adds a vector to this point
|
||||
QgsPoint operator+( QgsVector v ) const;
|
||||
|
||||
//! Subtracts a vector from this point
|
||||
QgsPoint operator-( QgsVector v ) const;
|
||||
|
||||
//! Multiplies the coordinates in this point by a scalar quantity
|
||||
QgsPoint operator*( double scalar ) const;
|
||||
|
||||
//! Divides the coordinates in this point by a scalar quantity
|
||||
QgsPoint operator/( double scalar ) const;
|
||||
|
||||
//! Multiplies the coordinates in this point by a scalar quantity in place
|
||||
QgsPoint &operator*=( double scalar );
|
||||
|
||||
//! Divides the coordinates in this point by a scalar quantity in place
|
||||
QgsPoint &operator/=( double scalar );
|
||||
|
||||
SIP_PYOBJECT __repr__();
|
||||
%MethodCode
|
||||
QString str = "(" + QString::number(sipCpp->x()) + "," + QString::number(sipCpp->y()) + ")";
|
||||
|
@ -28,22 +28,26 @@
|
||||
// QgsVector
|
||||
//
|
||||
|
||||
QgsVector::QgsVector() : m_x( 0.0 ), m_y( 0.0 )
|
||||
QgsVector::QgsVector()
|
||||
: mX( 0.0 )
|
||||
, mY( 0.0 )
|
||||
{
|
||||
}
|
||||
|
||||
QgsVector::QgsVector( double x, double y ) : m_x( x ), m_y( y )
|
||||
QgsVector::QgsVector( double x, double y )
|
||||
: mX( x )
|
||||
, mY( y )
|
||||
{
|
||||
}
|
||||
|
||||
QgsVector QgsVector::operator-( void ) const
|
||||
QgsVector QgsVector::operator-() const
|
||||
{
|
||||
return QgsVector( -m_x, -m_y );
|
||||
return QgsVector( -mX, -mY );
|
||||
}
|
||||
|
||||
QgsVector QgsVector::operator*( double scalar ) const
|
||||
{
|
||||
return QgsVector( m_x * scalar, m_y * scalar );
|
||||
return QgsVector( mX * scalar, mY * scalar );
|
||||
}
|
||||
|
||||
QgsVector QgsVector::operator/( double scalar ) const
|
||||
@ -53,33 +57,32 @@ QgsVector QgsVector::operator/( double scalar ) const
|
||||
|
||||
double QgsVector::operator*( QgsVector v ) const
|
||||
{
|
||||
return m_x * v.m_x + m_y * v.m_y;
|
||||
return mX * v.mX + mY * v.mY;
|
||||
}
|
||||
|
||||
double QgsVector::length() const
|
||||
{
|
||||
return sqrt( m_x * m_x + m_y * m_y );
|
||||
return sqrt( mX * mX + mY * mY );
|
||||
}
|
||||
|
||||
double QgsVector::x() const
|
||||
{
|
||||
return m_x;
|
||||
return mX;
|
||||
}
|
||||
|
||||
double QgsVector::y() const
|
||||
{
|
||||
return m_y;
|
||||
return mY;
|
||||
}
|
||||
|
||||
// perpendicular vector (rotated 90 degrees counter-clockwise)
|
||||
QgsVector QgsVector::perpVector() const
|
||||
{
|
||||
return QgsVector( -m_y, m_x );
|
||||
return QgsVector( -mY, mX );
|
||||
}
|
||||
|
||||
double QgsVector::angle( void ) const
|
||||
double QgsVector::angle() const
|
||||
{
|
||||
double ang = atan2( m_y, m_x );
|
||||
double ang = atan2( mY, mX );
|
||||
return ang < 0.0 ? ang + 2.0 * M_PI : ang;
|
||||
}
|
||||
|
||||
@ -90,18 +93,23 @@ double QgsVector::angle( QgsVector v ) const
|
||||
|
||||
QgsVector QgsVector::rotateBy( double rot ) const
|
||||
{
|
||||
double ang = atan2( m_y, m_x ) + rot;
|
||||
double ang = atan2( mY, mX ) + rot;
|
||||
double len = length();
|
||||
return QgsVector( len * cos( ang ), len * sin( ang ) );
|
||||
}
|
||||
|
||||
QgsVector QgsVector::normal() const
|
||||
{
|
||||
return normalized();
|
||||
}
|
||||
|
||||
QgsVector QgsVector::normalized() const
|
||||
{
|
||||
double len = length();
|
||||
|
||||
if ( len == 0.0 )
|
||||
{
|
||||
throw QgsException( "normal vector of null vector undefined" );
|
||||
throw QgsException( "normalized vector of null vector undefined" );
|
||||
}
|
||||
|
||||
return *this / len;
|
||||
@ -352,13 +360,31 @@ double QgsPoint::sqrDist( const QgsPoint& other ) const
|
||||
return sqrDist( other.x(), other.y() );
|
||||
}
|
||||
|
||||
double QgsPoint::azimuth( const QgsPoint& other )
|
||||
double QgsPoint::distance( double x, double y ) const
|
||||
{
|
||||
return sqrt( sqrDist( x, y ) );
|
||||
}
|
||||
|
||||
double QgsPoint::distance( const QgsPoint& other ) const
|
||||
{
|
||||
return sqrt( sqrDist( other ) );
|
||||
}
|
||||
|
||||
double QgsPoint::azimuth( const QgsPoint& other ) const
|
||||
{
|
||||
double dx = other.x() - m_x;
|
||||
double dy = other.y() - m_y;
|
||||
return ( atan2( dx, dy ) * 180.0 / M_PI );
|
||||
}
|
||||
|
||||
QgsPoint QgsPoint::project( double distance, double bearing ) const
|
||||
{
|
||||
double rads = bearing * M_PI / 180.0;
|
||||
double dx = distance * sin( rads );
|
||||
double dy = distance * cos( rads );
|
||||
return QgsPoint( m_x + dx, m_y + dy );
|
||||
}
|
||||
|
||||
bool QgsPoint::compare( const QgsPoint &other, double epsilon ) const
|
||||
{
|
||||
return ( qgsDoubleNear( m_x, other.x(), epsilon ) && qgsDoubleNear( m_y, other.y(), epsilon ) );
|
||||
|
@ -31,30 +31,82 @@
|
||||
|
||||
class CORE_EXPORT QgsVector
|
||||
{
|
||||
double m_x, m_y;
|
||||
|
||||
public:
|
||||
|
||||
/** Default constructor for QgsVector. Creates a vector with length of 0.0.
|
||||
*/
|
||||
QgsVector();
|
||||
|
||||
/** Constructor for QgsVector taking x and y component values.
|
||||
* @param x x-component
|
||||
* @param y y-component
|
||||
*/
|
||||
QgsVector( double x, double y );
|
||||
|
||||
//! @note not available in Python bindings
|
||||
QgsVector operator-( void ) const;
|
||||
//! Swaps the sign of the x and y components of the vector.
|
||||
QgsVector operator-() const;
|
||||
|
||||
/** Returns a vector where the components have been multiplied by a scalar value.
|
||||
* @param scalar factor to multiply by
|
||||
*/
|
||||
QgsVector operator*( double scalar ) const;
|
||||
|
||||
/** Returns a vector where the components have been divided by a scalar value.
|
||||
* @param scalar factor to divide by
|
||||
*/
|
||||
QgsVector operator/( double scalar ) const;
|
||||
|
||||
/** Returns the sum of the x component of this vector multiplied by the x component of another
|
||||
* vector plus the y component of this vector multipled by the y component of another vector.
|
||||
*/
|
||||
double operator*( QgsVector v ) const;
|
||||
|
||||
/** Returns the length of the vector.
|
||||
*/
|
||||
double length() const;
|
||||
|
||||
/** Returns the vector's x-component.
|
||||
* @see y()
|
||||
*/
|
||||
double x() const;
|
||||
|
||||
/** Returns the vector's y-component.
|
||||
* @see x()
|
||||
*/
|
||||
double y() const;
|
||||
|
||||
// perpendicular vector (rotated 90 degrees counter-clockwise)
|
||||
/** Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise)
|
||||
*/
|
||||
QgsVector perpVector() const;
|
||||
|
||||
//! @note not available in Python bindings
|
||||
double angle( void ) const;
|
||||
/** Returns the angle of the vector in radians.
|
||||
*/
|
||||
double angle() const;
|
||||
|
||||
/** Returns the angle between this vector and another vector in radians.
|
||||
*/
|
||||
double angle( QgsVector v ) const;
|
||||
|
||||
/** Rotates the vector by a specified angle.
|
||||
* @param rot angle in radians
|
||||
*/
|
||||
QgsVector rotateBy( double rot ) const;
|
||||
QgsVector normal() const;
|
||||
|
||||
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
|
||||
* if called on a vector with length of 0.
|
||||
* @deprecated use normalized() instead
|
||||
*/
|
||||
Q_DECL_DEPRECATED QgsVector normal() const;
|
||||
|
||||
/** Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0). Will throw an expection
|
||||
* if called on a vector with length of 0.
|
||||
*/
|
||||
QgsVector normalized() const;
|
||||
|
||||
private:
|
||||
|
||||
double mX, mY;
|
||||
|
||||
};
|
||||
|
||||
@ -179,17 +231,44 @@ class CORE_EXPORT QgsPoint
|
||||
*/
|
||||
QString wellKnownText() const;
|
||||
|
||||
/** Returns the squared distance between this point and x,y*/
|
||||
/** Returns the squared distance between this point a specified x, y coordinate.
|
||||
* @see distance()
|
||||
*/
|
||||
double sqrDist( double x, double y ) const;
|
||||
|
||||
/** Returns the squared distance between this and other point*/
|
||||
/** Returns the squared distance between this point another point.
|
||||
* @see distance()
|
||||
*/
|
||||
double sqrDist( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns the distance between this point and a specified x, y coordinate.
|
||||
* @param x x-coordniate
|
||||
* @param y y-coordinate
|
||||
* @see sqrDist()
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
double distance( double x, double y ) const;
|
||||
|
||||
/** Returns the distance between this point and another point.
|
||||
* @param other other point
|
||||
* @see sqrDist()
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
double distance( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns the minimum distance between this point and a segment */
|
||||
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon = DEFAULT_SEGMENT_EPSILON ) const;
|
||||
|
||||
/** Calculates azimuth between this point and other one (clockwise in degree, starting from north) */
|
||||
double azimuth( const QgsPoint& other );
|
||||
double azimuth( const QgsPoint& other ) const;
|
||||
|
||||
/** Returns a new point which correponds to this point projected by a specified distance
|
||||
* in a specified bearing.
|
||||
* @param distance distance to project
|
||||
* @param bearing angle to project in, clockwise in degrees starting from north
|
||||
* @note added in QGIS 2.16
|
||||
*/
|
||||
QgsPoint project( double distance, double bearing ) const;
|
||||
|
||||
/** Compares this point with another point with a fuzzy tolerance
|
||||
* @param other point to compare with
|
||||
@ -217,12 +296,33 @@ class CORE_EXPORT QgsPoint
|
||||
//! Assignment
|
||||
QgsPoint & operator=( const QgsPoint &other );
|
||||
|
||||
//! Calculates the vector obtained by subtracting a point from this point
|
||||
QgsVector operator-( const QgsPoint& p ) const { return QgsVector( m_x - p.m_x, m_y - p.m_y ); }
|
||||
|
||||
//! Adds a vector to this point in place
|
||||
QgsPoint &operator+=( QgsVector v ) { *this = *this + v; return *this; }
|
||||
|
||||
//! Subtracts a vector from this point in place
|
||||
QgsPoint &operator-=( QgsVector v ) { *this = *this - v; return *this; }
|
||||
|
||||
//! Adds a vector to this point
|
||||
QgsPoint operator+( QgsVector v ) const { return QgsPoint( m_x + v.x(), m_y + v.y() ); }
|
||||
|
||||
//! Subtracts a vector from this point
|
||||
QgsPoint operator-( QgsVector v ) const { return QgsPoint( m_x - v.x(), m_y - v.y() ); }
|
||||
|
||||
//! Multiplies the coordinates in this point by a scalar quantity
|
||||
QgsPoint operator*( double scalar ) const { return QgsPoint( m_x * scalar, m_y * scalar ); }
|
||||
|
||||
//! Divides the coordinates in this point by a scalar quantity
|
||||
QgsPoint operator/( double scalar ) const { return QgsPoint( m_x / scalar, m_y / scalar ); }
|
||||
|
||||
//! Multiplies the coordinates in this point by a scalar quantity in place
|
||||
QgsPoint &operator*=( double scalar ) { m_x *= scalar; m_y *= scalar; return *this; }
|
||||
|
||||
//! Divides the coordinates in this point by a scalar quantity in place
|
||||
QgsPoint &operator/=( double scalar ) { m_x /= scalar; m_y /= scalar; return *this; }
|
||||
|
||||
private:
|
||||
|
||||
//! x coordinate
|
||||
|
@ -38,8 +38,8 @@ void QgsGeometryAngleCheck::collectErrors( QList<QgsGeometryCheckError*>& errors
|
||||
QgsVector v21, v23;
|
||||
try
|
||||
{
|
||||
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normal();
|
||||
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normal();
|
||||
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normalized();
|
||||
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normalized();
|
||||
}
|
||||
catch ( const QgsException& )
|
||||
{
|
||||
@ -84,8 +84,8 @@ void QgsGeometryAngleCheck::fixError( QgsGeometryCheckError* error, int method,
|
||||
QgsVector v21, v23;
|
||||
try
|
||||
{
|
||||
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normal();
|
||||
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normal();
|
||||
v21 = QgsVector( p1.x() - p2.x(), p1.y() - p2.y() ).normalized();
|
||||
v23 = QgsVector( p3.x() - p2.x(), p3.y() - p2.y() ).normalized();
|
||||
}
|
||||
catch ( const QgsException& )
|
||||
{
|
||||
|
@ -75,7 +75,7 @@ namespace QgsGeomUtils
|
||||
QgsPointV2 p2 = geom1->vertexAt( QgsVertexId( iPart1, iRing1, jVert1 ) );
|
||||
double lambdap1 = 0.;
|
||||
double lambdap2 = qSqrt( QgsGeometryUtils::sqrDistance2D( p1, p2 ) );
|
||||
QgsVector d = QgsVector( p2.x() - p1.x(), p2.y() - p1.y() ).normal();
|
||||
QgsVector d = QgsVector( p2.x() - p1.x(), p2.y() - p1.y() ).normalized();
|
||||
|
||||
for ( int iPart2 = 0, nParts2 = geom2->partCount(); iPart2 < nParts2; ++iPart2 )
|
||||
{
|
||||
|
@ -38,6 +38,7 @@ class TestQgsPoint: public QObject
|
||||
void gettersSetters();
|
||||
void constructors();
|
||||
void toQPointF();
|
||||
void operators();
|
||||
void toString();
|
||||
void toDegreesMinutesSeconds();
|
||||
void toDegreesMinutesSecondsNoSuffix();
|
||||
@ -45,11 +46,11 @@ class TestQgsPoint: public QObject
|
||||
void toDegreesMinutes();
|
||||
void toDegreesMinutesNoSuffix();
|
||||
void toDegreesMinutesPadded();
|
||||
void wellKnownText();
|
||||
void sqrDist();
|
||||
void multiply();
|
||||
void onSegment();
|
||||
void distance();
|
||||
void compare();
|
||||
void project();
|
||||
void vector(); //tests for QgsVector
|
||||
|
||||
private:
|
||||
QgsPoint mPoint1;
|
||||
@ -129,6 +130,28 @@ void TestQgsPoint::toQPointF()
|
||||
QCOMPARE( result.y(), -20.0 );
|
||||
}
|
||||
|
||||
void TestQgsPoint::operators()
|
||||
{
|
||||
QgsPoint p( 1, 2 );
|
||||
QCOMPARE( p - QgsVector( 3, 5 ), QgsPoint( -2, -3 ) );
|
||||
p -= QgsVector( 3, 5 );
|
||||
QCOMPARE( p, QgsPoint( -2, -3 ) );
|
||||
|
||||
p = QgsPoint( 1, 2 );
|
||||
QCOMPARE( p + QgsVector( 3, 5 ), QgsPoint( 4, 7 ) );
|
||||
p += QgsVector( 3, 5 );
|
||||
QCOMPARE( p, QgsPoint( 4, 7 ) );
|
||||
|
||||
p = QgsPoint( 1, 2 );
|
||||
QCOMPARE( p * 3, QgsPoint( 3, 6 ) );
|
||||
p *= 3;
|
||||
QCOMPARE( p, QgsPoint( 3, 6 ) );
|
||||
|
||||
QCOMPARE( p / 3.0, QgsPoint( 1, 2 ) );
|
||||
p /= 3;
|
||||
QCOMPARE( p, QgsPoint( 1, 2 ) );
|
||||
}
|
||||
|
||||
void TestQgsPoint::initTestCase()
|
||||
{
|
||||
//
|
||||
@ -603,24 +626,32 @@ void TestQgsPoint::toDegreesMinutesPadded()
|
||||
QCOMPARE( QgsPoint( -0.000001, 0 ).toDegreesMinutes( 5, true, true ), myControlString );
|
||||
}
|
||||
|
||||
void TestQgsPoint::wellKnownText()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestQgsPoint::sqrDist()
|
||||
{
|
||||
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 2, 2 ) ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 2, 2 ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 3, 2 ) ), 4.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 3, 2 ), 4.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 1, 3 ) ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 1, 3 ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( QgsPoint( 1, 4 ) ), 4.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).sqrDist( 1, 4 ), 4.0 );
|
||||
QCOMPARE( QgsPoint( 1, -2 ).sqrDist( QgsPoint( 1, -4 ) ), 4.0 );
|
||||
QCOMPARE( QgsPoint( 1, -2 ).sqrDist( 1, -4 ), 4.0 );
|
||||
}
|
||||
|
||||
void TestQgsPoint::multiply()
|
||||
void TestQgsPoint::distance()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestQgsPoint::onSegment()
|
||||
{
|
||||
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 2, 2 ) ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( 2, 2 ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 3, 2 ) ), 2.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( 3, 2 ), 2.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 1, 3 ) ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( 1, 3 ), 1.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( QgsPoint( 1, 4 ) ), 2.0 );
|
||||
QCOMPARE( QgsPoint( 1, 2 ).distance( 1, 4 ), 2.0 );
|
||||
QCOMPARE( QgsPoint( 1, -2 ).distance( QgsPoint( 1, -4 ) ), 2.0 );
|
||||
QCOMPARE( QgsPoint( 1, -2 ).distance( 1, -4 ), 2.0 );
|
||||
}
|
||||
|
||||
void TestQgsPoint::compare()
|
||||
@ -634,5 +665,78 @@ void TestQgsPoint::compare()
|
||||
QVERIFY( point4.compare( QgsPoint( 10 / 3.0, 12 / 7.0 ) ) );
|
||||
}
|
||||
|
||||
void TestQgsPoint::project()
|
||||
{
|
||||
// test projecting a point
|
||||
QgsPoint p( 1, 2 );
|
||||
QVERIFY( p.project( 1, 0 ).compare( QgsPoint( 1, 3 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 1.5, 90 ).compare( QgsPoint( 2.5, 2 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 2, 180 ).compare( QgsPoint( 1, 0 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 5, 270 ).compare( QgsPoint( -4, 2 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 6, 360 ).compare( QgsPoint( 1, 8 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 5, 450 ).compare( QgsPoint( 6, 2 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( -1, 0 ).compare( QgsPoint( 1, 1 ), 0.0000000001 ) );
|
||||
QVERIFY( p.project( 1.5, -90 ).compare( QgsPoint( -0.5, 2 ), 0.0000000001 ) );
|
||||
}
|
||||
|
||||
void TestQgsPoint::vector()
|
||||
{
|
||||
//test constructors, x(), y() accessors
|
||||
QgsVector v1;
|
||||
QCOMPARE( v1.x(), 0.0 );
|
||||
QCOMPARE( v1.y(), 0.0 );
|
||||
QgsVector v2( 1.0, 2.0 );
|
||||
QCOMPARE( v2.x(), 1.0 );
|
||||
QCOMPARE( v2.y(), 2.0 );
|
||||
|
||||
// operator-
|
||||
QCOMPARE(( -v2 ).x(), -1.0 );
|
||||
QCOMPARE(( -v2 ).y(), -2.0 );
|
||||
|
||||
// operator*
|
||||
QCOMPARE(( v2 * 2.0 ).x(), 2.0 );
|
||||
QCOMPARE(( v2 * 2.0 ).y(), 4.0 );
|
||||
|
||||
// operator/
|
||||
QCOMPARE(( v2 / 2.0 ).x(), 0.5 );
|
||||
QCOMPARE(( v2 / 2.0 ).y(), 1.0 );
|
||||
|
||||
// QgsVector * QgsVector
|
||||
QCOMPARE(( v2 * v2 ), 5.0 );
|
||||
|
||||
// length
|
||||
QCOMPARE( v1.length(), 0.0 );
|
||||
QVERIFY( qgsDoubleNear( v2.length(), sqrt( 5 ), 0.000000001 ) );
|
||||
|
||||
// perpVector
|
||||
QCOMPARE( QgsVector( 2, 3 ).perpVector().x(), -3.0 );
|
||||
QCOMPARE( QgsVector( 2, 3 ).perpVector().y(), 2.0 );
|
||||
|
||||
// angle
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle(), M_PI_2, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle(), 0, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( -1, 0 ).angle(), M_PI, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, -1 ).angle(), 3 * M_PI_2, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 0 ).angle(), 0, 0.0000001 ) );
|
||||
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle( QgsVector( 0, 1 ) ), 0, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle( QgsVector( 0, 1 ) ), M_PI_2, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).angle( QgsVector( -1, 0 ) ), M_PI_2, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 1, 0 ).angle( QgsVector( -1, 0 ) ), M_PI, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( -1, 0 ).angle( QgsVector( 0, 0 ) ), -M_PI, 0.0000001 ) );
|
||||
|
||||
// rotateBy
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI_2 ).x(), -1.0, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI_2 ).y(), 0.0, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI ).x(), 0.0, 0.0000001 ) );
|
||||
QVERIFY( qgsDoubleNear( QgsVector( 0, 1 ).rotateBy( M_PI ).y(), -1.0, 0.0000001 ) );
|
||||
|
||||
// normalized
|
||||
QCOMPARE( QgsVector( 0, 2 ).normalized().x(), 0.0 );
|
||||
QCOMPARE( QgsVector( 0, 2 ).normalized().y(), 1.0 );
|
||||
QCOMPARE( QgsVector( 2, 0 ).normalized().x(), 1.0 );
|
||||
QCOMPARE( QgsVector( 2, 0 ).normalized().y(), 0.0 );
|
||||
}
|
||||
|
||||
QTEST_MAIN( TestQgsPoint )
|
||||
#include "testqgspoint.moc"
|
||||
|
Loading…
x
Reference in New Issue
Block a user