22 #include <QTextStream> 50 return QgsVector( mX * scalar, mY * scalar );
55 return *
this * ( 1.0 / scalar );
60 return mX * v.mX + mY * v.mY;
65 return sqrt( mX * mX + mY * mY );
85 double ang = atan2( mY, mX );
86 return ang < 0.0 ? ang + 2.0 *
M_PI : ang;
96 double ang = atan2( mY, mX ) + rot;
98 return QgsVector( len * cos( ang ), len * sin( ang ) );
112 throw QgsException(
"normalized vector of null vector undefined" );
139 ot << m_x <<
", " << m_y;
153 double myWrappedX = fmod( m_x, 360.0 );
155 if ( myWrappedX > 180.0 )
157 myWrappedX = myWrappedX - 360.0;
159 else if ( myWrappedX < -180.0 )
161 myWrappedX = myWrappedX + 360.0;
165 double myWrappedY = fmod( m_y, 180.0 );
167 if ( myWrappedY > 90.0 )
169 myWrappedY = myWrappedY - 180.0;
171 else if ( myWrappedY < -90.0 )
173 myWrappedY = myWrappedY + 180.0;
176 int myDegreesX = int( qAbs( myWrappedX ) );
177 double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
178 int myIntMinutesX = int( myFloatMinutesX );
179 double mySecondsX = double( myFloatMinutesX - myIntMinutesX ) * 60;
181 int myDegreesY = int( qAbs( myWrappedY ) );
182 double myFloatMinutesY = double(( qAbs( myWrappedY ) - myDegreesY ) * 60 );
183 int myIntMinutesY = int( myFloatMinutesY );
184 double mySecondsY = double( myFloatMinutesY - myIntMinutesY ) * 60;
187 if ( qRound( mySecondsX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
189 mySecondsX = qMax( mySecondsX - 60, 0.0 );
191 if ( myIntMinutesX >= 60 )
197 if ( qRound( mySecondsY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
199 mySecondsY = qMax( mySecondsY - 60, 0.0 );
201 if ( myIntMinutesY >= 60 )
219 if ( myWrappedX < 0 )
223 if ( myWrappedY < 0 )
230 if ( myDegreesX == 0 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
235 if ( myDegreesY == 0 && myIntMinutesY == 0 && qRound( mySecondsY * pow( 10.0, thePrecision ) ) == 0 )
241 if ( myDegreesX == 180 && myIntMinutesX == 0 && qRound( mySecondsX * pow( 10.0, thePrecision ) ) == 0 )
249 int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision );
254 myMinutesX +
QChar( 0x2032 ) +
255 myStrSecondsX +
QChar( 0x2033 ) +
256 myXHemisphere +
',' +
258 myMinutesY +
QChar( 0x2032 ) +
259 myStrSecondsY +
QChar( 0x2033 ) +
267 double myWrappedX = fmod( m_x, 360.0 );
269 if ( myWrappedX > 180.0 )
271 myWrappedX = myWrappedX - 360.0;
273 else if ( myWrappedX < -180.0 )
275 myWrappedX = myWrappedX + 360.0;
278 int myDegreesX = int( qAbs( myWrappedX ) );
279 double myFloatMinutesX = double(( qAbs( myWrappedX ) - myDegreesX ) * 60 );
281 int myDegreesY = int( qAbs( m_y ) );
282 double myFloatMinutesY = double(( qAbs( m_y ) - myDegreesY ) * 60 );
285 if ( qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
287 myFloatMinutesX = qMax( myFloatMinutesX - 60, 0.0 );
290 if ( qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) >= 60 * pow( 10.0, thePrecision ) )
292 myFloatMinutesY = qMax( myFloatMinutesY - 60, 0.0 );
307 if ( myWrappedX < 0 )
318 if ( myDegreesX == 0 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
323 if ( myDegreesY == 0 && qRound( myFloatMinutesY * pow( 10.0, thePrecision ) ) == 0 )
329 if ( myDegreesX == 180 && qRound( myFloatMinutesX * pow( 10.0, thePrecision ) ) == 0 )
335 int digits = 2 + ( thePrecision == 0 ? 0 : 1 + thePrecision );
340 myStrMinutesX +
QChar( 0x2032 ) +
341 myXHemisphere +
',' +
343 myStrMinutesY +
QChar( 0x2032 ) +
355 return ( m_x - x ) * ( m_x -
x ) + ( m_y - y ) * ( m_y -
y );
360 return sqrDist( other.
x(), other.
y() );
365 return sqrt( sqrDist( x, y ) );
370 return sqrt( sqrDist( other ) );
375 double dx = other.
x() - m_x;
376 double dy = other.
y() - m_y;
377 return ( atan2( dx, dy ) * 180.0 /
M_PI );
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 );
412 if ( &other !=
this )
431 qAbs(( b.
y() - a.
y() ) *( m_x - a.
x() ) - ( m_y - a.
y() ) *( b.
x() - a.
x() ) )
432 >= qMax( qAbs( b.
x() - a.
x() ), qAbs( b.
y() - a.
y() ) )
437 if (( b.
x() < a.
x() && a.
x() < m_x ) || ( b.
y() < a.
y() && a.
y() < m_y ) )
441 if (( m_x < a.
x() && a.
x() < b.
x() ) || ( m_y < a.
y() && a.
y() < b.
y() ) )
445 if (( a.
x() < b.
x() && b.
x() < m_x ) || ( a.
y() < b.
y() && b.
y() < m_y ) )
449 if (( m_x < b.
x() && b.
x() < a.
x() ) || ( m_y < b.
y() && b.
y() < a.
y() ) )
465 t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
469 minDistPoint.
setX( x1 );
470 minDistPoint.
setY( y1 );
474 minDistPoint.
setX( x2 );
475 minDistPoint.
setY( y2 );
479 minDistPoint.
setX( x1 + t *( x2 - x1 ) );
480 minDistPoint.
setY( y1 + t *( y2 - y1 ) );
483 double dist = sqrDist( minDistPoint );
487 minDistPoint.
setX( m_x );
488 minDistPoint.
setY( m_y );
QgsVector()
Default constructor for QgsVector.
QPointF toQPointF() const
Converts a point to a QPointF.
void setRealNumberPrecision(int precision)
double distance(double x, double y) const
Returns the distance between this point and a specified x, y coordinate.
void multiply(double scalar)
Multiply x and y by the given value.
double azimuth(const QgsPoint &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north) ...
QgsPoint & operator=(const QgsPoint &other)
Assignment.
QString toDegreesMinutes(int thePrecision, const bool useSuffix=true, const bool padded=false) const
Return a string representation as degrees minutes.
QString tr(const char *sourceText, const char *disambiguation, int n)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
double y() const
Get the y value of the point.
QgsPoint()
Default constructor.
QString number(int n, int base)
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPoint &minDistPoint, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
bool compare(const QgsPoint &other, double epsilon=4 *DBL_EPSILON) const
Compares this point with another point with a fuzzy tolerance.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
bool operator==(const QgsPoint &other)
equality operator
QString toDegreesMinutesSeconds(int thePrecision, const bool useSuffix=true, const bool padded=false) const
Return a string representation as degrees minutes seconds.
A class to represent a point.
QgsVector operator/(double scalar) const
Returns a vector where the components have been divided by a scalar value.
QString toString() const
String representation of the point (x,y)
double length() const
Returns the length of the vector.
Q_DECL_DEPRECATED QgsVector normal() const
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0).
A class to represent a vector.
void setX(double x)
Sets the x value of the point.
void setY(double y)
Sets the y value of the point.
double angle() const
Returns the angle of the vector in radians.
QString wellKnownText() const
Return the well known text representation for the point.
QgsVector perpVector() const
Returns the perpendicular vector to this vector (rotated 90 degrees counter-clockwise) ...
QgsVector operator*(double scalar) const
Returns a vector where the components have been multiplied by a scalar value.
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
QgsVector rotateBy(double rot) const
Rotates the vector by a specified angle.
double x() const
Returns the vector's x-component.
QgsVector operator-() const
Swaps the sign of the x and y components of the vector.
QgsPoint project(double distance, double bearing) const
Returns a new point which correponds to this point projected by a specified distance in a specified b...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
double y() const
Returns the vector's y-component.
Defines a qgis exception class.
double x() const
Get the x value of the point.
bool operator!=(const QgsPoint &other) const
Inequality operator.
QgsVector normalized() const
Returns the vector's normalized (or "unit") vector (ie same angle but length of 1.0).
int onSegment(const QgsPoint &a, const QgsPoint &b) const
Test if this point is on the segment defined by points a, b.