Jump to content

  • Log In with Google      Sign In   
  • Create Account

ybijgu

Member Since 21 Jan 2008
Offline Last Active Aug 21 2011 02:27 PM

Topics I've Started

Critique my cover letter please.

22 July 2010 - 07:33 AM

Deleted

Closest point on tri

03 April 2009 - 04:25 AM

Basically, I have the same problem that was raised here: link Where I have an arbitrary point in 3d space and a triangle, and I want to find the the coordinates of the point on the triangle that is closest to my point in 3d space. I'm interested in the pseudocode very last post on the page I linked, however, no one posted there to verify whether or not it was accurate, and I don't understand the math well enough to verify it myself. Also, there was a vague place or two that I'm wasn't clear about. I've created some code that reflects the pseudocode as best as I could understand, and I was hoping someone would help me fix any errors.
Vector3D GetClosestPointOnTri(Vector3D& point,Vector3D& v0,Vector3D& v1,Vector3D& v2)
{
	Vector3D r0;
	Vector3D r1;
	Vector3D r2;

	float q1;
	float q2;

	float t;
	
	r0 = point - v0;
	r1 = v1 - v0;
	r2 = v2 - v0;

	q1 = Dot(r1,r0) / SquaredMag(r1);
	q2 = Dot(r2,r0) / SquaredMag(r2);
	
	if((q1 > 0) && (q2 > 0) && (q1 + q2 < 1))
	{
		return (v0 + (r1 * q1) + (r2 * q2));
	}
	
	if(q1 < 0)
	{
		if(q2 < 0)
		{
			return v0;
		}
		else if(q2 > 1)
		{
			return v2;
		}
		else
		{
			return (v0 + (r2 * q2));
		}
	}

	if(q2 < 0)
	{
		if(q1 > 1)
		{
			return v1; //is this right?
		}
		else
		{
			return Vector3D(-3000,-3000,-3000); //What actually goes here?
		}
	}

	if((q1 > 0) && (q2 > 0))
	{
		t = (1 - q1 + q2) / 2;
		if(t > 1)
		{
			return v2;
		}
		else if(t < 0)
		{
			return v0;
		}
		else
		{
			return (v0 + v1 + ((v2 - v1) * t));
		}
	}

	return Vector3D(-5000,-5000,-5000);
}


EDIT: also, what are the tags for putting code into a scrollbox, the ones I'm trying aren't working [Edited by - zebeste on April 3, 2009 11:11:32 AM]

PARTNERS