#ifndef __IRR_LINE_2D_H_INCLUDED__
#define __IRR_LINE_2D_H_INCLUDED__
#include "irrTypes.h"
#include "vector2d.h"
namespace irr
{
namespace core
{
template <class T>
class line2d
{
public:
line2d() : start(0,0), end(1,1) {}
line2d(T xa, T ya, T xb, T yb) : start(xa, ya), end(xb, yb) {}
line2d(const vector2d<T>& start, const vector2d<T>& end) : start(start), end(end) {}
line2d(const line2d<T>& other) : start(other.start), end(other.end) {}
line2d<T> operator+(const vector2d<T>& point) const { return line2d<T>(start + point, end + point); }
line2d<T>& operator+=(const vector2d<T>& point) { start += point; end += point; return *this; }
line2d<T> operator-(const vector2d<T>& point) const { return line2d<T>(start - point, end - point); }
line2d<T>& operator-=(const vector2d<T>& point) { start -= point; end -= point; return *this; }
bool operator==(const line2d<T>& other) const
{ return (start==other.start && end==other.end) || (end==other.start && start==other.end);}
bool operator!=(const line2d<T>& other) const
{ return !(start==other.start && end==other.end) || (end==other.start && start==other.end);}
void setLine(const T& xa, const T& ya, const T& xb, const T& yb){start.set(xa, ya); end.set(xb, yb);}
void setLine(const vector2d<T>& nstart, const vector2d<T>& nend){start.set(nstart); end.set(nend);}
void setLine(const line2d<T>& line){start.set(line.start); end.set(line.end);}
f64 getLength() const { return start.getDistanceFrom(end); }
T getLengthSQ() const { return start.getDistanceFromSQ(end); }
vector2d<T> getMiddle() const
{
return (start + end) * (T)0.5;
}
vector2d<T> getVector() const { return vector2d<T>(end.X - start.X, end.Y - start.Y); }
bool intersectWith(const line2d<T>& l, vector2d<T>& out, bool checkOnlySegments=true) const
{
const f32 commonDenominator = (l.end.Y - l.start.Y)*(end.X - start.X) -
(l.end.X - l.start.X)*(end.Y - start.Y);
const f32 numeratorA = (l.end.X - l.start.X)*(start.Y - l.start.Y) -
(l.end.Y - l.start.Y)*(start.X -l.start.X);
const f32 numeratorB = (end.X - start.X)*(start.Y - l.start.Y) -
(end.Y - start.Y)*(start.X -l.start.X);
if(equals(commonDenominator, 0.f))
{
if(equals(numeratorA, 0.f) && equals(numeratorB, 0.f))
{
if(l.start == start || l.end == start)
out = start;
else if(l.end == end || l.start == end)
out = end;
else if (l.start.X>start.X && l.end.X>start.X && l.start.X>end.X && l.end.X>end.X)
return false;
else if (l.start.Y>start.Y && l.end.Y>start.Y && l.start.Y>end.Y && l.end.Y>end.Y)
return false;
else if (l.start.X<start.X && l.end.X<start.X && l.start.X<end.X && l.end.X<end.X)
return false;
else if (l.start.Y<start.Y && l.end.Y<start.Y && l.start.Y<end.Y && l.end.Y<end.Y)
return false;
else
{
vector2d<T> maxp;
vector2d<T> minp;
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))
maxp=start;
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))
maxp=end;
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))
maxp=l.start;
else
maxp=l.end;
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)))
minp=start;
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)))
minp=end;
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)))
minp=l.start;
else
minp=l.end;
out = core::vector2d<T>();
if (start != maxp && start != minp)
out += start;
if (end != maxp && end != minp)
out += end;
if (l.start != maxp && l.start != minp)
out += l.start;
if (l.end != maxp && l.end != minp)
out += l.end;
out *= 0.5f;
}
return true;
}
return false;
}
const f32 uA = numeratorA / commonDenominator;
if(checkOnlySegments && (uA < 0.f || uA > 1.f) )
return false;
const f32 uB = numeratorB / commonDenominator;
if(checkOnlySegments && (uB < 0.f || uB > 1.f))
return false;
out.X = start.X + uA * (end.X - start.X);
out.Y = start.Y + uA * (end.Y - start.Y);
return true;
}
vector2d<T> getUnitVector() const
{
T len = (T)(1.0 / getLength());
return vector2d<T>((end.X - start.X) * len, (end.Y - start.Y) * len);
}
f64 getAngleWith(const line2d<T>& l) const
{
vector2d<T> vect = getVector();
vector2d<T> vect2 = l.getVector();
return vect.getAngleWith(vect2);
}
T getPointOrientation(const vector2d<T>& point) const
{
return ( (end.X - start.X) * (point.Y - start.Y) -
(point.X - start.X) * (end.Y - start.Y) );
}
bool isPointOnLine(const vector2d<T>& point) const
{
T d = getPointOrientation(point);
return (d == 0 && point.isBetweenPoints(start, end));
}
bool isPointBetweenStartAndEnd(const vector2d<T>& point) const
{
return point.isBetweenPoints(start, end);
}
vector2d<T> getClosestPoint(const vector2d<T>& point) const
{
vector2d<T> c = point - start;
vector2d<T> v = end - start;
T d = (T)v.getLength();
v /= d;
T t = v.dotProduct(c);
if (t < (T)0.0) return start;
if (t > d) return end;
v *= t;
return start + v;
}
vector2d<T> start;
vector2d<T> end;
};
typedef line2d<f32> line2df;
typedef line2d<s32> line2di;
}
}
#endif