Intersection between lines

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
EViruS
Posts: 19
Joined: Sat Jul 07, 2007 8:32 pm

Intersection between lines

Post by EViruS »

How to find intersection between core::line3d ?
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

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
EViruS
Posts: 19
Joined: Sat Jul 07, 2007 8:32 pm

Post by EViruS »

Thx
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;
}}
Post Reply