line2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __IRR_LINE_2D_H_INCLUDED__
  5. #define __IRR_LINE_2D_H_INCLUDED__
  6. #include "irrTypes.h"
  7. #include "vector2d.h"
  8. namespace irr
  9. {
  10. namespace core
  11. {
  12. //! 2D line between two points with intersection methods.
  13. template <class T>
  14. class line2d
  15. {
  16. public:
  17. //! Default constructor for line going from (0,0) to (1,1).
  18. line2d() : start(0,0), end(1,1) {}
  19. //! Constructor for line between the two points.
  20. line2d(T xa, T ya, T xb, T yb) : start(xa, ya), end(xb, yb) {}
  21. //! Constructor for line between the two points given as vectors.
  22. line2d(const vector2d<T>& start, const vector2d<T>& end) : start(start), end(end) {}
  23. //! Copy constructor.
  24. line2d(const line2d<T>& other) : start(other.start), end(other.end) {}
  25. // operators
  26. line2d<T> operator+(const vector2d<T>& point) const { return line2d<T>(start + point, end + point); }
  27. line2d<T>& operator+=(const vector2d<T>& point) { start += point; end += point; return *this; }
  28. line2d<T> operator-(const vector2d<T>& point) const { return line2d<T>(start - point, end - point); }
  29. line2d<T>& operator-=(const vector2d<T>& point) { start -= point; end -= point; return *this; }
  30. bool operator==(const line2d<T>& other) const
  31. { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
  32. bool operator!=(const line2d<T>& other) const
  33. { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
  34. // functions
  35. //! Set this line to new line going through the two points.
  36. void setLine(const T& xa, const T& ya, const T& xb, const T& yb){start.set(xa, ya); end.set(xb, yb);}
  37. //! Set this line to new line going through the two points.
  38. void setLine(const vector2d<T>& nstart, const vector2d<T>& nend){start.set(nstart); end.set(nend);}
  39. //! Set this line to new line given as parameter.
  40. void setLine(const line2d<T>& line){start.set(line.start); end.set(line.end);}
  41. //! Get length of line
  42. /** \return Length of the line. */
  43. T getLength() const { return start.getDistanceFrom(end); }
  44. //! Get squared length of the line
  45. /** \return Squared length of line. */
  46. T getLengthSQ() const { return start.getDistanceFromSQ(end); }
  47. //! Get middle of the line
  48. /** \return center of the line. */
  49. vector2d<T> getMiddle() const
  50. {
  51. return (start + end)/(T)2;
  52. }
  53. //! Get the vector of the line.
  54. /** \return The vector of the line. */
  55. vector2d<T> getVector() const { return vector2d<T>(end.X - start.X, end.Y - start.Y); }
  56. //! Tests if this line intersects with another line.
  57. /** \param l: Other line to test intersection with.
  58. \param checkOnlySegments: Default is to check intersection between the begin and endpoints.
  59. When set to false the function will check for the first intersection point when extending the lines.
  60. \param out: If there is an intersection, the location of the
  61. intersection will be stored in this vector.
  62. \return True if there is an intersection, false if not. */
  63. bool intersectWith(const line2d<T>& l, vector2d<T>& out, bool checkOnlySegments=true) const
  64. {
  65. // Uses the method given at:
  66. // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  67. const f32 commonDenominator = (f32)(l.end.Y - l.start.Y)*(end.X - start.X) -
  68. (l.end.X - l.start.X)*(end.Y - start.Y);
  69. const f32 numeratorA = (f32)(l.end.X - l.start.X)*(start.Y - l.start.Y) -
  70. (l.end.Y - l.start.Y)*(start.X -l.start.X);
  71. const f32 numeratorB = (f32)(end.X - start.X)*(start.Y - l.start.Y) -
  72. (end.Y - start.Y)*(start.X -l.start.X);
  73. if(equals(commonDenominator, 0.f))
  74. {
  75. // The lines are either coincident or parallel
  76. // if both numerators are 0, the lines are coincident
  77. if(equals(numeratorA, 0.f) && equals(numeratorB, 0.f))
  78. {
  79. // Try and find a common endpoint
  80. if(l.start == start || l.end == start)
  81. out = start;
  82. else if(l.end == end || l.start == end)
  83. out = end;
  84. // now check if the two segments are disjunct
  85. else if (l.start.X>start.X && l.end.X>start.X && l.start.X>end.X && l.end.X>end.X)
  86. return false;
  87. else if (l.start.Y>start.Y && l.end.Y>start.Y && l.start.Y>end.Y && l.end.Y>end.Y)
  88. return false;
  89. else if (l.start.X<start.X && l.end.X<start.X && l.start.X<end.X && l.end.X<end.X)
  90. return false;
  91. else if (l.start.Y<start.Y && l.end.Y<start.Y && l.start.Y<end.Y && l.end.Y<end.Y)
  92. return false;
  93. // else the lines are overlapping to some extent
  94. else
  95. {
  96. // find the points which are not contributing to the
  97. // common part
  98. vector2d<T> maxp;
  99. vector2d<T> minp;
  100. if ((start.X>l.start.X && start.X>l.end.X && start.X>end.X) || (start.Y>l.start.Y && start.Y>l.end.Y && start.Y>end.Y))
  101. maxp=start;
  102. else if ((end.X>l.start.X && end.X>l.end.X && end.X>start.X) || (end.Y>l.start.Y && end.Y>l.end.Y && end.Y>start.Y))
  103. maxp=end;
  104. else if ((l.start.X>start.X && l.start.X>l.end.X && l.start.X>end.X) || (l.start.Y>start.Y && l.start.Y>l.end.Y && l.start.Y>end.Y))
  105. maxp=l.start;
  106. else
  107. maxp=l.end;
  108. if (maxp != start && ((start.X<l.start.X && start.X<l.end.X && start.X<end.X) || (start.Y<l.start.Y && start.Y<l.end.Y && start.Y<end.Y)))
  109. minp=start;
  110. else if (maxp != end && ((end.X<l.start.X && end.X<l.end.X && end.X<start.X) || (end.Y<l.start.Y && end.Y<l.end.Y && end.Y<start.Y)))
  111. minp=end;
  112. else if (maxp != l.start && ((l.start.X<start.X && l.start.X<l.end.X && l.start.X<end.X) || (l.start.Y<start.Y && l.start.Y<l.end.Y && l.start.Y<end.Y)))
  113. minp=l.start;
  114. else
  115. minp=l.end;
  116. // one line is contained in the other. Pick the center
  117. // of the remaining points, which overlap for sure
  118. out = core::vector2d<T>();
  119. if (start != maxp && start != minp)
  120. out += start;
  121. if (end != maxp && end != minp)
  122. out += end;
  123. if (l.start != maxp && l.start != minp)
  124. out += l.start;
  125. if (l.end != maxp && l.end != minp)
  126. out += l.end;
  127. out.X = (T)(out.X/2);
  128. out.Y = (T)(out.Y/2);
  129. }
  130. return true; // coincident
  131. }
  132. return false; // parallel
  133. }
  134. // Get the point of intersection on this line, checking that
  135. // it is within the line segment.
  136. const f32 uA = numeratorA / commonDenominator;
  137. if(checkOnlySegments && (uA < 0.f || uA > 1.f) )
  138. return false; // Outside the line segment
  139. const f32 uB = numeratorB / commonDenominator;
  140. if(checkOnlySegments && (uB < 0.f || uB > 1.f))
  141. return false; // Outside the line segment
  142. // Calculate the intersection point.
  143. out.X = (T)(start.X + uA * (end.X - start.X));
  144. out.Y = (T)(start.Y + uA * (end.Y - start.Y));
  145. return true;
  146. }
  147. //! Get unit vector of the line.
  148. /** \return Unit vector of this line. */
  149. vector2d<T> getUnitVector() const
  150. {
  151. T len = (T)(1.0 / getLength());
  152. return vector2d<T>((end.X - start.X) * len, (end.Y - start.Y) * len);
  153. }
  154. //! Get angle between this line and given line.
  155. /** \param l Other line for test.
  156. \return Angle in degrees. */
  157. f64 getAngleWith(const line2d<T>& l) const
  158. {
  159. vector2d<T> vect = getVector();
  160. vector2d<T> vect2 = l.getVector();
  161. return vect.getAngleWith(vect2);
  162. }
  163. //! Tells us if the given point lies to the left, right, or on the line.
  164. /** \return 0 if the point is on the line
  165. <0 if to the left, or >0 if to the right. */
  166. T getPointOrientation(const vector2d<T>& point) const
  167. {
  168. return ( (end.X - start.X) * (point.Y - start.Y) -
  169. (point.X - start.X) * (end.Y - start.Y) );
  170. }
  171. //! Check if the given point is a member of the line
  172. /** \return True if point is between start and end, else false. */
  173. bool isPointOnLine(const vector2d<T>& point) const
  174. {
  175. T d = getPointOrientation(point);
  176. return (d == 0 && point.isBetweenPoints(start, end));
  177. }
  178. //! Check if the given point is between start and end of the line.
  179. /** Assumes that the point is already somewhere on the line. */
  180. bool isPointBetweenStartAndEnd(const vector2d<T>& point) const
  181. {
  182. return point.isBetweenPoints(start, end);
  183. }
  184. //! Get the closest point on this line to a point
  185. /** \param checkOnlySegments: Default (true) is to return a point on the line-segment (between begin and end) of the line.
  186. When set to false the function will check for the first the closest point on the the line even when outside the segment. */
  187. vector2d<T> getClosestPoint(const vector2d<T>& point, bool checkOnlySegments=true) const
  188. {
  189. vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));
  190. vector2d<f64> v((f64)(end.X-start.X), (f64)(end.Y-start.Y));
  191. f64 d = v.getLength();
  192. if ( d == 0 ) // can't tell much when the line is just a single point
  193. return start;
  194. v /= d;
  195. f64 t = v.dotProduct(c);
  196. if ( checkOnlySegments )
  197. {
  198. if (t < 0) return vector2d<T>((T)start.X, (T)start.Y);
  199. if (t > d) return vector2d<T>((T)end.X, (T)end.Y);
  200. }
  201. v *= t;
  202. return vector2d<T>((T)(start.X + v.X), (T)(start.Y + v.Y));
  203. }
  204. //! Start point of the line.
  205. vector2d<T> start;
  206. //! End point of the line.
  207. vector2d<T> end;
  208. };
  209. // partial specialization to optimize <f32> lines (avoiding casts)
  210. template <>
  211. inline vector2df line2d<irr::f32>::getClosestPoint(const vector2df& point, bool checkOnlySegments) const
  212. {
  213. vector2df c = point - start;
  214. vector2df v = end - start;
  215. f32 d = (f32)v.getLength();
  216. if ( d == 0 ) // can't tell much when the line is just a single point
  217. return start;
  218. v /= d;
  219. f32 t = v.dotProduct(c);
  220. if ( checkOnlySegments )
  221. {
  222. if (t < 0) return start;
  223. if (t > d) return end;
  224. }
  225. v *= t;
  226. return start + v;
  227. }
  228. //! Typedef for an f32 line.
  229. typedef line2d<f32> line2df;
  230. //! Typedef for an integer line.
  231. typedef line2d<s32> line2di;
  232. } // end namespace core
  233. } // end namespace irr
  234. #endif