Irrlicht 3D Engine
line2d.h
Go to the documentation of this file.
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 
5 #ifndef __IRR_LINE_2D_H_INCLUDED__
6 #define __IRR_LINE_2D_H_INCLUDED__
7 
8 #include "irrTypes.h"
9 #include "vector2d.h"
10 
11 namespace irr
12 {
13 namespace core
14 {
15 
17 template <class T>
18 class line2d
19 {
20  public:
22  line2d() : start(0,0), end(1,1) {}
24  line2d(T xa, T ya, T xb, T yb) : start(xa, ya), end(xb, yb) {}
26  line2d(const vector2d<T>& start, const vector2d<T>& end) : start(start), end(end) {}
28  line2d(const line2d<T>& other) : start(other.start), end(other.end) {}
29 
30  // operators
31 
32  line2d<T> operator+(const vector2d<T>& point) const { return line2d<T>(start + point, end + point); }
33  line2d<T>& operator+=(const vector2d<T>& point) { start += point; end += point; return *this; }
34 
35  line2d<T> operator-(const vector2d<T>& point) const { return line2d<T>(start - point, end - point); }
36  line2d<T>& operator-=(const vector2d<T>& point) { start -= point; end -= point; return *this; }
37 
38  bool operator==(const line2d<T>& other) const
39  { return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
40  bool operator!=(const line2d<T>& other) const
41  { return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
42 
43  // functions
45  void setLine(const T& xa, const T& ya, const T& xb, const T& yb){start.set(xa, ya); end.set(xb, yb);}
47  void setLine(const vector2d<T>& nstart, const vector2d<T>& nend){start.set(nstart); end.set(nend);}
49  void setLine(const line2d<T>& line){start.set(line.start); end.set(line.end);}
50 
52 
53  T getLength() const { return start.getDistanceFrom(end); }
54 
56 
57  T getLengthSQ() const { return start.getDistanceFromSQ(end); }
58 
60 
62  {
63  return (start + end)/(T)2;
64  }
65 
67 
68  vector2d<T> getVector() const { return vector2d<T>(end.X - start.X, end.Y - start.Y); }
69 
71 
77  bool intersectWith(const line2d<T>& l, vector2d<T>& out, bool checkOnlySegments=true) const
78  {
79  // Uses the method given at:
80  // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
81  const f32 commonDenominator = (f32)(l.end.Y - l.start.Y)*(end.X - start.X) -
82  (l.end.X - l.start.X)*(end.Y - start.Y);
83 
84  const f32 numeratorA = (f32)(l.end.X - l.start.X)*(start.Y - l.start.Y) -
85  (l.end.Y - l.start.Y)*(start.X -l.start.X);
86 
87  const f32 numeratorB = (f32)(end.X - start.X)*(start.Y - l.start.Y) -
88  (end.Y - start.Y)*(start.X -l.start.X);
89 
90  if(equals(commonDenominator, 0.f))
91  {
92  // The lines are either coincident or parallel
93  // if both numerators are 0, the lines are coincident
94  if(equals(numeratorA, 0.f) && equals(numeratorB, 0.f))
95  {
96  // Try and find a common endpoint
97  if(l.start == start || l.end == start)
98  out = start;
99  else if(l.end == end || l.start == end)
100  out = end;
101  // now check if the two segments are disjunct
102  else if (l.start.X>start.X && l.end.X>start.X && l.start.X>end.X && l.end.X>end.X)
103  return false;
104  else if (l.start.Y>start.Y && l.end.Y>start.Y && l.start.Y>end.Y && l.end.Y>end.Y)
105  return false;
106  else if (l.start.X<start.X && l.end.X<start.X && l.start.X<end.X && l.end.X<end.X)
107  return false;
108  else if (l.start.Y<start.Y && l.end.Y<start.Y && l.start.Y<end.Y && l.end.Y<end.Y)
109  return false;
110  // else the lines are overlapping to some extent
111  else
112  {
113  // find the points which are not contributing to the
114  // common part
115  vector2d<T> maxp;
116  vector2d<T> minp;
117  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))
118  maxp=start;
119  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))
120  maxp=end;
121  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))
122  maxp=l.start;
123  else
124  maxp=l.end;
125  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)))
126  minp=start;
127  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)))
128  minp=end;
129  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)))
130  minp=l.start;
131  else
132  minp=l.end;
133 
134  // one line is contained in the other. Pick the center
135  // of the remaining points, which overlap for sure
136  out = core::vector2d<T>();
137  if (start != maxp && start != minp)
138  out += start;
139  if (end != maxp && end != minp)
140  out += end;
141  if (l.start != maxp && l.start != minp)
142  out += l.start;
143  if (l.end != maxp && l.end != minp)
144  out += l.end;
145  out.X = (T)(out.X/2);
146  out.Y = (T)(out.Y/2);
147  }
148 
149  return true; // coincident
150  }
151 
152  return false; // parallel
153  }
154 
155  // Get the point of intersection on this line, checking that
156  // it is within the line segment.
157  const f32 uA = numeratorA / commonDenominator;
158  if(checkOnlySegments && (uA < 0.f || uA > 1.f) )
159  return false; // Outside the line segment
160 
161  const f32 uB = numeratorB / commonDenominator;
162  if(checkOnlySegments && (uB < 0.f || uB > 1.f))
163  return false; // Outside the line segment
164 
165  // Calculate the intersection point.
166  out.X = (T)(start.X + uA * (end.X - start.X));
167  out.Y = (T)(start.Y + uA * (end.Y - start.Y));
168  return true;
169  }
170 
172 
174  {
175  T len = (T)(1.0 / getLength());
176  return vector2d<T>((end.X - start.X) * len, (end.Y - start.Y) * len);
177  }
178 
180 
182  f64 getAngleWith(const line2d<T>& l) const
183  {
184  vector2d<T> vect = getVector();
185  vector2d<T> vect2 = l.getVector();
186  return vect.getAngleWith(vect2);
187  }
188 
190 
192  T getPointOrientation(const vector2d<T>& point) const
193  {
194  return ( (end.X - start.X) * (point.Y - start.Y) -
195  (point.X - start.X) * (end.Y - start.Y) );
196  }
197 
199 
200  bool isPointOnLine(const vector2d<T>& point) const
201  {
202  T d = getPointOrientation(point);
203  return (d == 0 && point.isBetweenPoints(start, end));
204  }
205 
207 
208  bool isPointBetweenStartAndEnd(const vector2d<T>& point) const
209  {
210  return point.isBetweenPoints(start, end);
211  }
212 
214 
216  vector2d<T> getClosestPoint(const vector2d<T>& point, bool checkOnlySegments=true) const
217  {
218  vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));
219  vector2d<f64> v((f64)(end.X-start.X), (f64)(end.Y-start.Y));
220  f64 d = v.getLength();
221  if ( d == 0 ) // can't tell much when the line is just a single point
222  return start;
223  v /= d;
224  f64 t = v.dotProduct(c);
225 
226  if ( checkOnlySegments )
227  {
228  if (t < 0) return vector2d<T>((T)start.X, (T)start.Y);
229  if (t > d) return vector2d<T>((T)end.X, (T)end.Y);
230  }
231 
232  v *= t;
233  return vector2d<T>((T)(start.X + v.X), (T)(start.Y + v.Y));
234  }
235 
240 };
241 
242  // partial specialization to optimize <f32> lines (avoiding casts)
243  template <>
244  inline vector2df line2d<irr::f32>::getClosestPoint(const vector2df& point, bool checkOnlySegments) const
245  {
246  vector2df c = point - start;
247  vector2df v = end - start;
248  f32 d = (f32)v.getLength();
249  if ( d == 0 ) // can't tell much when the line is just a single point
250  return start;
251  v /= d;
252  f32 t = v.dotProduct(c);
253 
254  if ( checkOnlySegments )
255  {
256  if (t < 0) return start;
257  if (t > d) return end;
258  }
259 
260  v *= t;
261  return start + v;
262  }
263 
264 
269 
270 } // end namespace core
271 } // end namespace irr
272 
273 #endif
274 
line2d(T xa, T ya, T xb, T yb)
Constructor for line between the two points.
Definition: line2d.h:24
line2d< T > operator-(const vector2d< T > &point) const
Definition: line2d.h:35
vector2d< T > start
Start point of the line.
Definition: line2d.h:237
float f32
32 bit floating point variable.
Definition: irrTypes.h:104
vector2d< T > getClosestPoint(const vector2d< T > &point, bool checkOnlySegments=true) const
Get the closest point on this line to a point.
Definition: line2d.h:216
T Y
Y coordinate of vector.
Definition: vector2d.h:319
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
line2d< s32 > line2di
Typedef for an integer line.
Definition: line2d.h:268
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition: vector2d.h:238
bool operator!=(const line2d< T > &other) const
Definition: line2d.h:40
double f64
64 bit floating point variable.
Definition: irrTypes.h:108
void setLine(const line2d< T > &line)
Set this line to new line given as parameter.
Definition: line2d.h:49
vector2d< T > getUnitVector() const
Get unit vector of the line.
Definition: line2d.h:173
bool isPointBetweenStartAndEnd(const vector2d< T > &point) const
Check if the given point is between start and end of the line.
Definition: line2d.h:208
vector2d< T > getVector() const
Get the vector of the line.
Definition: line2d.h:68
line2d< T > & operator-=(const vector2d< T > &point)
Definition: line2d.h:36
T getPointOrientation(const vector2d< T > &point) const
Tells us if the given point lies to the left, right, or on the line.
Definition: line2d.h:192
bool intersectWith(const line2d< T > &l, vector2d< T > &out, bool checkOnlySegments=true) const
Tests if this line intersects with another line.
Definition: line2d.h:77
void setLine(const vector2d< T > &nstart, const vector2d< T > &nend)
Set this line to new line going through the two points.
Definition: line2d.h:47
T getLength() const
Get length of line.
Definition: line2d.h:53
T getLength() const
Gets the length of the vector.
Definition: vector2d.h:114
line2d< T > operator+(const vector2d< T > &point) const
Definition: line2d.h:32
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition: vector2d.h:124
line2d< f32 > line2df
Typedef for an f32 line.
Definition: line2d.h:266
bool isPointOnLine(const vector2d< T > &point) const
Check if the given point is a member of the line.
Definition: line2d.h:200
line2d(const vector2d< T > &start, const vector2d< T > &end)
Constructor for line between the two points given as vectors.
Definition: line2d.h:26
bool operator==(const line2d< T > &other) const
Definition: line2d.h:38
line2d()
Default constructor for line going from (0,0) to (1,1).
Definition: line2d.h:22
2d vector template class with lots of operators and methods.
Definition: dimension2d.h:16
bool isBetweenPoints(const vector2d< T > &begin, const vector2d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition: vector2d.h:259
f64 getAngleWith(const line2d< T > &l) const
Get angle between this line and given line.
Definition: line2d.h:182
line2d< T > & operator+=(const vector2d< T > &point)
Definition: line2d.h:33
line2d(const line2d< T > &other)
Copy constructor.
Definition: line2d.h:28
vector2d< T > end
End point of the line.
Definition: line2d.h:239
vector2d< T > getMiddle() const
Get middle of the line.
Definition: line2d.h:61
2D line between two points with intersection methods.
Definition: line2d.h:18
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:185
T getLengthSQ() const
Get squared length of the line.
Definition: line2d.h:57
void setLine(const T &xa, const T &ya, const T &xb, const T &yb)
Set this line to new line going through the two points.
Definition: line2d.h:45
T X
X coordinate of vector.
Definition: vector2d.h:316