0
点赞
收藏
分享

微信扫一扫

判断·点与三角形位置关系

柠檬的那个酸_2333 2022-02-06 阅读 45
c++

叉乘法

理论依据
P0P1−→−−×P0P2−→−−>0↔P1在P2的顺时针方向(相对P0)P0P1→×P0P2→>0↔P1在P2的顺时针方向(相对P0)
P0P1−→−−×P0P2−→−−<0↔P1在P2的逆时针方向(相对P0)P0P1→×P0P2→<0↔P1在P2的逆时针方向(相对P0)
P0P1−→−−×P0P2−→−−=0↔P1,P2,P0三点共线P0P1→×P0P2→=0↔P1,P2,P0三点共线

仍然连结第四个点和三角形的三个顶点,算出三个叉积,如果同正或同负,则在三角形内;

如果有一个为0,另两个同号,则在三角形边上。

在顶点特判即可

#include <bits/stdc++.h>
#define eps 1e-6
using namespace std;
int vec(int x1, int y1, int x2, int y2, int x0, int y0) {
	return (x1-x0) * (y2-y0) - (x2-x0) * (y1-y0);
}
int main() {
	int x1,y1,x2,y2,x3,y3,x,y;
	scanf("(%d,%d)\n(%d,%d)\n(%d,%d)\n(%d,%d)", &x1, &y1, &x2, &y2, &x3, &y3, &x, &y);
	if ((x==x1&&y==y1) || (x==x2&&y==y2) || (x==x3&&y==y3)) {
		putchar('4');
		return 0;
	}
	int c1 = vec(x1, y1, x2, y2, x, y),
	    c2 = vec(x2, y2, x3, y3, x, y),
	    c3 = vec(x3, y3, x1, y1, x, y);
	if ((c1>0&&c2>0&&c3>0) || (c1<0&&c2<0&&c3<0)) {
		putchar('1');
		return 0;
	}
	if ((c1==0&&c2*c3>0) || (c2==0&&c1*c3>0) || (c3==0&&(c1*c2>0))) putchar('3');
	else putchar('2');
	return 0;
}

举报

相关推荐

0 条评论