Intersection between lines
Intersection between lines
How to find intersection between core::line3d ?
Have no quick silution, line3d have no such method build in. You may find answer together with some usefull knoweledge here: http://www.flipcode.com/geometry/.
Look at part about relationship between lines (including intersection):http://www.flipcode.com/articles/gprimer2_issue02.shtml
Look at part about relationship between lines (including intersection):http://www.flipcode.com/articles/gprimer2_issue02.shtml
Thx
My solution:
My solution:
Code: Select all
bool g_IntersectLinesFast(const core::line3df& _a, const core::line3df& _b, core::vector3df& out) {
core::line3df a = _a, b = _b;
a.end -= a.start;
b.end -= b.start;
f32 t1 = 0.0f;
f32 t2 = 0.0f;
bool bCalculated = false;
//by Cramer
f32 det = 0;
//XY
if(!bCalculated) {
det = a.end.X * (-b.end.Y) + a.end.Y * b.end.X;
if(0.00001f < abs(det)) {
t1 = (b.start.X - a.start.X) * (-b.end.Y) + (b.start.Y - a.start.Y) * b.end.X;
t2 = (b.start.Y - a.start.Y) * (-a.end.X) + (b.start.X - a.start.X) * a.end.Y;
bCalculated = true;
}
}
//XZ
if(!bCalculated) {
det = a.end.X * (-b.end.Z) + a.end.Z * b.end.X;
if(0.00001f < abs(det)) {
t1 = (b.start.X - a.start.X) * (-b.end.Z) + (b.start.Z - a.start.Z) * b.end.X;
t2 = (b.start.Z - a.start.Z) * (-a.end.X) + (b.start.X - a.start.X) * a.end.Z;
bCalculated = true;
}
}
//YZ
if(!bCalculated) {
det = a.end.Y * (-b.end.Z) + a.end.Z * b.end.Y;
if(0.00001f < abs(det)) {
t1 = (b.start.Y - a.start.Y) * (-b.end.Z) + (b.start.Z - a.start.Z) * b.end.Y;
t2 = (b.start.Z - a.start.Z) * (-a.end.Y) + (b.start.Y - a.start.Y) * a.end.Z;
bCalculated = true;
}
}
if(!bCalculated) {
out = core::vector3df(0.0f, 0.0f, 0.0f);
return false;
}
t1 /= det;
t2 /= det;
core::vector3df r1 = a.start + t1 * a.end;
core::vector3df r2 = b.start + t2 * b.end;
out = r1;
return true;
}}