#include<iostream>
#include<math.h>
using namespace std;
#define err 0.0001
struct Node
{
float x;
float y;
};
float TriangleArea(Node p1, Node p2, Node p3)//海伦公式
{
float AB, BC, AC,P;
AB = sqrt(pow(p2.x - p1.x,2)+ pow(p2.y - p1.y, 2));
AC = sqrt(pow(p3.x - p1.x, 2) + pow(p3.y - p1.y, 2));
BC = sqrt(pow(p3.x - p2.x, 2) + pow(p3.y - p2.y, 2));
P = (AB + AC + BC) / 2;
return sqrt(P*(P-AB)*(P-AC)*(P-BC));
}
bool IsInTriangle(Node A, Node B, Node C, Node D)
{
float S1, S2, S3, Ssum;
S1 = TriangleArea(A, B, D);
S2 = TriangleArea(A, C, D);
S3 = TriangleArea(B, C, D);
Ssum = TriangleArea(A,B,C);
if (err > fabs(Ssum - S1 - S2 - S3))//注意绝对值
return true;
else
return false;
}
int main()
{
Node A, B, C,D;
A.x = 1; A.y = 0;
B.x = 3; B.y = 0;
C.x = 0; C.y = 3;
D.x = 2; D.y = 0.5;
cout << "是否在三角形内:" << IsInTriangle(A, B, C, D) << endl;
system("pause");
return 0;
}
c++之判断一个点是否在三角形内
最新推荐文章于 2024-03-27 10:57:34 发布