On 19/06/2015 at 04:26, xxxxxxxx wrote:
it is quite easy, I remember I wrote or found one somewhere before, here is the function from my code:
template<typename T>
struct SNodePoint
{
T x;
T y;
SNodePoint() :
x(0), y(0)
{}
SNodePoint(T &_x, T &_y) :
x(_x), y(_y)
{}
SNodePoint(const T &_x, const T &_y) :
x(_x), y(_y)
{}
SNodePoint& operator+=(const SNodePoint& rhs)
{
x = x + rhs.x;
y = y + rhs.y;
return *this;
}
friend SNodePoint operator+(SNodePoint lhs, const SNodePoint& rhs)
{
return lhs += rhs;
}
SNodePoint& operator-=(const SNodePoint& rhs)
{
x = x - rhs.x;
y = y - rhs.y;
return *this;
}
friend SNodePoint operator-(SNodePoint lhs, const SNodePoint& rhs)
{
return lhs -= rhs;
}
SNodePoint& operator*=(const Float32& rhs)
{
x = x * rhs;
y = y * rhs;
return *this;
}
friend SNodePoint operator*(SNodePoint lhs, const Float32& rhs)
{
return lhs *= rhs;
}
friend SNodePoint operator*(Float32 lhs, const SNodePoint& rhs)
{
return rhs * lhs;
}
SNodePoint& operator/=(const Float32& rhs)
{
x = x / rhs;
y = y / rhs;
return *this;
}
friend SNodePoint operator/(SNodePoint lhs, const Float32& rhs)
{
return lhs /= rhs;
}
friend SNodePoint operator/(Float32 lhs, const SNodePoint& rhs)
{
return rhs / lhs;
}
};
Bool get_line_intersection(const SNodePoint<Float32> &p0, const SNodePoint<Float32> &p1,
const SNodePoint<Float32> &p2, const SNodePoint<Float32> & p3, SNodePoint<Float32> *pi)
{
float s02_x, s02_y, s10_x, s10_y, s32_x, s32_y, s_numer, t_numer, denom, t;
s10_x = p1.x - p0.x;
s10_y = p1.y - p0.y;
s32_x = p3.x - p2.x;
s32_y = p3.y - p2.y;
denom = s10_x * s32_y - s32_x * s10_y;
if (denom == 0)
return FALSE; // Collinear
bool denomPositive = denom > 0;
s02_x = p0.x - p2.x;
s02_y = p0.y - p2.y;
s_numer = s10_x * s02_y - s10_y * s02_x;
if ((s_numer < 0) == denomPositive)
return FALSE; // No collision
t_numer = s32_x * s02_y - s32_y * s02_x;
if ((t_numer < 0) == denomPositive)
return FALSE; // No collision
if (((s_numer > denom) == denomPositive) || ((t_numer > denom) == denomPositive))
return FALSE; // No collision
// Collision detected
t = t_numer / denom;
if (pi != NULL)
{
pi->x = p0.x + (t * s10_x);
pi->y = p0.y + (t * s10_y);
}
return TRUE;
}