判断空间某点是否在三角形内

方法一:内角之和等于360度或面积之和等于ABC面积;
以二维点为例:三维同理;

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<unordered_map>
#include <bitset>

using namespace std;
struct point{
    int x;
    int y;
    point(int i,int j):x(i),y(j){};
};
double areas(point A,point P,point B)
{
    point AP(0,0);
    point PB(0,0);
    AP.x=P.x-A.x;
    PB.x=B.x-P.x;
    AP.y=P.y-A.y;
    PB.y=B.y-P.y;
    double s=(AP.x*PB.y-AP.y*PB.x)/2.0;
    return s;
}
bool Trigon1(point A,point B,point C,point P)
{
    double apb=areas(A,P,B);
    double bpc=areas(B,P,C);
    double cpa=areas(C,P,A);
    double abc=areas(A,B,C);
    cout<<apb<<"  "<<bpc<<" "<<cpa<<endl;
    cout<<abc<<endl;
    double inacc=apb+bpc+cpa+abc;
    if(-0.001<inacc && inacc<0.001)
        return true;
    else
        return false;
}
int main() {
        char *ch="hello world! ";
        point A(0,4);
        point B(0,0);
        point C(4,0);
        point P(0,1);
        bool t1=Trigon1(A,B,C,P);
        cout<<"t1="<<t1<<endl;
    }

在这里插入图片描述
方法二:p点一定在AB边下边,AC边右边,BC边左边;

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<unordered_map>
#include <bitset>

using namespace std;
class Vector3
{
public:
    Vector3(float fx, float fy, float fz)
        :x(fx), y(fy), z(fz)
    {
    }
    // Subtract
    Vector3 operator - (const Vector3& v) const
    {
        //传进来的参数是引用类型,const不允许修改;
        return Vector3(x - v.x, y - v.y, z - v.z) ;
    }

    // Dot product 点乘=|a|*|b|*cos&;
    float Dot(const Vector3& v) const
    //const不允许修改*this指针指向的对象
    {
        return x * v.x + y * v.y + z * v.z ;
    }

    // Cross product 叉积,向量积、叉乘
    //两向量叉积与这两向量组成的坐标面垂直;
    Vector3 Cross(const Vector3& v) const
    { //(x,  y,  z)
      //(v.x,v.y,v.z)
        return Vector3(
            y * v.z - z * v.y,
            z * v.x - x * v.z,
            x * v.y - y * v.x ) ;
    }

public:
    float x, y, z ;
};
// Determine whether two vectors v1 and v2 point to the same direction
// v1 = Cross(AB, AC)
// v2 = Cross(AB, AP)
bool SameSide(Vector3 A, Vector3 B, Vector3 C, Vector3 P)
{
    Vector3 AB = B - A ;
    Vector3 AC = C - A ;
    Vector3 AP = P - A ;
    //满足右手定则(按夹角为锐角,顺时针),顺时针由a到b,
    //大拇指的方向就是e的方向,AP x AB  与 AC x AB 叉乘的结果是同向的,
    //方向都是垂直三角形ABC这个平面朝上的,
    Vector3 v1 = AB.Cross(AC) ;
    Vector3 v2 = AB.Cross(AP) ;
    // v1 and v2 should point to the same direction
    return v1.Dot(v2) >= 0 ;
}
// Same side method
// Determine whether point P in triangle ABC
bool PointinTriangle1(Vector3 A, Vector3 B, Vector3 C, Vector3 P)
{
    return SameSide(A, B, C, P) &&
        SameSide(B, C, A, P) &&
        SameSide(C, A, B, P) ;
}

int main() {
        char *ch="hello world! ";
        Vector3 A(0,4,0);
        Vector3 B(4,0,0);
        Vector3 C(0,0,0);
        Vector3 P(1,1,0);
        bool b=PointinTriangle1(A,B,C,P);
        cout<<b<<endl;
    }

猜你喜欢

转载自blog.csdn.net/qq_42698422/article/details/106050930