Original Post
So I was looking around for a way to do the old point in triangle test, and I was finding a lot of ways to do it using trig. Then I found one using areas, and this was simple enough, but the only way I knew to do areas was Heron's formula, and that's a lot of sqrts (16 per triangle test, I believe). Then I found a way to do triangle areas using the determinant. No sqrt, no cos, or sin, etc. I'm working from this. Is this sort of thing going to work, or am I missing something? I haven't written any testing code. But I did write a couple of functions (one to calc area, one to compare areas). They are here:
// A floating point
struct fPoint
{
float x;
float y;
};
/* IsPointInTri() -
* Used by IsPointInQuad(). Function takes the point and the triangle's
* vertices. It finds the area of the passed triangle (v1 v2 v3), and then the
* areas of the three triangles (pt v2 v3), (pt v1 v3), and (pt v1 v2). If the
* sum of these three is greater than the first, then the point is outside of
* the triangle.
*/
bool IsPointInTri(const fPoint *pt, const fPoint *v1, const fPoint *v2, const fPoint *v3)
{
float TotalArea = CalcTriArea(v1, v2, v3);
float Area1 = CalcTriArea(pt, v2, v3);
float Area2 = CalcTriArea(pt, v1, v3);
float Area3 = CalcTriArea(pt, v1, v2);
if((Area1 + Area2 + Area3) > TotalArea)
return false;
else
return true;
}
/* CalcTriArea() -
* Find the area of a triangle. This function uses the 1/2 determinant
* method. Given three points (x1, y1), (x2, y2), (x3, y3):
* | x1 y1 1 |
* Area = .5 * | x2 y2 1 |
* | x3 y3 1 |
* From: http://mcraefamily.com/MathHelp/GeometryTriangleAreaDeterminant.htm
*/
float CalcTriArea(fPoint *v1, fPoint *v2, fPoint *v3)
{
float det = 0.0f;
det = ((v1->x - v3->x) * (v2->y - v3->y)) - ((v2->x - v3->x) * (v1->y - v3->y));
return (det / 2.0f);
}