Math
Rect
判断点是否在矩形中:
方法一(UE4)
bool FSlateRotatedRect::IsUnderLocation(const FVector2D& Location) const
{
const FVector2D Offset = Location - TopLeft;
const float Det = FVector2D::CrossProduct(ExtentX, ExtentY);
// Not exhaustively efficient. Could optimize the checks for [0..1] to short circuit faster.
const float S = -FVector2D::CrossProduct(Offset, ExtentX) / Det;
if (FMath::IsWithinInclusive(S, 0.0f, 1.0f))
{
const float T = FVector2D::CrossProduct(Offset, ExtentY) / Det;
return FMath::IsWithinInclusive(T, 0.0f, 1.0f);
}
return false;
}
问题:如果Det为0时,就会出现除0错误
方法二:
只需要判断该点是否在上下两条边和左右两条边之间就行。
判断一个点是否在两条线段之间夹着就转化成,判断一个点是否在某条线段的一边上,就可以利用叉乘的方向性,来判断夹角是否超过了180度 如下图

只要判断(AB X AE ) * (CDX CE) >= 0 就说明E在AB,CD中间夹着,同理计算另两边DA和BC就可以了。
最后就是只需要判断
(AB X AE ) * (CD X CE) >= 0 && (DA X DE ) * (BC X BE) >= 0 。
Last updated