lib/linear_segment.cpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Includes
6 *****************************************************************************/
7 
8 #include "../../include/ecl/geometry/linear_segment.hpp"
9 
10 /*****************************************************************************
11 ** Namespaces
12 *****************************************************************************/
13 
14 namespace ecl {
15 
16 /*****************************************************************************
17 ** Implementation
18 *****************************************************************************/
19 
20 LinearSegment::LinearSegment(const double& x_1,
21  const double& y_1,
22  const double& x_2,
23  const double& y_2)
24 : x_1(x_1), y_1(y_1)
25 , x_2(x_2), y_2(y_2)
26 {
27  if ( x_2 == x_1 ) {
28  B = 0; A = 1; C = x_1;
29  } else {
30  B = 1;
31  A = -1*(y_2-y_1)/(x_2-x_1);
32  C = -y_1 - x_1*A;
33  }
34 }
35 
36 double LinearSegment::squaredDistanceFromPoint(const double& x, const double& y) const
37 {
38  // pp1 = [ x - x1 ] p2p1 = [ x2 - x1 ]
39  // [ y - y1 ] [ y2 - y1 ]
40  // The dot product of the above two vectors will give a hint as to where
41  // the point lies in relation to the segment
42  double dot = (x-x_1)*(x_2-x_1) + (y-y_1)*(y_2-y_1);
43  double squared_length_p2p1 = (x_2-x_1)*(x_2-x_1) + (y_2-y_1)*(y_2-y_1);
44  double t = -1;
45  if ( squared_length_p2p1 != 0 ) { // special case handling
46  t = dot / squared_length_p2p1;
47  }
48  double xx, yy;
49  if ( t < 0 ) {
50  xx = x_1; yy = y_1;
51  } else if ( t > 1 ) {
52  xx = x_2; yy = y_2;
53  } else {
54  xx = x_1 + t*(x_2-x_1);
55  yy = y_1 + t*(y_2-y_1);
56  }
57  return (x - xx)*(x - xx) + (y - yy)*(y - yy);
58 }
59 
60 /*****************************************************************************
61  ** Trailers
62  *****************************************************************************/
63 
64 } // namespace ecl
ecl::LinearSegment::x_1
double x_1
Definition: linear_segment.hpp:66
ecl::LinearSegment::y_2
double y_2
Definition: linear_segment.hpp:67
ecl::LinearSegment::LinearSegment
LinearSegment(const double &x_1, const double &y_1, const double &x_2, const double &y_2)
Definition: lib/linear_segment.cpp:24
ecl::LinearSegment::x_2
double x_2
Definition: linear_segment.hpp:67
ecl::LinearSegment::squaredDistanceFromPoint
double squaredDistanceFromPoint(const double &x, const double &y) const
Distance of a point from the segment.
Definition: lib/linear_segment.cpp:40
ecl::LinearSegment::y_1
double y_1
Definition: linear_segment.hpp:66
dot
def dot(left, right)
ecl
Embedded control libraries.


ecl_geometry
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:39