如果在一个二维坐标系中,已知三角形三个点的坐标,那么对于坐标系中的任意一点,如何判断该点是否在三角形内(点在三角形边线上也认为在三角形内)?
Input
本问题有多组测试数据,每组数据一行,共8个实数,每两个实数表示一个点,前三个点表示三角形的三个点,后一个点表示需要你判断是不是在内部的点。
Output
如果再三角形内(边上也算内),输出“Yes”,否则输出“No”。
Sample Input
0.0 0.0 4.0 4.0 6.0 0.0 3.0 2.5
Sample Output
Yes
#include<bits/stdc++.h>
using namespace std;
const double PI = 4.0 * atan(1.0);
const double EPS = 1e-6;
struct point
{
double x,y;
point (double _x = 0,double _y = 0)
{
x = _x;
y = _y;
}
};
typedef point vect;
bool operator < (const point& a,const point& b)
{
if(a.x!=b.x)
return a.x<b.x;
return a.y<b.y;
}
int dcmp(double x)
{
return x<-EPS?-1:(x>EPS?1:0);
}
bool operator == (const point& a,const point& b)
{
return !dcmp(a.x-b.x)&&!dcmp(a.y-b.y);
}
bool operator != (const point& a,const point& b)
{
return dcmp(a.x-b.x)||dcmp(a.y-b.y);
}
vect operator + (vect a,vect b)
{
return vect (a.x+b.x,a.y+b.y);
}
vect operator - (point a,point b)
{
return vect (a.x-b.x,a.y-b.y);
}
vect operator * (vect a,double b)
{
return vect(a.x*b,a.y*b);
}
vect operator / (vect a,double b)
{
return vect(a.x/b,a.y/b);
}
double dot(vect a,vect b)
{
return a.x*b.x+a.y*b.y;
}
double cross(vect a,vect b)
{
return a.x*b.y-b.x*a.y;
}
double vect_len(vect a)
{
return sqrt(dot(a,a));
}
vect vect_rotate(vect a,double rad)
{
return vect(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
vect vect_normal(vect a)
{
double L=vect_len(a);
return vect(-a.y/L,a.x/L);
}
point a,b,c,d;
int main()
{
while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&d.x,&d.y)!=EOF)
{
if(dcmp(abs(cross(b-a,c-a))-abs(cross(d-a,d-b))-abs(cross(d-b,d-c))-abs(cross(d-c,d-a)))==0)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}