Triangle and rectangle intersect find position points of rectangle that intersect

Started by
11 comments, last by Vortez 9 years, 11 months ago
I need integer positions

This is a probably a design flaw... There's no way you can use these intersections as integers unless you can guarantee the shapes will always intersect on round coordinates. To guarantee your intersections always have integer coordinates, you'll have to limit your triangles to (45º 45º 90º) or create some complex angle-position rules to fit your problem...

I don't really know in what context this intersections are going to be used and can't say much without really seeing the context.

But you could pretty much cast the resulting coordinates to integer, after all the math; but it will wield less accurate positions. If your problem allows you to use approximations of these intersections and your shapes are big enough to reduce the relative difference, you'd probably do fine without the decimals. But you can't use integers when calculating these intersections as, as @slayemin said, the results could vary enormously from the correct values.

But the better option, based on my limited understanding of your problem, would be to fix the design and use floating point numbers instead.

Advertisement

I need integer positions

This is a probably a design flaw... There's no way you can use these intersections as integers unless you can guarantee the shapes will always intersect on round coordinates. To guarantee your intersections always have integer coordinates, you'll have to limit your triangles to (45º 45º 90º) or create some complex angle-position rules to fit your problem...

I have two rectangles, one rectangle gets sliced up intro four triangles where each one of them has starting point at center and takes two outer positions of rectangle ensuing (45,45,90 degrees angles) and the other rectangle has (45,45,90)% angle, and therefore i had no issues with the math.

I usually do math test on paper before writing it in code so i check possible solutions and if it is correct, i had no issues to achieve what i required so it is fine, but i understand there are conditions that will not work with given example.

Also i always do my best so simplify the question so i don't paste pages of code thus some points get ignored, my apologies i will try to do better job next time happy.png

I know other already posted the solution but, here's mine:


//-----------------------------------------------------------------------------
// Test if 2 lines intersect each other
//-----------------------------------------------------------------------------
bool CPongEngine::LinesIntersect(CLine l1, CLine l2, CVector *pIntersectPoint)
{
	CVector p0 = l1.p1;
	CVector p1 = l1.p2;
	CVector p2 = l2.p1;
	CVector p3 = l2.p2;

	CVector s1 = p1 - p0;
	CVector s2 = p3 - p2;

	float s = ((-s1.v.y * (p0.v.x - p2.v.x)) + (s1.v.x * (p0.v.y - p2.v.y))) / ((-s2.v.x * s1.v.y) + (s1.v.x * s2.v.y));
	float t = (( s2.v.x * (p0.v.y - p2.v.y)) - (s2.v.y * (p0.v.x - p2.v.x))) / ((-s2.v.x * s1.v.y) + (s1.v.x * s2.v.y));

	CVector u = p0 + (t * s1);

        // Return the intersection point 
	if(s >= 0.0f && s <= 1.0f && t >= 0.0f && t <= 1.0f){
		if(pIntersectPoint)
			*pIntersectPoint = u;
		return true;
	}

        // Not intersecting...
	if(pIntersectPoint)
		pIntersectPoint->Set(0.0f, 0.0f, 0.0f);

	return false;
}

That's what i used in my pong and pool game and it work fine. CLine is just a structure of 2 CVector btw.

This topic is closed to new replies.

Advertisement