linear_segment.cpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Includes
00006 *****************************************************************************/
00007 
00008 #include "../../include/ecl/geometry/linear_segment.hpp"
00009 
00010 /*****************************************************************************
00011 ** Namespaces
00012 *****************************************************************************/
00013 
00014 namespace ecl {
00015 
00016 /*****************************************************************************
00017 ** Implementation
00018 *****************************************************************************/
00019 
00020 LinearSegment::LinearSegment(const double& x_1,
00021                                const double& y_1,
00022                                const double& x_2,
00023                                const double& y_2)
00024 : x_1(x_1), y_1(y_1)
00025 , x_2(x_2), y_2(y_2)
00026 {
00027   if ( x_2 == x_1 ) {
00028     B = 0; A = 1; C = x_1;
00029   } else {
00030     B = 1;
00031     A = -1*(y_2-y_1)/(x_2-x_1);
00032     C = -y_1 - x_1*A;
00033   }
00034 }
00035 
00036 double LinearSegment::squaredDistanceFromPoint(const double& x, const double& y) const
00037 {
00038   // pp1 = [ x - x1 ]     p2p1 = [ x2 - x1 ]
00039   //       [ y - y1 ]            [ y2 - y1 ]
00040   // The dot product of the above two vectors will give a hint as to where
00041   // the point lies in relation to the segment
00042   double dot = (x-x_1)*(x_2-x_1) + (y-y_1)*(y_2-y_1);
00043   double squared_length_p2p1 = (x_2-x_1)*(x_2-x_1) + (y_2-y_1)*(y_2-y_1);
00044   double t = -1;
00045   if ( squared_length_p2p1 != 0 ) { // special case handling
00046     t = dot / squared_length_p2p1;
00047   }
00048   double xx, yy;
00049   if ( t < 0 ) {
00050     xx = x_1; yy = y_1;
00051   } else if ( t > 1 ) {
00052     xx = x_2; yy = y_2;
00053   } else {
00054     xx = x_1 + t*(x_2-x_1);
00055     yy = y_1 + t*(y_2-y_1);
00056   }
00057   return (x - xx)*(x - xx) + (y - yy)*(y - yy);
00058 }
00059 
00060 /*****************************************************************************
00061  ** Trailers
00062  *****************************************************************************/
00063 
00064 } // namespace ecl


ecl_geometry
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:51