uva 191 Intersection
来源:互联网 发布:淘宝靠谱香水代购 编辑:程序博客网 时间:2024/06/14 03:30
题目:判断线段是否与矩形相交。
分析:计算几何,简单题。先判断线段在矩形内的情况,然后判断线段与四边关系即可。
注意:线段在矩形内部属于相交,线段与边平行时的特判。
/*线段相交 */#include <iostream>#include <cstdio>#include <cstdlib>#define deff 1e-6//#define Tusing namespace std;struct Point{ double x;double y;};int direction(Point* pi,Point* pj,Point* pk){ Point p1,p2; p1.x = pk->x - pi->x; p1.y = pk->y - pi->y; p2.x = pj->x - pi->x; p2.y = pj->y - pi->y; double cross = p1.x*p2.y - p2.x*p1.y; if(cross > deff) return 1; else if(cross < -deff) return -1; else return 0;}bool onSegment(Point* pi,Point* pj,Point* pk){ double minx,miny,maxx,maxy; if(pi->x > pj->x) { minx = pj->x; maxx = pi->x; } else { minx = pi->x; maxx = pj->x; } if(pi->y > pj->y) { miny = pj->y; maxy = pi->y; } else { miny = pi->y; maxy = pj->y; } return (minx <= pk->x)&&(maxx >= pk->x)&&(miny <=pk->y)&&(maxy >= pk->y);}bool segmentIntersect(Point* p1,Point* p2,Point* p3,Point* 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*d2 < 0&&d3*d4 < 0) { // printf("1\n"); return true; } if(!d1 && onSegment(p3,p4,p1)) { //printf("2\n"); return true; } if(!d2 && onSegment(p3,p4,p2)) { // printf("3\n"); return true; } if(!d3 && onSegment(p1,p2,p3)) { // printf("4\n"); return true; } if(!d4 && onSegment(p1,p2,p4)) { // printf("5\n"); return true; } return false;}//判断线段是否在矩形内bool inRectangle(Point* p1,Point* p2,Point* p3,Point* p4){ return onSegment(p3,p4,p1)&&onSegment(p3,p4,p2);}int main(){ #ifdef T freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif Point p1,p2,p3,p4,p5,p6; bool b1,b2,b3,b4; int n; // int cas = 0; scanf("%d",&n); while(n--) { // printf("cas = %d\n",cas++); scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y); scanf("%lf %lf %lf %lf",&p3.x,&p3.y,&p5.x,&p5.y); if(inRectangle(&p1,&p2,&p3,&p5)) { printf("T\n"); continue; } p4.x = p5.x;p4.y =p3.y; p6.x = p3.x;p6.y =p5.y; b1 = segmentIntersect(&p1,&p2,&p3,&p4);b2 = segmentIntersect(&p1,&p2,&p4,&p5); b3 = segmentIntersect(&p1,&p2,&p5,&p6);b4 = segmentIntersect(&p1,&p2,&p6,&p3); if(b1||b2||b3||b4) printf("T\n"); else printf("F\n"); } return 0;}
0 0
- uva 191 Intersection
- UVa 191 - Intersection
- uva 191 Intersection
- poj 1410 || UVA 191 Intersection
- UVA 10321 Polygon Intersection
- UVA 191 Intersection POJ 1410【向量法判断线段相交】
- UVA 191 || Intersection (判断线段是否与矩形相交
- UVa 10790 How Many Points of Intersection?
- uva 10790 How Many Points of Intersection?
- uva 10790 - How Many Points of Intersection?
- uva-10790-How Many Points of Intersection?
- UVa 10790 - How Many Points of Intersection?
- Uva 10790 - How Many Points of Intersection?
- UVa 10790 - How Many Points of Intersection?
- UVA 10790 How Many Points of Intersection?
- uva 10790 - How Many Points of Intersection
- uva 10790 How Many Points of Intersection?
- UVa 10790 How Many Points of Intersection?
- 选择排序法
- Proxy 与 Agent 的区别
- Volley的简单实用
- 电压转电流op
- javascript笔试题(1)
- uva 191 Intersection
- InputStreamReader和OutputStreamWriter的用法
- Java TreeMap工作原理及实现(二)
- VB作业
- php函数imagejpeg,imagepng,imagegif参数详解
- 研读:Regulating ARM TrustZone Devices in Restricted Spaces
- Activiti生成流程图
- Tomcate 配置多个数据源
- Android Selector和Shape的组合