如何确定一个三角形的一个点?角形

2023-09-10 22:22:58 作者:你是我的梦

有没有一种简单的方法来确定一个三角形内一个点?这是2D不是3D。

Is there an easy way to determine if a point inside a triangle? It's 2D not 3D.

推荐答案

在一般情况下,最简单的(和相当最佳)算法检查在其上的边缘点是

In general, the simplest (and quite optimal) algorithm is checking on which side of the half-plane created by the edges the point is.

下面是在GameDev 这话题,包括性能问题,一些高品质的信息。

Here's some high quality info in this topic on GameDev, including performance issues.

和这里的一些code,让你开始:

And here's some code to get you started:

float sign (fPoint p1, fPoint p2, fPoint p3)
{
    return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}

bool PointInTriangle (fPoint pt, fPoint v1, fPoint v2, fPoint v3)
{
    bool b1, b2, b3;

    b1 = sign(pt, v1, v2) < 0.0f;
    b2 = sign(pt, v2, v3) < 0.0f;
    b3 = sign(pt, v3, v1) < 0.0f;

    return ((b1 == b2) && (b2 == b3));
}
 
精彩推荐
图片推荐