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; }
相关文章推荐
- fzu 1015 土地划分(判断线段相交+求出交点+找规律)
- fzu acm 1015 土地划分 (线段相交)
- FZU 2273 Triangles(判断线段相交和点在多变形内)
- 计算几何 ( 判断线段相交 )——土地划分( FZU 1015 )
- Fzu 2148 Moon Game 判断凸四边形 判断线段相交
- zoj 1010 (线段相交判断+多边形求面积)
- 两线段相交的判别方法
- poj 1410 判断线段和矩形是否相交
- POJ 2653 Pick-up sticks(线段相交)
- HDOJ 1086 线段相交
- 线段相交
- HDU 1086 You can Solve a Geometry Problem too 线段相交
- POJ 2074 Line of Sight 直线相交+线段覆盖
- POJ 3304 Segments [枚举+叉乘判断线段相交]
- POJ 1066 Treasure Hunt --几何,线段相交
- 【计算几何初步-线段相交】【HDU1089】线段交点
- 判断线段相交并求交点
- 判断两个线段相交
- POJ3304 Segments 【线段直线相交】
- poj 2653 线段相交