Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgspoint.cpp - description 00003 ------------------- 00004 begin : Sat Jun 22 2002 00005 copyright : (C) 2002 by Gary E.Sherman 00006 email : sherman at mrcc.com 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 00019 #include "qgspoint.h" 00020 #include "qgis.h" 00021 #include <cmath> 00022 #include <QTextStream> 00023 #include <QObject> // for tr() 00024 00025 #include "qgsexception.h" 00026 00027 // 00028 // QgsVector 00029 // 00030 00031 QgsVector::QgsVector() : m_x( 0.0 ), m_y( 0.0 ) 00032 { 00033 } 00034 00035 QgsVector::QgsVector( double x, double y ) : m_x( x ), m_y( y ) 00036 { 00037 } 00038 00039 QgsVector QgsVector::operator-( void ) const 00040 { 00041 return QgsVector( -m_x, -m_y ); 00042 } 00043 00044 QgsVector QgsVector::operator*( double scalar ) const 00045 { 00046 return QgsVector( m_x * scalar, m_y * scalar ); 00047 } 00048 00049 QgsVector QgsVector::operator/( double scalar ) const 00050 { 00051 return *this * ( 1.0 / scalar ); 00052 } 00053 00054 double QgsVector::operator*( QgsVector v ) const 00055 { 00056 return m_x * v.m_x + m_y * v.m_y; 00057 } 00058 00059 double QgsVector::length() const 00060 { 00061 return sqrt( m_x * m_x + m_y * m_y ); 00062 } 00063 00064 double QgsVector::x() const 00065 { 00066 return m_x; 00067 } 00068 00069 double QgsVector::y() const 00070 { 00071 return m_y; 00072 } 00073 00074 // perpendicular vector (rotated 90� counter-clockwise) 00075 QgsVector QgsVector::perpVector() const 00076 { 00077 return QgsVector( -m_y, m_x ); 00078 } 00079 00080 double QgsVector::angle( void ) const 00081 { 00082 double ang = atan2( m_y, m_x ); 00083 return ang < 0.0 ? ang + 2.0 * M_PI : ang; 00084 } 00085 00086 double QgsVector::angle( QgsVector v ) const 00087 { 00088 return v.angle() - angle(); 00089 } 00090 00091 QgsVector QgsVector::rotateBy( double rot ) const 00092 { 00093 double ang = atan2( m_y, m_x ) + rot; 00094 double len = length(); 00095 return QgsVector( len * cos( ang ), len * sin( ang ) ); 00096 } 00097 00098 QgsVector QgsVector::normal() const 00099 { 00100 double len = length(); 00101 00102 if ( len == 0.0 ) 00103 { 00104 throw QgsException( "normal vector of null vector undefined" ); 00105 } 00106 00107 return *this / len; 00108 } 00109 00110 00111 // 00112 // QgsPoint 00113 // 00114 00115 QgsPoint::QgsPoint( const QgsPoint& p ) 00116 { 00117 m_x = p.x(); 00118 m_y = p.y(); 00119 } 00120 00121 QString QgsPoint::toString() const 00122 { 00123 QString rep; 00124 QTextStream ot( &rep ); 00125 ot.setRealNumberPrecision( 12 ); 00126 ot << m_x << ", " << m_y; 00127 return rep; 00128 } 00129 00130 QString QgsPoint::toString( int thePrecision ) const 00131 { 00132 QString x = qIsFinite( m_x ) ? QString::number( m_x, 'f', thePrecision ) : QObject::tr( "infinite" ); 00133 QString y = qIsFinite( m_y ) ? QString::number( m_y, 'f', thePrecision ) : QObject::tr( "infinite" ); 00134 return QString( "%1,%2" ).arg( x ).arg( y ); 00135 } 00136 00137 QString QgsPoint::toDegreesMinutesSeconds( int thePrecision ) const 00138 { 00139 int myDegreesX = int( qAbs( m_x ) ); 00140 float myFloatMinutesX = float(( qAbs( m_x ) - myDegreesX ) * 60 ); 00141 int myIntMinutesX = int( myFloatMinutesX ); 00142 float mySecondsX = float( myFloatMinutesX - myIntMinutesX ) * 60; 00143 00144 int myDegreesY = int( qAbs( m_y ) ); 00145 float myFloatMinutesY = float(( qAbs( m_y ) - myDegreesY ) * 60 ); 00146 int myIntMinutesY = int( myFloatMinutesY ); 00147 float mySecondsY = float( myFloatMinutesY - myIntMinutesY ) * 60; 00148 00149 QString myXHemisphere = m_x < 0 ? QObject::tr( "W" ) : QObject::tr( "E" ); 00150 QString myYHemisphere = m_y < 0 ? QObject::tr( "S" ) : QObject::tr( "N" ); 00151 QString rep = QString::number( myDegreesX ) + QChar( 176 ) + 00152 QString::number( myIntMinutesX ) + QString( "'" ) + 00153 QString::number( mySecondsX, 'f', thePrecision ) + QString( "\"" ) + 00154 myXHemisphere + QString( "," ) + 00155 QString::number( myDegreesY ) + QChar( 176 ) + 00156 QString::number( myIntMinutesY ) + QString( "'" ) + 00157 QString::number( mySecondsY, 'f', thePrecision ) + QString( "\"" ) + 00158 myYHemisphere; 00159 return rep; 00160 } 00161 00162 QString QgsPoint::wellKnownText() const 00163 { 00164 return QString( "POINT(%1 %2)" ).arg( QString::number( m_x, 'f', 18 ) ).arg( QString::number( m_y, 'f', 18 ) ); 00165 } 00166 00167 double QgsPoint::sqrDist( double x, double y ) const 00168 { 00169 return ( m_x - x ) * ( m_x - x ) + ( m_y - y ) * ( m_y - y ); 00170 } 00171 00172 double QgsPoint::sqrDist( const QgsPoint& other ) const 00173 { 00174 return sqrDist( other.x(), other.y() ); 00175 } 00176 00177 double QgsPoint::azimuth( const QgsPoint& other ) 00178 { 00179 double dx = other.x() - m_x; 00180 double dy = other.y() - m_y; 00181 return ( atan2( dx, dy ) * 180.0 / M_PI ); 00182 } 00183 00184 // operators 00185 bool QgsPoint::operator==( const QgsPoint & other ) 00186 { 00187 if (( m_x == other.x() ) && ( m_y == other.y() ) ) 00188 return true; 00189 else 00190 return false; 00191 } 00192 00193 bool QgsPoint::operator!=( const QgsPoint & other ) const 00194 { 00195 if (( m_x == other.x() ) && ( m_y == other.y() ) ) 00196 return false; 00197 else 00198 return true; 00199 } 00200 00201 QgsPoint & QgsPoint::operator=( const QgsPoint & other ) 00202 { 00203 if ( &other != this ) 00204 { 00205 m_x = other.x(); 00206 m_y = other.y(); 00207 } 00208 00209 return *this; 00210 } 00211 00212 void QgsPoint::multiply( const double& scalar ) 00213 { 00214 m_x *= scalar; 00215 m_y *= scalar; 00216 } 00217 00218 int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const 00219 { 00220 //algorithm from 'graphics GEMS', A. Paeth: 'A Fast 2D Point-on-line test' 00221 if ( 00222 qAbs(( b.y() - a.y() ) *( m_x - a.x() ) - ( m_y - a.y() ) *( b.x() - a.x() ) ) 00223 >= qMax( qAbs( b.x() - a.x() ), qAbs( b.y() - a.y() ) ) 00224 ) 00225 { 00226 return 0; 00227 } 00228 if (( b.x() < a.x() && a.x() < m_x ) || ( b.y() < a.y() && a.y() < m_y ) ) 00229 { 00230 return 1; 00231 } 00232 if (( m_x < a.x() && a.x() < b.x() ) || ( m_y < a.y() && a.y() < b.y() ) ) 00233 { 00234 return 1; 00235 } 00236 if (( a.x() < b.x() && b.x() < m_x ) || ( a.y() < b.y() && b.y() < m_y ) ) 00237 { 00238 return 3; 00239 } 00240 if (( m_x < b.x() && b.x() < a.x() ) || ( m_y < b.y() && b.y() < a.y() ) ) 00241 { 00242 return 3; 00243 } 00244 00245 return 2; 00246 } 00247 00248 double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint, double epsilon ) const 00249 { 00250 double nx, ny; //normal vector 00251 00252 nx = y2 - y1; 00253 ny = -( x2 - x1 ); 00254 00255 double t; 00256 t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx ); 00257 00258 if ( t < 0.0 ) 00259 { 00260 minDistPoint.setX( x1 ); 00261 minDistPoint.setY( y1 ); 00262 } 00263 else if ( t > 1.0 ) 00264 { 00265 minDistPoint.setX( x2 ); 00266 minDistPoint.setY( y2 ); 00267 } 00268 else 00269 { 00270 minDistPoint.setX( x1 + t *( x2 - x1 ) ); 00271 minDistPoint.setY( y1 + t *( y2 - y1 ) ); 00272 } 00273 00274 double dist = sqrDist( minDistPoint ); 00275 //prevent rounding errors if the point is directly on the segment 00276 if ( doubleNear( dist, 0.0, epsilon ) ) 00277 { 00278 minDistPoint.setX( m_x ); 00279 minDistPoint.setY( m_y ); 00280 return 0.0; 00281 } 00282 return dist; 00283 }