Add IsPointInTriangle2D

This commit is contained in:
Steve Streeting 2023-12-01 16:21:14 +00:00
parent dad1fde054
commit 50f788762e

View File

@ -125,4 +125,26 @@ public:
return GetDistanceToConvex2D(ConvexPoints, LocalPoint) * ConvexTransform.GetScale3D().X;
}
/**
* Returns whether a 2D point is inside a triangle
* @param p Point to test
* @param v0 First triangle point
* @param v1 Second triangle point
* @param v2 Third triangle point
* @return Whether point p is inside the triangle.
*/
static bool IsPointInTriangle2D(const FVector& p,
const FVector2f& v0,
const FVector2f& v1,
const FVector2f& v2)
{
const float s = (v0.X - v2.X) * (p.Y - v2.Y) - (v0.Y - v2.Y) * (p.X - v2.X);
const float t = (v1.X - v0.X) * (p.Y - v0.Y) - (v1.Y - v0.Y) * (p.X - v0.X);
if ((s < 0) != (t < 0) && s != 0 && t != 0)
return false;
const float d = (v2.X - v1.X) * (p.Y - v1.Y) - (v2.Y - v1.Y) * (p.X - v1.X);
return d == 0 || (d < 0) == (s + t <= 0);
}
};