版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/84933520
地址:https://ac.nowcoder.com/acm/contest/289/D
思路:点p在三角形内,就要有点P和C在直线AB同侧,直线P和A在直线BC同侧,直线P和B在直线AC同侧。
当同时满足时,P在三角形ABC内。
判断是否同侧
直线方程为 (y-y1)(x1-x2)-(y1-y2)(x-x1)=0
当 (y-y1)(x1-x2)-(y1-y2)(x-x1)>0 时,点(x,y)在直线的右侧
当 (y-y1)(x1-x2)-(y1-y2)(x-x1)<0 时,点(x,y)在直线的左侧
Code:
#include<iostream>
using namespace std;
/*
点p在三角形内,就要有点P和C在直线AB同侧,直线P和A在直线BC同侧,直线P和B在直线AC同侧。
当同时满足时,P在三角形ABC内。
判断是否同侧
直线方程为 (y-y1)(x1-x2)-(y1-y2)(x-x1)=0
当 (y-y1)(x1-x2)-(y1-y2)(x-x1)>0 时,点(x,y)在直线的右侧
当 (y-y1)(x1-x2)-(y1-y2)(x-x1)<0 时,点(x,y)在直线的左侧
*/
bool IsInTri(int x1,int y1,int x2,int y2,int x3,int y3,int x,int y);
int main()
{
int x1,y1,x2,y2,x3,y3,x,y;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3>>x>>y){
if(IsInTri(x1,y1,x2,y2,x3,y3,x,y)) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
bool IsInTri(int x1,int y1,int x2,int y2,int x3,int y3,int x,int y)
{
int d=(y-y1)*(x2-x1)-(y2-y1)*(x-x1);
int q=(y3-y1)*(x2-x1)-(y2-y1)*(x3-x1);
if(d*q<0) return false;
d=(y-y2)*(x3-x2)-(y3-y2)*(x-x2);
q=(y1-y2)*(x3-x2)-(y3-y2)*(x1-x2);
if(d*q<0) return false;
d=(y-y3)*(x1-x3)-(y1-y3)*(x-x3);
q=(y2-y3)*(x1-x3)-(y1-y3)*(x2-x3);
if(d*q<0) return false;
return true;
}