不包括点在边线上的情况,判断点是否在三角形内,仍然用面积法进行判断
边线上的情形用向量平行的判定条件设向量AB(x1,y1),向量AD(x2,y2)若x1y2-x2y1==0则这两向量平行,表面D要么在边线上要么在三角形外
均属于不在三角形内的情形
其余的判定为点D与三边围成的面积之和是否等于三角形ABC的面积
三角形的面积用海伦公式计算
#include<iostream>
#include<cmath>
using namespace std;
struct point{
point(double i,double j):x(i),y(j){}
double x,y;
};
void computeEdgeLen(point A,point B,
point C,double& a,double &b,double &c){
a=sqrt(pow(B.x-A.x,2)+pow(B.y-A.y,2));
b=sqrt(pow(C.x-B.x,2)+pow(C.y-B.y,2));
c=sqrt(pow(A.x-C.x,2)+pow(A.y-C.y,2));
}
double Area(point A,point B,point C)
{
double a,b,c=0;
computeEdgeLen(A,B,C,a,b,c);
double p=(a+b+c)/2;
//海伦公式
return sqrt(p*(p-a)*(p-b)*(p-c));
}
bool isParallel(point A,point B,point C){
if((B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x)==0)
return true;
else
return false;
}
bool isTriangle(point A,point B,point C,point D)
{
if(isParallel(A,B,D)||
isParallel(B,C,D)||isParallel(C,A,D))
return false;
return (Area(A,B,D)+Area(B,C,D)+Area(C,A,D)<=Area(A,B,C));
}
int main(){
point A(0,2);
point B(0,0);
point C(1,0);
point D(0.1,0);
if(isTriangle(A,B,C,D))
cout<<"true"<<endl;
else
cout<<"false"<<endl;
system("pause");
return 0;
}