判断点是否在三角形内

本文介绍了两种判断三维空间中点是否在三角形内的方法:SameSide代码实现和重心坐标法。 SameSide方法基于向量叉乘判断点的位置,而重心坐标法利用坐标系和向量比例来高效判断。文中提供了详细的代码实现,并指出重心坐标法在效率上优于SameSide方法。同时,解释了如何通过重心坐标计算公式来确定点在三角形内的条件。


参考[1]的话比我看得其他几篇文章更加的清晰,所以可以直接看它就可以了。

方法一:Same Side

代码实现

参考[1],用的是Eigen库

static bool SameSide(const Vector3f& p1, const Vector3f& p2, const Vector3f& a, const Vector3f& b)
{
   
   
    Vector3f ab = b - a;
    Vector3f ap1 = p1 - a;
    Vector3f cp1 = ab.cross(ap1);
	Vector3f ap2 = p2 - a;
    Vector3f cp2 = ab.cross(ap2);
    if (cp1.dot(cp2) >= 0)
        return true;
    else
        return false;
}

static bool IsInsideTriangle(Vector3f& p, const Vector3f* _v)
{
   
      
    if (SameSide(p, _v[0], _v[1], _v[2]) && SameSide(p,_v[1], _v[0], _v[2]) && SameSide(p, _v[2], _v[0], _v[1]))
        return true;
    else
        return false;
}

方法二:重心坐标法

参考[1],比方法一更加高效。简单测了一下,方法二比方法一快了一倍!!!

So remember that the three points of the triangle define a plane in space. Pick one of the 	points and we can consider all other locations on the plane as relative to that point. Let's go with A -- it'll be our origin on the plane. Now what we need are basis vectors so we can give coordinate values to all the locations on the plane. We'll pick the two edges of the triangle that touch A, (C - A) and (B - A). Now we can get to any point on the plane just by starting at A and walking some distance along (C - A) and then from there walking some more in the direction (B - A).

With that in mind we can now describe any point on the plane as

    P = A + u * (C - A) + v * (B - A)
Notice now that if u or v < 0 then we've walked in the wrong direction and must be outside the triangle. Also if u or v > 1 then we've walked too far in a direction and are outside the triangle. Finally if u + v > 1 then we've crossed the edge BC again leaving the triangle.

在这里插入图片描述
                     (图1)

重心坐标

看上面的描述,那么任意点$P$可以用下面这个表达式描述:

### 面积法判断是否三角形内的C++代码实现 以下是使用面积法判断是否三角形内的C++代码示例。该方法通过计算三角形的总面积以及由测试三角形构成的三个子三角形的面积,来判断测试是否位于三角形内部。 ```cpp #include <iostream> #include <cmath> using namespace std; // 计算三角形面积的函数 float area(int x1, int y1, int x2, int y2, int x3, int y3) { return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0); } // 判断是否三角形内 bool isInside(int x1, int y1, int x2, int y2, int x3, int y3, int x, int y) { // 计算三角形ABC的总面积 float A = area(x1, y1, x2, y2, x3, y3); // 计算三角形PBC的面积 float A1 = area(x, y, x2, y2, x3, y3); // 计算三角形PAC的面积 float A2 = area(x1, y1, x, y, x3, y3); // 计算三角形PAB的面积 float A3 = area(x1, y1, x2, y2, x, y); // 如果总面积等于三个子三角形面积之和,则三角形内 if (A == A1 + A2 + A3) return true; else return false; } int main() { // 定义三角形的三个顶坐标 int x1 = 0, y1 = 0; int x2 = 20, y2 = 0; int x3 = 10, y3 = 30; // 定义测试坐标 int x = 10, y = 15; // 判断测试是否三角形内 if (isInside(x1, y1, x2, y2, x3, y3, x, y)) cout << "Inside"; else cout << "Not Inside"; return 0; } ``` 上述代码中,`area` 函数用于计算三角形的面积[^3],而 `isInside` 函数则根据面积法判断是否位于三角形内部。如果测试三角形构成的三个子三角形面积之和等于原三角形的总面积,则测试位于三角形内部[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值