您的位置:首页 > 其它

FZU April 线段相交

2011-04-17 19:05 204 查看
/***********************************************************
** Description: http://acm.fzu.edu.cn/contest/problem.php?cid=113&sortid=4 ** Analysis:	(1)判断线段与三角形是否相交
1.相交
1.共线相交
2.非共线相交
2.不相交
可以通过一个点在三角形内/外来判断
1.在三角形内
2.在三角形外
** Source: FZU April D
** 2011-04-17 18:51:25
***********************************************************/
#include <iostream>
#include <stdio.h>
using namespace std;
struct pot {
int x, y;
};

pot operator - (pot a, pot b) {
pot c;
c.x = a.x-b.x;
c.y = a.y-b.y;
return c;
}

int MIN(int,int);
int MAX(int,int);
int chaji(pot,pot);					// 求两向量叉积
bool INTERSECT(pot,pot,pot,pot);	//判断是否相交
int DIRECTION(pot,pot,pot);			//求三点的叉积
bool ONSEG(pot,pot,pot);			//判断点是否在直线上,前提是有共线
bool INSIDE(pot,pot,pot,pot);		//判断点是否在区域内
bool ONELINE(pot,pot,pot,pot);		//判断是否共线,且有公共部分

int main() {
freopen("d.in", "r", stdin);
pot t1, t2, t3, a1, a2;
int n, i;
while (scanf("%d %d", &t1.x, &t1.y) == 2) {
scanf("%d %d", &t2.x, &t2.y);
scanf("%d %d", &t3.x, &t3.y);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d %d", &a1.x, &a1.y);
scanf("%d %d", &a2.x, &a2.y);
if (INTERSECT(a1,a2,t1,t2) || INTERSECT(a1,a2,t1,t3) || INTERSECT(a1,a2,t2,t3)) {
if (ONELINE(a1,a2,t1,t2) || ONELINE(a1,a2,t1,t3) || ONELINE(a1,a2,t2,t3)) {
printf("ON_EDGE/n");
} else {
printf("SIM_INT/n");
}
} else {
if (!INSIDE(t1,t2,t3,a1) || !INSIDE(t2,t1,t3,a1) || !INSIDE(t3,t1,t2,a1)) {//OUT_LINE
printf("OUT_SIDE/n");
} else {
printf("IN_SIDE/n");
}//INLINE
}
}
}
}

int MIN(int a, int b) {
if (a < b) return a; else return b;
}
int MAX(int a, int b) {
if (a < b) return b; else return a;
}
bool INTERSECT(pot p1, pot p2, pot p3, pot p4) {
int d1 = DIRECTION(p3,p4,p1);
int d2 = DIRECTION(p3,p4,p2);
int d3 = DIRECTION(p1,p2,p3);
int d4 = DIRECTION(p1,p2,p4);
if ( ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) &&
((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)) )
return true;
if (d1 == 0 && ONSEG(p3,p4,p1)) return true;
if (d2 == 0 && ONSEG(p3,p4,p2)) return true;
if (d3 == 0 && ONSEG(p1,p2,p3)) return true;
if (d4 == 0 && ONSEG(p1,p2,p4)) return true;
return false;
}
int chaji(pot a, pot b) {
return a.x*b.y-a.y*b.x;
}
int DIRECTION(pot p1, pot p2, pot p3) {
return chaji(p2-p1,p3-p1);
}
bool ONSEG(pot i, pot j, pot k) {
if (MIN(i.x,j.x) <= k.x && k.x <= MAX(i.x,j.x) &&
MIN(i.y,j.y) <= k.y && k.y <= MAX(i.y,j.y))
return true;
else return false;
}
bool INSIDE(pot a, pot b, pot c, pot r) {
int d1 = DIRECTION(a,b,r);
int d2 = DIRECTION(a,r,c);
if ((d1 > 0 && d2 > 0) || (d1 < 0 && d2 < 0)) return true; else return false;
}
bool ONELINE(pot a,pot b, pot c, pot d) {
int d1 = DIRECTION(a,b,c);
int d2 = DIRECTION(a,b,d);
if (d1 == 0 && d2 == 0) {
if (ONSEG(a,b,c) || ONSEG(a,b,d) || ONSEG(c,d,a) || ONSEG(c,d,b))
return true;
else
return false;
} else return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: