我想检查点 Q 是否在圆内(P1,P2,P3),但是当该点是圆的一部分时我遇到问题,因为最后一个条件在应该为 true 时将其返回为 flase,这是因为浮动精度。
public static bool QTest(Vector3 P1 ,Vector3 P2, Vector3 P3, Vector3 Q)//Test if the point Q is inside the circumcircle of P1, P2, P3
{
Vector3 midPoint1 = MidPoint(P1, P2);
float pSlope = -1 / ((P2.z - P1.z) / (P2.x - P1.x));
Vector3 midPoint2 = MidPoint(P2, P3);
float pSlope2 = -1 / ((P3.z - P2.z) / (P3.x - P2.x));
//formula y - midPoint.z = perpendicularSlope * (x - midPoint.x)
float x = (- pSlope2 * midPoint2.x + midPoint2.z + pSlope * midPoint1.x - midPoint1.z) / (pSlope - pSlope2);
float y = pSlope * (x - midPoint1.x) + midPoint1.z;
Vector3 center = new(x, 0, y);
float epsilon = 1e-3f;
float radius = (center - P1).sqrMagnitude;
float distance2 = (center - Q).sqrMagnitude;
if (distance2 < radius - epsilon) return false;
return true;
}
如果您想包含边框,您可能需要
radius + epsilon
而不是 radius - epsilon
(假设您稍微增加圆的半径以包含边框)。也许你还必须交换 true
和 false
,因为目前当点在圆外时,该函数将返回 true。