internal static bool IsPointInTriangle(Point p, Point t0, Point t1, Point t2)
{
// TODO: no need to create new Point instances here
Point d0 = new Point(t1.X - t0.X, t1.Y - t0.Y);
Point d1 = new Point(t2.X - t0.X, t2.Y - t0.Y);
Point d2 = new Point(p.X - t0.X, p.Y - t0.Y);
// crossproduct of (0, 0, 1) and d0
Point c0 = new Point(-d0.Y, d0.X);
// crossproduct of (0, 0, 1) and d1
Point c1 = new Point(-d1.Y, d1.X);
// Linear combination d2 = s * d0 + v * d1.
//
// Multiply both sides of the equation with c0 and c1
// and solve for s and v respectively
//
// s = d2 * c1 / d0 * c1
// v = d2 * c0 / d1 * c0
double s = DotProduct(d2, c1) / DotProduct(d0, c1);
double v = DotProduct(d2, c0) / DotProduct(d1, c0);
if (s >= 0 && v >= 0 && ((s + v) <= 1))
{
// Point is inside or on the edge of this triangle.
return true;
}
return false;
}