如何确定一个点是否在二维三角形中? [英] How to determine if a point is in a 2D triangle?

查看:128
本文介绍了如何确定一个点是否在二维三角形中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来确定一个点是否在三角形内?它是2D的,而不是3D。

解决方案

一般来说,最简单的(也是最佳的)算法是检查一半 - 由该边缘创建的平面。



以下是 GameDev主题,包括性能问题。



这里有一些代码让你开始:
$ b

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));
}


Is there an easy way to determine if a point is 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.

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

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));
}

这篇关于如何确定一个点是否在二维三角形中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆