//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Determine if a two dimensional point lies within the confines of a given
// two dimensional triangle.
//
// From Dr. Dobbs Journal, October, 2000
bool IsPointInsideFace(
double pX,
double pY,
double v0x,
double v0y,
double v1x,
double v1y,
double v2x,
double v2y
)
{
double x0, x1, x2, y1, u, v, denom;
x1 = v1x - v0x;
if ( x1 == 0.0 )
{
// Triangle points 0 and 1 have the same X value
// Zero area test - can be removed if triangle is known to be valid
x2 = v2x - v0x;
if ( ( x2 == 0.0 ) | |
// Compute u and check bounds
( ( u = (pX - v0x) / x2 ) < 0.0 ) &#0124; &#0124;
( u > 1.0 ) &#0124; &#0124;
// Zero area test - remove if known to be valid
( ( y1 = v1y - v0y ) == 0.0 ) &#0124; &#0124;
// Compute v and check bounds
( ( v = ( (pY - v0y) - u *
( v2y - v0y ) ) / y1 ) < 0.0 ) )
{
// Missed for some reason
return false;
}
}
else
{
// Triangle points 0 and 1 have a different X value
// Compute denom and check for zero area triangle - check
// is not needed if triangle known to be valid.
x2 = v2x - v0x;
y1 = v1y - v0y;
denom = ( v2y - v0y ) * x1 - x2 * y1;
if ( ( denom == 0.0 ) &#0124; &#0124;
// Compute u and check bounds
( ( u = ( (pY - v0y) * x1 - (x0 = pX - v0x) * y1 ) / denom ) < 0.0 ) &#0124; &#0124;
( u > 1.0 ) &#0124; &#0124;
// Compute v & check bounds
( ( v = ( x0 - u * x2 ) / x1 ) < 0.0 ) )
{
// Missed for some reason
return false;
}
}
// Check gamma
if ( u + v > 1.0 )
{
// Outside third edge
return false;
}
return true;
}