Loading...
Searching...
No Matches
line2d.h
Go to the documentation of this file.
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// 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
{
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) {}
// operators
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);}
// functions
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);}
T getLength() const { return start.getDistanceFrom(end); }
T getLengthSQ() const { return start.getDistanceFromSQ(end); }
vector2d<T> getMiddle() const
{
return (start + end)/(T)2;
}
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
{
// Uses the method given at:
// http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
const f32 commonDenominator = (f32)(l.end.Y - l.start.Y)*(end.X - start.X) -
(l.end.X - l.start.X)*(end.Y - start.Y);
const f32 numeratorA = (f32)(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 = (f32)(end.X - start.X)*(start.Y - l.start.Y) -
(end.Y - start.Y)*(start.X -l.start.X);
if(equals(commonDenominator, 0.f))
{
// The lines are either coincident or parallel
// if both numerators are 0, the lines are coincident
if(equals(numeratorA, 0.f) && equals(numeratorB, 0.f))
{
// Try and find a common endpoint
if(l.start == start || l.end == start)
out = start;
else if(l.end == end || l.start == end)
out = end;
// now check if the two segments are disjunct
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 the lines are overlapping to some extent
else
{
// find the points which are not contributing to the
// common part
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;
// one line is contained in the other. Pick the center
// of the remaining points, which overlap for sure
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.X = (T)(out.X/2);
out.Y = (T)(out.Y/2);
}
return true; // coincident
}
return false; // parallel
}
// Get the point of intersection on this line, checking that
// it is within the line segment.
const f32 uA = numeratorA / commonDenominator;
if(checkOnlySegments && (uA < 0.f || uA > 1.f) )
return false; // Outside the line segment
const f32 uB = numeratorB / commonDenominator;
if(checkOnlySegments && (uB < 0.f || uB > 1.f))
return false; // Outside the line segment
// Calculate the intersection point.
out.X = (T)(start.X + uA * (end.X - start.X));
out.Y = (T)(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, bool checkOnlySegments=true) const
{
vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));
vector2d<f64> v((f64)(end.X-start.X), (f64)(end.Y-start.Y));
f64 d = v.getLength();
if ( d == 0 ) // can't tell much when the line is just a single point
return start;
v /= d;
f64 t = v.dotProduct(c);
if ( checkOnlySegments )
{
if (t < 0) return vector2d<T>((T)start.X, (T)start.Y);
if (t > d) return vector2d<T>((T)end.X, (T)end.Y);
}
v *= t;
return vector2d<T>((T)(start.X + v.X), (T)(start.Y + v.Y));
}
vector2d<T> start;
vector2d<T> end;
};
// partial specialization to optimize <f32> lines (avoiding casts)
template <>
inline vector2df line2d<irr::f32>::getClosestPoint(const vector2df& point, bool checkOnlySegments) const
{
vector2df c = point - start;
vector2df v = end - start;
f32 d = (f32)v.getLength();
if ( d == 0 ) // can't tell much when the line is just a single point
return start;
v /= d;
f32 t = v.dotProduct(c);
if ( checkOnlySegments )
{
if (t < 0) return start;
if (t > d) return end;
}
v *= t;
return start + v;
}
typedef line2d<f32> line2df;
typedef line2d<s32> line2di;
} // end namespace core
} // end namespace irr
#endif
irr::core::line2d
2D line between two points with intersection methods.
Definition line2d.h:19
irr::core::line2d::isPointOnLine
bool isPointOnLine(const vector2d< T > &point) const
Check if the given point is a member of the line.
Definition line2d.h:200
irr::core::line2d::getAngleWith
f64 getAngleWith(const line2d< T > &l) const
Get angle between this line and given line.
Definition line2d.h:182
irr::core::line2d::operator==
bool operator==(const line2d< T > &other) const
Definition line2d.h:38
irr::core::line2d::line2d
line2d(const line2d< T > &other)
Copy constructor.
Definition line2d.h:28
irr::core::line2d::operator-
line2d< T > operator-(const vector2d< T > &point) const
Definition line2d.h:35
irr::core::line2d::setLine
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
irr::core::line2d::getClosestPoint
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
irr::core::line2d< f32 >::end
vector2d< f32 > end
Definition line2d.h:240
irr::core::line2d::operator+=
line2d< T > & operator+=(const vector2d< T > &point)
Definition line2d.h:33
irr::core::line2d::line2d
line2d(const vector2d< T > &start, const vector2d< T > &end)
Constructor for line between the two points given as vectors.
Definition line2d.h:26
irr::core::line2d::getMiddle
vector2d< T > getMiddle() const
Get middle of the line.
Definition line2d.h:61
irr::core::line2d::isPointBetweenStartAndEnd
bool isPointBetweenStartAndEnd(const vector2d< T > &point) const
Check if the given point is between start and end of the line.
Definition line2d.h:208
irr::core::line2d::setLine
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
irr::core::line2d::getUnitVector
vector2d< T > getUnitVector() const
Get unit vector of the line.
Definition line2d.h:173
irr::core::line2d::getLength
T getLength() const
Get length of line.
Definition line2d.h:53
irr::core::line2d::line2d
line2d()
Default constructor for line going from (0,0) to (1,1).
Definition line2d.h:22
irr::core::line2d::getPointOrientation
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
irr::core::line2d::operator!=
bool operator!=(const line2d< T > &other) const
Definition line2d.h:40
irr::core::line2d::intersectWith
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
irr::core::line2d::getLengthSQ
T getLengthSQ() const
Get squared length of the line.
Definition line2d.h:57
irr::core::line2d::operator+
line2d< T > operator+(const vector2d< T > &point) const
Definition line2d.h:32
irr::core::line2d::operator-=
line2d< T > & operator-=(const vector2d< T > &point)
Definition line2d.h:36
irr::core::line2d< f32 >::start
vector2d< f32 > start
Definition line2d.h:238
irr::core::line2d::getVector
vector2d< T > getVector() const
Get the vector of the line.
Definition line2d.h:68
irr::core::line2d::setLine
void setLine(const line2d< T > &line)
Set this line to new line given as parameter.
Definition line2d.h:49
irr::core::line2d::line2d
line2d(T xa, T ya, T xb, T yb)
Constructor for line between the two points.
Definition line2d.h:24
irr::core::vector2d
2d vector template class with lots of operators and methods.
Definition vector2d.h:22
irr::core::vector2d::getAngleWith
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition vector2d.h:238
irr::core::vector2d::getLength
T getLength() const
Gets the length of the vector.
Definition vector2d.h:114
irr::core::vector2d::X
T X
X coordinate of vector.
Definition vector2d.h:316
irr::core::vector2d::Y
T Y
Y coordinate of vector.
Definition vector2d.h:319
irr::core::vector2d::dotProduct
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition vector2d.h:124
irr::core::vector2d::isBetweenPoints
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
irrTypes.h
irr::core
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
irr::core::line2df
line2d< f32 > line2df
Typedef for an f32 line.
Definition line2d.h:267
irr::core::vector2df
vector2d< f32 > vector2df
Typedef for f32 2d vector.
Definition vector2d.h:323
irr::core::line2di
line2d< s32 > line2di
Typedef for an integer line.
Definition line2d.h:269
irr::core::equals
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
irr
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
irr::f32
float f32
32 bit floating point variable.
Definition irrTypes.h:104
irr::f64
double f64
64 bit floating point variable.
Definition irrTypes.h:108
vector2d.h
- Irrlicht Engine Documentation © 2003-2012 by Nikolaus Gebhardt. Generated on for Irrlicht 3D Engine by Doxygen 1.15.0