UVA 378 Intersecting Lines POJ 1249【直线位置情况简单判定】
2013-05-03 00:21
309 查看
题目大意:判断一组两条直线(是直线不是线段)位置情况:
1,相交,按照格式输出交点;
2,平行,输出“NONE“;
3,重叠,输出”LINE“;
解题策略:1, 由于一直考虑用求向量叉积求解,所以抛弃解析几何方法,题目简单,需要仔细耐心;
2, 直线相交情况复杂,所以一开始一直wa,考虑首先通过求叉积求两条直线是否平行(包含共线),
继而判断共线(重叠)与平行(不共线情况),
如果不属于上述两种情况,则最后一种情况必定是相交,直接计算出交点并输出;
1,相交,按照格式输出交点;
2,平行,输出“NONE“;
3,重叠,输出”LINE“;
解题策略:1, 由于一直考虑用求向量叉积求解,所以抛弃解析几何方法,题目简单,需要仔细耐心;
2, 直线相交情况复杂,所以一开始一直wa,考虑首先通过求叉积求两条直线是否平行(包含共线),
继而判断共线(重叠)与平行(不共线情况),
如果不属于上述两种情况,则最后一种情况必定是相交,直接计算出交点并输出;
/* UVA 378 Intersecting Lines AC by J_Dark ON 2013/5/3 0:00 Time 0.012s */ #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <climits> #include <vector> #include <algorithm> using namespace std; typedef long long int LL; struct point{ LL x, y; point(LL p=0, LL q=0){ x = p; y = q; } }; struct line{ point a, b; line(point x, point y){ a = x; b = y; } }; vector<line> Seg; double ansX, ansY; ////////////////////////////////// void Input(){ Seg.clear(); LL ax1, ay1, bx1, by1, ax2, ay2, bx2, by2; cin >> ax1 >> ay1 >> bx1 >> by1 >> ax2 >> ay2 >> bx2 >> by2; point s1(ax1, ay1), e1(bx1, by1), s2(ax2, ay2), e2(bx2, by2); Seg.push_back(line(s1, e1)); Seg.push_back(line(s2, e2)); } double Direction(point Pi, point Pj, point Pk){ return (Pj.x-Pi.x)*(Pk.y-Pi.y)-(Pk.x-Pi.x)*(Pj.y-Pi.y); } //判断向量p q平行(包含共线情况) bool isParallel(line p, line q){ if( !((p.b.x-p.a.x)*(q.b.y-q.a.y)-(q.b.x-q.a.x)*(p.b.y-p.a.y)) ) return true; return false; } //判断线段p,q是否相交 int isIntersect(line p, line q){ LL d1, d2, d3, d4; d1 = Direction(p.a, p.b, q.a); d2 = Direction(p.a, p.b, q.b); d3 = Direction(q.a, q.b, p.a); d4 = Direction(q.a, q.b, p.b); if(isParallel(p, q)){ if(((!d1 && !d2) || (!d3 && !d4))){ //重叠 return 2; } else return 3; //平行不共线 } //相交 else{ ansX = (double)(q.a.x*d2 - q.b.x*d1)/(d2 - d1); ansY = (double)(q.a.y*d2 - q.b.y*d1)/(d2 - d1); return 1; } } void Compute(){ int ff; ff = isIntersect(Seg[0], Seg[1]); if(ff == 1){//相交 输出交点 printf("POINT %.2lf %.2lf\n", ansX, ansY); } if(ff == 2) cout << "LINE" << endl; //重叠 if(ff == 3) cout << "NONE" << endl; //平行 } ////////////////////////////////// int main(){ int testCase; while(cin >> testCase) { cout << "INTERSECTING LINES OUTPUT" << endl; while(testCase--) { Input(); Compute(); } cout << "END OF OUTPUT" << endl; } return 0; }
相关文章推荐
- 简单几何(线段与直线的位置) POJ 3304 Segments
- poj1269——Intersecting Lines//直线交点
- 简单几何(直线位置) POJ 1269 Intersecting Lines
- POJ - 1269 Intersecting Lines 两直线的位置关系
- POJ 2318 TOYS + POJ 2398 Toy Storage (点与直线的位置关系)
- POJ 1039 Pipe 判断直线与线段相交(规范相交, 非规范相交),没有共线情况
- POJ 1269 || Intersecting Lines(直线重合,平行,相交判定
- poj 1269 两条直线的位置关系
- POJ1269 判断2条直线的位置关系
- POJ 1269 Intersecting Lines(判断两条直线的位置关系)
- Intersecting Lines POJ - 1269
- POJ 3304 Segments(直线与线段相交判定)
- POJ1269_Intersecting Lines(计算几何/两直线位置关系模板)
- Intersecting Lines POJ - 1269
- POJ 1264 UVA 109 简单的计算几何
- poj 1269 判断两直线位置
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
- POJ 2318 TOYS(计算几何+点与直线位置关系+二分)
- UVA 10180 Rope Crisis in Ropeland!【判断圆与直线位置】
- POJ_1269_Intersecting_Lines_(计算几何基础)