Irrlicht 3D Engine
 
Loading...
Searching...
No Matches
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
11namespace irr
12{
13namespace core
14{
15
17template <class T>
18class 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) {}
29
30 // operators
31
33 line2d<T>& operator+=(const vector2d<T>& point) { start += point; end += point; return *this; }
34
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
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
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
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.
159 return false; // Outside the line segment
160
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
183 {
185 vector2d<T> vect2 = l.getVector();
186 return vect.getAngleWith(vect2);
187 }
188
190
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 {
203 return (d == 0 && point.isBetweenPoints(start, end));
204 }
205
207
209 {
210 return point.isBetweenPoints(start, end);
211 }
212
214
218 {
219 vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));
220 vector2d<f64> v((f64)(end.X-start.X), (f64)(end.Y-start.Y));
221 f64 d = v.getLength();
222 if ( d == 0 ) // can't tell much when the line is just a single point
223 return start;
224 v /= d;
225 f64 t = v.dotProduct(c);
226
227 if ( checkOnlySegments )
228 {
229 if (t < 0) return vector2d<T>((T)start.X, (T)start.Y);
230 if (t > d) return vector2d<T>((T)end.X, (T)end.Y);
231 }
232
233 v *= t;
234 return vector2d<T>((T)(start.X + v.X), (T)(start.Y + v.Y));
235 }
236
241};
242
243 // partial specialization to optimize <f32> lines (avoiding casts)
244 template <>
246 {
247 vector2df c = point - start;
248 vector2df v = end - start;
249 f32 d = (f32)v.getLength();
250 if ( d == 0 ) // can't tell much when the line is just a single point
251 return start;
252 v /= d;
253 f32 t = v.dotProduct(c);
254
255 if ( checkOnlySegments )
256 {
257 if (t < 0) return start;
258 if (t > d) return end;
259 }
260
261 v *= t;
262 return start + v;
263 }
264
265
270
271} // end namespace core
272} // end namespace irr
273
274#endif
275
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
aabbox3d()
Default Constructor.
Definition aabbox3d.h:26
2D line between two points with intersection methods.
Definition line2d.h:19
bool isPointOnLine(const vector2d< T > &point) const
Check if the given point is a member of the line.
Definition line2d.h:200
f64 getAngleWith(const line2d< T > &l) const
Get angle between this line and given line.
Definition line2d.h:182
bool operator==(const line2d< T > &other) const
Definition line2d.h:38
line2d(const line2d< T > &other)
Copy constructor.
Definition line2d.h:28
line2d< T > operator-(const vector2d< T > &point) const
Definition line2d.h:35
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
vector2d< T > getClosestPoint(const vector2d< T > &point, bool checkOnlySegments=true) const
Get the closest point on this line to a point.
Definition line2d.h:217
vector2d< T > end
End point of the line.
Definition line2d.h:240
line2d< T > & operator+=(const vector2d< T > &point)
Definition line2d.h:33
line2d(const vector2d< T > &start, const vector2d< T > &end)
Constructor for line between the two points given as vectors.
Definition line2d.h:26
vector2d< T > getMiddle() const
Get middle of the line.
Definition line2d.h:61
bool isPointBetweenStartAndEnd(const vector2d< T > &point) const
Check if the given point is between start and end of the line.
Definition line2d.h:208
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
vector2d< T > getUnitVector() const
Get unit vector of the line.
Definition line2d.h:173
T getLength() const
Get length of line.
Definition line2d.h:53
line2d()
Default constructor for line going from (0,0) to (1,1).
Definition line2d.h:22
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 operator!=(const line2d< T > &other) const
Definition line2d.h:40
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
T getLengthSQ() const
Get squared length of the line.
Definition line2d.h:57
line2d< T > operator+(const vector2d< T > &point) const
Definition line2d.h:32
line2d< T > & operator-=(const vector2d< T > &point)
Definition line2d.h:36
vector2d< T > start
Start point of the line.
Definition line2d.h:238
vector2d< T > getVector() const
Get the vector of the line.
Definition line2d.h:68
void setLine(const line2d< T > &line)
Set this line to new line given as parameter.
Definition line2d.h:49
line2d(T xa, T ya, T xb, T yb)
Constructor for line between the two points.
Definition line2d.h:24
T getLength() const
Gets the length of the vector.
Definition vector2d.h:114
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:267
line2d< s32 > line2di
Typedef for an integer line.
Definition line2d.h:269
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
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
float f32
32 bit floating point variable.
Definition irrTypes.h:104
double f64
64 bit floating point variable.
Definition irrTypes.h:108