// 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); }