匿名用户
1级
2015-12-01 回答
你最近备考的是什么?全是计算几何的东西啊
上一个问题的大部分没变,加了一个判断符号的函数,一个求三点叉积的函数,判断三个叉积方向相同就行了。
#include
#include
typedef struct Point{ // 二维点或矢量
double x, y;
}Point;
Point decPoint(Point p1,Point p2){ //计算 p1 - p2
Point ret;
ret.x=p1.x - p2.x;
ret.y=p1.y - p2.y;
return ret;
}
double multiPoint(Point p1, Point p2) // 计算叉乘 p1 × p2
{
return (p1.x * p2.y - p2.x * p1.y);
}
double crossByThreePoint(Point A,Point B,Point C) //叉乘 (A->B)X(A->C)
{
return multiPoint(decPoint(B,A),decPoint(C,A));
}
int sign(double x){
double eps=1e-8;
if(fabs(x)
if(x>0) return 1;
return -1;
}
int main(){
int i,j,k;
Point pts[4];// 寸4个点
double x,y;
int sgn[3];
for(i=0;i<4;i++)
scanf("%lf%lf",&pts[i].x,&pts[i].y);
sgn[0]=sign(crossByThreePoint(pts[0],pts[1],pts[3]));
sgn[1]=sign(crossByThreePoint(pts[1],pts[2],pts[3]));
sgn[2]=sign(crossByThreePoint(pts[2],pts[0],pts[3]));
if(sgn[0]==0 || sgn[1]==0 || sgn[2]==0)
printf("Point D is on the triangle!");
else if(sgn[0]==sgn[1] && sgn[1]==sgn[2])
printf("Point D is in the triangle!");
else
printf("Point D is out of the triangle!");
return 0;
}