# point in polgyn test

This topic is 5062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I use the following algorithm to check if a point is in a triangle. The normal of the triangle is normalized. Should i normalize cp also in the following code?
bool CorrosionSim::pointInTriangle(const Vector3D& p, const Face& t)
{
Vector3D p0=m_pObject->getMesh()->getVertex(t.getVertex(0)).getPosition();
Vector3D p1=m_pObject->getMesh()->getVertex(t.getVertex(1)).getPosition();
Vector3D p2=m_pObject->getMesh()->getVertex(t.getVertex(2)).getPosition();

Vector3D n, cp;

n=m_pObject->getMesh()->getFaceNormal(t.getNormal());

Vector3D p0p=p0-p;
Vector3D p1p=p1-p;

cp.crossProduct(p0p, p1p);
if(Vector3D::dotProduct(cp, n)<0.f)
return false;

Vector3D p2p=p2-p;

cp.crossProduct(p1p, p2p);
if(Vector3D::dotProduct(cp, n)<0.f)
return false;

cp.crossProduct(p2p, p0p);
if(Vector3D::dotProduct(cp,n)<0.f)
return false;

return true;
}


##### Share on other sites
It shouldn't matter whether or not cp is normalized. Is it not working? You might try changing < 0 to > 0 (if you haven't tried that already), just in case your cross products are pointing in the wrong direction. Other than that I'd have to look at the code more carefully, but can't now cause I'm at work - sorry!

##### Share on other sites
jyk is right, you don't need to normalize it.
The <0 is ok too, the function his based on the fact that if the point is outside at least one of the vectors you obtain with the cross product is opposite to the normal.

By the way, good idea, it's much faster than my PointInTriangle function.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015656
×