#1450 : Inside Triangle

本文介绍了一种用于确定点是否位于二维三角形内的算法。通过输入三角形顶点坐标及待检测点坐标,算法能准确判断点是否位于三角形内部,包括边界上的点。适用于计算机图形学、游戏开发等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述

Determine if a point is inside a 2D triangle.

输入

The first line contains an integer T denoting the number of test case. (1 <= T <= 10)

The following T lines each contain 8 integers, Px, Py, Ax, Ay, Bx, By, Cx, Cy. P denotes the point. ABC denotes the triangle. The coordinates are within [-1000000, 1000000].

输出

For each test case output YES or NO denoting if the point is inside the triangle. Note that the point is considered inside the triangle if the point is just on some side of the triangle.

样例输入

2  
0 1 -1 0 1 0 0 2  
0 0 0 1 1 0 1 1

样例输出

YES  
NO

图片来源

 

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=100;
LL X[10],Y[10];

LL PX[10],PY[10];

int main()
{
	int n;
	scanf("%d",&n);
	while(n--){
		for(int i=0;i<=3;i++){
			scanf("%lld %lld",&X[i],&Y[i]);
		}

		for(int i=0;i<3;i++){
			PX[i]=X[0]-X[i+1];
			PY[i]=Y[0]-Y[i+1];
		}

		PX[3]=PX[0],PY[3]=PY[0];

		int f1=PX[0]*PY[1]-PX[1]*PY[0]>0?1:-1,f2=0;

		for(int i=0;i<3;i++){
			LL tmp=PX[i]*PY[i+1]-PX[i+1]*PY[i];
			if(tmp==0&&PX[i]*PX[i+1]+PY[i]*PY[i+1]<=0){
				f2=1;break;
			}

			tmp=tmp<0?-1:1;
			if(tmp!=f1){
				f1=0;break;
			}
		}
		//printf("f1:%d f2:%d\n",f1,f2);
		if(!f1&&!f2)printf("NO\n");
		else printf("YES\n");
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值