Point2.cpp

Go to the documentation of this file.
00001 #include "Point2.hpp"
00002 #include "Point3.hpp"
00003 #include "../math/CoorT.hpp"
00004 
00005 namespace se_core {
00006 
00007     coor_t Point2
00008     ::distance(const Point2& p1) const {
00009         return CoorT::sqrt(distanceSquared(p1));
00010     }
00011 
00012     coor_t Point2
00013     ::distanceL1(const Point2& p1) const {
00014         return CoorT::abs(x_-p1.x_) + CoorT::abs(y_-p1.y_);
00015     }
00016 
00017 
00018     coor_t Point2
00019     ::distanceLinf(const Point2& p1) const {
00020         return CoorT::max(CoorT::abs(x_-p1.x_), CoorT::abs(y_-p1.y_));
00021     }
00022 
00023 
00024     void Point2
00025     ::nearestPoint(const Point2& pt1, const Point2& pt2, const Point2& testPoint) {
00026         Vector2 A, u;
00027         A.sub(testPoint, pt1);
00028         u.sub(pt2, pt1);
00029 
00030         // Normalize u
00031         coor_t len = u.length();
00032         u.scale(1 / len);
00033 
00034         // Nearest point
00035         scale_t s = A.dot(u);
00036         // Clamp point between pt1 and pt2
00037         if(s < 0)
00038             s = 0;
00039         else if(s > len)
00040             s = len;
00041 
00042         // Calc point
00043         scale(s, u);
00044         add(pt1);
00045     }
00046 
00047     /*
00048     void Point2
00049     ::lineIntersect(const Point2& p0
00050                        , const Point2& p1
00051                        , const Point2& p2
00052                        , const Point2& p3) {
00053         // this function computes the intersection of the sent lines
00054         // and returns the intersection point, note that the function assumes
00055         // the lines intersect. the function can handle vertical as well
00056         // as horizontal lines. note the function isn't very clever, it simply
00057         //applies the math, but we don't need speed since this is a
00058         //pre-processing step
00059 
00060         // b1 and b2 unused
00061         float a1, c1, // constants of linear equations
00062             a2, c2,
00063             det_inv,  // the inverse of the determinant of the coefficie matrix
00064             m1, m2;    // the slopes of each line
00065 
00066         // compute slopes, note the cludge for infinity, however, this will
00067         // be close enough
00068 
00069         if ((p1.x_ - p0.x_) != 0)
00070             m1 = (p1.y_ - p0.y_) / (p1.x_ - p0.x_);
00071         else
00072             m1 = (float)1e+10;   // close enough to infinity
00073 
00074         if ((p3.x_ - p2.x_) != 0)
00075             m2 = (p3.y_ - p2.y_) / (p3.x_ - p2.x_);
00076         else
00077             m2 = (float)1e+10;   // close enough to infinity
00078 
00079         // compute constants
00080         a1 = m1;
00081         a2 = m2;
00082 
00083         c1 = (p0.y_ - m1 * p0.x_);
00084         c2 = (p2.y_ - m2 * p2.x_);
00085 
00086         // compute the inverse of the determinate
00087         det_inv = 1 / (a2 - a1);
00088 
00089         // use Kramers rule to compute xi and yi
00090         x_ = ((c1 - c2) * det_inv);
00091         y_ = m1 * ( (c2 - c1) / (m1 - m2) ) + c1;
00092     }
00093     */
00094 
00095 
00096     scale_t Point2
00097     ::lineIntersect(const Point2& a0
00098                        , const Point2& a1
00099                        , const Point2& b0
00100                        , const Point2& b1) {
00101         scale_t den = ((a1.x_-a0.x_) * (b1.y_-b0.y_)-(a1.y_-a0.y_) * (b1.x_-b0.x_));
00102         if(den == 0) {
00103             return -1;
00104         }
00105         scale_t r = ((a0.y_ - b0.y_) * (b1.x_ - b0.x_) - (a0.x_ - b0.x_) * (b1.y_ - b0.y_)) / den;
00106         scale_t s = ((a0.y_-b0.y_)*(a1.x_-a0.x_)-(a0.x_-b0.x_)*(a1.y_-a0.y_)) / den;
00107 
00108         if(s < 0 || s > 1 || r < 0 || r > 1) {
00109             return -1;
00110         }
00111 
00112         x_ = a0.x_ + r * (a1.x_ - a0.x_);
00113         y_ = a0.y_ + r * (a1.y_ - a0.y_);
00114 
00115         return r;
00116     }
00117 
00118 
00119     scale_t Point2
00120     ::lineIntersect(const Point3& a0
00121                        , const Point3& a1
00122                        , const Point3& b0
00123                        , const Point3& b1) {
00124         scale_t den = ((a1.x_-a0.x_) * (b1.z_-b0.z_)-(a1.z_-a0.z_) * (b1.x_-b0.x_));
00125         if(den == 0)
00126             return -1;
00127         scale_t r = ((a0.z_ - b0.z_) * (b1.x_ - b0.x_) - (a0.x_ - b0.x_) * (b1.z_ - b0.z_)) / den;
00128         scale_t s = ((a0.z_-b0.z_)*(a1.x_-a0.x_)-(a0.x_-b0.x_)*(a1.z_-a0.z_)) / den;
00129 
00130         if(s < 0 || s > 1 || r < 0 || r > 1) {
00131             return -1;
00132         }
00133 
00134         x_ = a0.x_ + r * (a1.x_ - a0.x_);
00135         y_ = a0.z_ + r * (a1.z_ - a0.z_);
00136 
00137         return r;
00138     }
00139 
00140 
00141     bool Point2
00142     ::willAIntersectB(const Point2& a0
00143                        , const Point2& a1
00144                        , const Point2& b0
00145                        , const Point2& b1) {
00146         scale_t den = ((a1.x_-a0.x_) * (b1.y_-b0.y_)-(a1.y_-a0.y_) * (b1.x_-b0.x_));
00147         if(den == 0) {
00148             return false;
00149         }
00150         scale_t r = ((a0.y_ - b0.y_) * (b1.x_ - b0.x_) - (a0.x_ - b0.x_) * (b1.y_ - b0.y_)) / den;
00151         scale_t s = ((a0.y_-b0.y_)*(a1.x_-a0.x_)-(a0.x_-b0.x_)*(a1.y_-a0.y_)) / den;
00152 
00153         if(s < 0 || s > 1 || r < 0) {
00154             return false;
00155         }
00156 
00157         x_ = a0.x_ + r * (a1.x_ - a0.x_);
00158         y_ = a0.y_ + r * (a1.y_ - a0.y_);
00159 
00160         return true;
00161     }
00162 
00163 
00164     bool Point2
00165     ::willAIntersectB(const Point3& a0
00166                        , const Point3& a1
00167                        , const Point3& b0
00168                        , const Point3& b1
00169                        , float tolerance) {
00170         scale_t den = ((a1.x_-a0.x_) * (b1.z_-b0.z_)-(a1.z_-a0.z_) * (b1.x_-b0.x_));
00171         if(den == 0) {
00172             return false;
00173         }
00174         scale_t r = ((a0.z_ - b0.z_) * (b1.x_ - b0.x_) - (a0.x_ - b0.x_) * (b1.z_ - b0.z_)) / den;
00175         scale_t s = ((a0.z_-b0.z_)*(a1.x_-a0.x_)-(a0.x_-b0.x_)*(a1.z_-a0.z_)) / den;
00176 
00177         if(s < -tolerance || s > 1 + tolerance || r < 0) {
00178             return false;
00179         }
00180 
00181         x_ = a0.x_ + r * (a1.x_ - a0.x_);
00182         y_ = a0.z_ + r * (a1.z_ - a0.z_);
00183 
00184         return true;
00185     }
00186 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:13 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo