kivio
tkmath.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _TKVECTOR_H
00020 #define _TKVECTOR_H
00021
00022 #include <math.h>
00023
00024 class TKVec2
00025 {
00026 protected:
00027 double m_x, m_y;
00028
00029 public:
00030 TKVec2(const double &_x, const double &_y)
00031 { m_x = _x; m_y=_y; }
00032
00033 inline double dotProduct( const TKVec2 &v2 ) const
00034 { return m_x*v2.m_x + m_y*v2.m_y; }
00035
00036 inline double crossProduct( const TKVec2 &v2 ) const
00037 { return ( m_x*v2.m_y - m_y*v2.m_x ); }
00038
00039 inline double magnitude() const
00040 { return sqrt(m_x*m_x + m_y*m_y); }
00041
00042 inline void normalize()
00043 { double l=this->magnitude(); m_x/=l; m_y/=l; }
00044
00045 inline void scale( const double &sx, const double &sy )
00046 { m_x *= sx; m_y *= sy; }
00047
00048 inline void scale( const TKVec2 &v )
00049 { m_x *= v.m_x; m_y *= v.m_y; }
00050
00051 inline void scale( const double &s )
00052 { m_x*=s; m_y*=s; }
00053
00054 inline TKVec2 getPerpendicular() const
00055 { return TKVec2(-m_y, m_x); }
00056
00057 inline void reverse()
00058 { m_x*=-1.0f; m_y*=-1.0f; }
00059
00060 inline void rotate( const double &rad )
00061 { double newX, newY; newX= m_x*cos(rad) - m_y*sin(rad); newY=m_x*sin(rad)+m_y*cos(rad); m_x=newX; m_y=newY;}
00062
00063 inline void rotateDeg( const double ° )
00064 { double newX, newY, rad=deg*3.14159f/180.0f;
00065 newX= m_x*cos(rad) - m_y*sin(rad); newY=m_x*sin(rad)+m_y*cos(rad); m_x=newX; m_y=newY;}
00066
00067 inline void translate( const double &tx, const double &ty )
00068 { m_x += tx; m_y += ty; }
00069
00070 inline double angle( const TKVec2 &v )
00071 { return acos( (this->dotProduct(v)) / (this->magnitude() * v.magnitude()) ); }
00072
00073 inline double x() { return m_x; }
00074 inline double y() { return m_y; }
00075
00076 inline void setX(const double &_x) { m_x=_x; }
00077 inline void setY(const double &_y) { m_y=_y; }
00078
00079 inline void set(const double &_x, const double &_y) {m_x=_x; m_y=_y;}
00080 };
00081
00082 inline double shortestDistance(double sx, double sy, double ex, double ey, double px, double py )
00083 {
00084 TKVec2 u(sx-ex, sy-ey);
00085 TKVec2 pq(sx-px, sy-py);
00086
00087 double magTop = fabs( pq.crossProduct(u) );
00088 double magU = u.magnitude();
00089
00090
00091 if( magU==0.0f )
00092 {
00093 return -1.0f;
00094 }
00095
00096 return magTop / magU;
00097 }
00098
00099 inline bool collisionLine( double sx, double sy, double ex, double ey, double px, double py, double threshold )
00100 {
00101 double minx, miny, maxx, maxy;
00102
00103 if( sx < ex ) {
00104 minx = sx;
00105 maxx = ex;
00106 }
00107 else {
00108 minx = ex;
00109 maxx = sx;
00110 }
00111
00112 minx -= threshold;
00113 maxx += threshold;
00114
00115 if( sy < ey ) {
00116 miny = sy;
00117 maxy = ey;
00118 }
00119 else {
00120 miny = ey;
00121 maxy = sy;
00122 }
00123
00124 miny -= threshold;
00125 maxy += threshold;
00126
00127 if( !(px >= minx &&
00128 px <= maxx &&
00129 py >= miny &&
00130 py <= maxy ) )
00131 {
00132 return false;
00133 }
00134
00135 if( shortestDistance( sx, sy, ex, ey, px, py ) <= threshold )
00136 return true;
00137
00138 return false;
00139 }
00140
00141 #endif
|