kig
coordinate.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "coordinate.h"
00019
00020 #include <qglobal.h>
00021 #include <cmath>
00022 #include <kdebug.h>
00023
00024 #include "common.h"
00025
00026 using namespace std;
00027
00028 Coordinate Coordinate::fromQPoint( const QPoint& p )
00029 {
00030 return Coordinate( p.x(), p.y() );
00031 }
00032
00033 QDebug& operator<<( QDebug& s, const Coordinate& t )
00034 {
00035 s << "x:" << t.x << "y:" << t.y;
00036 return s;
00037 }
00038
00039 const Coordinate operator+ ( const Coordinate& a, const Coordinate& b )
00040 {
00041 return Coordinate ( a.x + b.x, a.y + b.y );
00042 }
00043
00044 const Coordinate operator- ( const Coordinate& a, const Coordinate& b )
00045 {
00046 return Coordinate ( a.x - b.x, a.y - b.y );
00047 }
00048
00049 const Coordinate operator* ( const Coordinate& a, double r )
00050 {
00051 return Coordinate ( r*a.x, r*a.y );
00052 }
00053
00054 const Coordinate operator* ( double r, const Coordinate& a )
00055 {
00056 return Coordinate ( r*a.x, r*a.y );
00057 }
00058
00059 const Coordinate operator/ ( const Coordinate& a, double r )
00060 {
00061 return Coordinate ( a.x/r, a.y/r );
00062 }
00063
00064 bool operator==( const Coordinate& a, const Coordinate& b )
00065 {
00066 return a.x == b.x && a.y == b.y;
00067 }
00068
00069 bool operator!=( const Coordinate& a, const Coordinate& b )
00070 {
00071 return !operator==( a, b );
00072 }
00073
00074 Coordinate::Coordinate()
00075 : x(0),
00076 y(0)
00077 {
00078 }
00079
00080 Coordinate::Coordinate( double nx, double ny )
00081 : x( nx ),
00082 y( ny )
00083 {
00084 }
00085
00086 Coordinate::Coordinate( const Coordinate& p )
00087 : x( p.x ),
00088 y( p.y )
00089 {
00090 }
00091
00092 const Coordinate Coordinate::operator-() const
00093 {
00094 return Coordinate ( -x, -y );
00095 }
00096
00097 Coordinate& Coordinate::operator=( const Coordinate& p )
00098 {
00099 x = p.x;
00100 y = p.y;
00101 return *this;
00102 }
00103
00104 Coordinate& Coordinate::operator+=( const Coordinate& p )
00105 {
00106 x += p.x;
00107 y += p.y;
00108 return *this;
00109 }
00110
00111 Coordinate& Coordinate::operator-=( const Coordinate& p )
00112 {
00113 x -= p.x;
00114 y -= p.y;
00115 return *this;
00116 }
00117
00118 Coordinate& Coordinate::operator*=( double r )
00119 {
00120 x *= r;
00121 y *= r;
00122 return *this;
00123 }
00124
00125 Coordinate& Coordinate::operator*=( int r )
00126 {
00127 x *= r;
00128 y *= r;
00129 return *this;
00130 }
00131
00132 Coordinate& Coordinate::operator/=( double r )
00133 {
00134 x /= r;
00135 y /= r;
00136 return *this;
00137 }
00138
00139 double Coordinate::distance( const Coordinate& p ) const
00140 {
00141 return (p - *this).length();
00142 }
00143
00144 double Coordinate::length() const
00145 {
00146 return sqrt(x*x+y*y);
00147 }
00148
00149 const Coordinate Coordinate::orthogonal() const
00150 {
00151 return Coordinate( -y, x );
00152 }
00153
00154 const Coordinate Coordinate::normalize( double l ) const
00155 {
00156 double oldlength = length();
00157 return ( *this * l ) / oldlength;
00158 }
00159
00160 const Coordinate Coordinate::round() const
00161 {
00162 return Coordinate( qRound( x ), qRound( y ) );
00163 }
00164
00165 QPoint Coordinate::toQPoint() const
00166 {
00167 Coordinate t = round();
00168 return QPoint( (int) t.x, (int) t.y );
00169 }
00170
00171 Coordinate Coordinate::invalidCoord()
00172 {
00173 return Coordinate( double_inf, double_inf );
00174 }
00175
00176 bool Coordinate::valid() const
00177 {
00178 return abs( x ) != double_inf && abs( y ) != double_inf;
00179 }
00180
00181 double operator*( const Coordinate& a, const Coordinate& b )
00182 {
00183 return a.x * b.x + a.y * b.y;
00184 }