2008-05-22 04:51:37 -07:00
|
|
|
// Copyright (C) 2002-2008 Nikolaus Gebhardt
|
2007-05-20 11:03:49 -07:00
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#ifndef __IRR_LINE_2D_H_INCLUDED__
|
|
|
|
#define __IRR_LINE_2D_H_INCLUDED__
|
|
|
|
|
|
|
|
#include "irrTypes.h"
|
|
|
|
#include "vector2d.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace core
|
|
|
|
{
|
|
|
|
|
|
|
|
//! 2D line between two points with intersection methods.
|
|
|
|
template <class T>
|
|
|
|
class line2d
|
|
|
|
{
|
|
|
|
public:
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Default constructor for line going from (0,0) to (1,1).
|
2007-09-17 09:09:50 -07:00
|
|
|
line2d() : start(0,0), end(1,1) {}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Constructor for line between the two points.
|
2007-09-17 09:09:50 -07:00
|
|
|
line2d(T xa, T ya, T xb, T yb) : start(xa, ya), end(xb, yb) {}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Constructor for line between the two points given as vectors.
|
2007-09-17 09:09:50 -07:00
|
|
|
line2d(const vector2d<T>& start, const vector2d<T>& end) : start(start), end(end) {}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Copy constructor.
|
2007-09-17 09:09:50 -07:00
|
|
|
line2d(const line2d<T>& other) : start(other.start), end(other.end) {}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// operators
|
|
|
|
|
2007-09-17 09:09:50 -07:00
|
|
|
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; }
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2007-09-17 09:09:50 -07:00
|
|
|
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; }
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-04-29 04:34:54 -07:00
|
|
|
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);}
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
// functions
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Set this line to new line going through the two points.
|
2007-05-20 11:03:49 -07:00
|
|
|
void setLine(const T& xa, const T& ya, const T& xb, const T& yb){start.set(xa, ya); end.set(xb, yb);}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Set this line to new line going through the two points.
|
2007-05-20 11:03:49 -07:00
|
|
|
void setLine(const vector2d<T>& nstart, const vector2d<T>& nend){start.set(nstart); end.set(nend);}
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Set this line to new line given as parameter.
|
2007-05-20 11:03:49 -07:00
|
|
|
void setLine(const line2d<T>& line){start.set(line.start); end.set(line.end);}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get length of line
|
|
|
|
/** \return Length of the line. */
|
2007-09-17 09:09:50 -07:00
|
|
|
f64 getLength() const { return start.getDistanceFrom(end); }
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get squared length of the line
|
|
|
|
/** \return Squared length of line. */
|
2007-09-17 09:09:50 -07:00
|
|
|
T getLengthSQ() const { return start.getDistanceFromSQ(end); }
|
2007-05-20 11:03:49 -07:00
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get middle of the line
|
|
|
|
/** \return center of the line. */
|
2007-05-20 11:03:49 -07:00
|
|
|
vector2d<T> getMiddle() const
|
|
|
|
{
|
|
|
|
return (start + end) * (T)0.5;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get the vector of the line.
|
|
|
|
/** \return The vector of the line. */
|
2007-09-17 09:09:50 -07:00
|
|
|
vector2d<T> getVector() const { return vector2d<T>(start.X - end.X, start.Y - end.Y); }
|
2007-05-20 11:03:49 -07:00
|
|
|
|
|
|
|
//! Tests if this line intersects with another line.
|
2008-05-22 04:51:37 -07:00
|
|
|
/** \param l: Other line to test intersection with.
|
|
|
|
\param out: If there is an intersection, the location of the
|
|
|
|
intersection will be stored in this vector.
|
|
|
|
\return True if there is an intersection, false if not. */
|
2007-05-20 11:03:49 -07:00
|
|
|
bool intersectWith(const line2d<T>& l, vector2d<T>& out) const
|
|
|
|
{
|
|
|
|
bool found=false;
|
|
|
|
|
|
|
|
f32 a1,a2,b1,b2;
|
|
|
|
|
|
|
|
// calculate slopes, deal with infinity
|
|
|
|
if (end.X-start.X == 0)
|
|
|
|
b1 = (f32)1e+10;
|
|
|
|
else
|
|
|
|
b1 = (end.Y-start.Y)/(end.X-start.X);
|
|
|
|
if (l.end.X-l.start.X == 0)
|
|
|
|
b2 = (f32)1e+10;
|
|
|
|
else
|
|
|
|
b2 = (l.end.Y-l.start.Y)/(l.end.X-l.start.X);
|
|
|
|
|
|
|
|
// calculate position
|
2008-05-22 04:51:37 -07:00
|
|
|
a1 = start.Y - b1 * start.X;
|
2007-05-20 11:03:49 -07:00
|
|
|
a2 = l.start.Y - b2 * l.start.X;
|
|
|
|
out.X = - (a1-a2)/(b1-b2);
|
|
|
|
out.Y = a1 + b1*out.X;
|
|
|
|
|
|
|
|
// did the lines cross?
|
2008-05-22 04:51:37 -07:00
|
|
|
if ((start.X-out.X) *(out.X-end.X) >= -ROUNDING_ERROR_32 &&
|
2007-05-20 11:03:49 -07:00
|
|
|
(l.start.X-out.X)*(out.X-l.end.X)>= -ROUNDING_ERROR_32 &&
|
|
|
|
(start.Y-out.Y) *(out.Y-end.Y) >= -ROUNDING_ERROR_32 &&
|
|
|
|
(l.start.Y-out.Y)*(out.Y-l.end.Y)>= -ROUNDING_ERROR_32 )
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get unit vector of the line.
|
|
|
|
/** \return Unit vector of this line. */
|
2007-09-17 09:09:50 -07:00
|
|
|
vector2d<T> getUnitVector() const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
T len = (T)(1.0 / getLength());
|
|
|
|
return vector2d<T>((end.X - start.X) * len, (end.Y - start.Y) * len);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get angle between this line and given line.
|
|
|
|
/** \param l Other line for test.
|
|
|
|
\return Angle in degrees. */
|
2007-09-17 09:09:50 -07:00
|
|
|
f64 getAngleWith(const line2d<T>& l) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
vector2d<T> vect = getVector();
|
|
|
|
vector2d<T> vect2 = l.getVector();
|
|
|
|
return vect.getAngleWith(vect2);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Tells us if the given point lies to the left, right, or on the line.
|
|
|
|
/** \return 0 if the point is on the line
|
|
|
|
<0 if to the left, or >0 if to the right. */
|
2007-09-17 09:09:50 -07:00
|
|
|
T getPointOrientation(const vector2d<T>& point) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
2008-05-22 04:51:37 -07:00
|
|
|
return ( (end.X - start.X) * (point.Y - start.Y) -
|
|
|
|
(point.X - start.X) * (end.Y - start.Y) );
|
2007-05-20 11:03:49 -07:00
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Check if the given point is a member of the line
|
2008-08-20 01:36:41 -07:00
|
|
|
/** \return True if point is between start and end, else false. */
|
2007-09-17 09:09:50 -07:00
|
|
|
bool isPointOnLine(const vector2d<T>& point) const
|
2007-05-20 11:03:49 -07:00
|
|
|
{
|
|
|
|
T d = getPointOrientation(point);
|
|
|
|
return (d == 0 && point.isBetweenPoints(start, end));
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Check if the given point is between start and end of the line.
|
|
|
|
/** Assumes that the point is already somewhere on the line. */
|
2007-05-20 11:03:49 -07:00
|
|
|
bool isPointBetweenStartAndEnd(const vector2d<T>& point) const
|
|
|
|
{
|
|
|
|
return point.isBetweenPoints(start, end);
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Get the closest point on this line to a point
|
2007-05-20 11:03:49 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Start point of the line.
|
2007-05-20 11:03:49 -07:00
|
|
|
vector2d<T> start;
|
2008-05-22 04:51:37 -07:00
|
|
|
//! End point of the line.
|
2007-05-20 11:03:49 -07:00
|
|
|
vector2d<T> end;
|
|
|
|
};
|
|
|
|
|
2008-05-22 04:51:37 -07:00
|
|
|
//! Typedef for an f32 line.
|
2007-05-20 11:03:49 -07:00
|
|
|
typedef line2d<f32> line2df;
|
|
|
|
//! Typedef for an integer line.
|
|
|
|
typedef line2d<s32> line2di;
|
|
|
|
|
|
|
|
} // end namespace core
|
|
|
|
} // end namespace irr
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|