uva 378Intersecting Lines
来源:互联网 发布:linux c 开源项目 编辑:程序博客网 时间:2024/06/16 00:04
题意:判断两条直线之间的关系,相交,平行,或者同一条直线,相交的话给出交点
解题:数学问题,分情况讨论:先判断直线之间的关系,利用向量叉积不为0就相交,叉积为0可能是同一条直线,例如(p1,p2)(p3,p4) ,计算p1,p2),(p1,p3)的叉积,等于0 ,就是同一条直线了,否则,就是平行。接着就是计算两条相交直线的交点,(肯定有交点了),然后看斜率是否存在,分步讨论k1 和 k2 是否存在
代码:
//uva 378 Intersecting Lines //AC By Warteac//2013-5-8//Runtime:0.009s#include<iostream>#include <iomanip>using namespace std;////////////////////////////////////////////////////////////////struct point{ int x,y; point(int x1 = 0, int y1 = 0){x = x1; y = y1;} void print(){cout << x << y << endl;} };//定义点结构体 struct point2{ double x,y; point2(double x1 = 0,double y1 = 0){x = x1; y = y1;} void print(){ cout.precision(3); cout.setf(ios_base::showpoint);//show point cout << x << " " << y << endl;} }; int direction(point v1,point v2){ return (v1.x*v2.y-v1.y*v2.x); } bool isLine(point p1,point p2,point p3,point p4){ point p(p1.x - p2.x, p1.y - p2.y); point q(p1.x - p3.x, p1.y - p3.y); int d = direction(p,q); if(d) return false; else return true; }int isItersect(point p1,point p2,point p3,point p4){ point p (p1.x - p2.x, p1.y - p2.y); point q (p3.x - p4.x, p3.y - p4.y); int d1 = direction(p,q); if(d1) return 1;//intersect else if(isLine(p1,p2,p3,p4)) return -1;// a line else return 0; // parallel}point2 intersectPoint(point p1,point p2,point p3,point p4){ if(p1.x == p2.x){//line1 has no slope double k2 = (double)(p4.y - p3.y)/(p4.x - p3.x); double b2 = p3.y - k2*p3.x; int a = p1.x; double y = k2*a + b2; return point2(a,y); } if(p3.x == p4.x){//line2 has no slope double k1 = (double)(p2.y - p1.y)/(p2.x - p1.x); double b1 = p1.y - k1*p1.x; int b = p3.x; double y = k1*b + b1; return point2(b,y); } double k1 = (double)(p2.y - p1.y)/(p2.x - p1.x); double k2 = (double)(p4.y - p3.y)/(p4.x - p3.x); double b1 = p1.y - k1*p1.x; double b2 = p3.y - k2*p3.x; double x3 = (b1 - b2)/(k2 - k1); double y3 =(k1*b2 - k2*b1)/(k1 - k2); // cout << k1 << " "<<k2<<" "<<b1 <<" " << b2 <<endl; return point2(x3,y3);}////////////////////////////////////////////////////////////////int main(){ int caseNum; cin >> caseNum; int x1,y1,x2,y2,x3,y3,x4,y4; cout << "INTERSECTING LINES OUTPUT" << endl; while(caseNum--){ cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; point p1(x1,y1); point p2(x2,y2); point p3(x3,y3); point p4(x4,y4); int t = isItersect(p1,p2,p3,p4); if(t == 1){ point2 r = intersectPoint(p1,p2,p3,p4); cout << "POINT "; r.print(); }else if(t == -1) cout << "LINE" << endl; else cout << "NONE" <<endl; } cout << "END OF OUTPUT" << endl; return 0;}
- uva 378Intersecting Lines
- UVa 378 - Intersecting Lines
- UVa 378 - Intersecting Lines
- UVa 378 - Intersecting Lines
- uva 378 Intersecting Lines
- uva 378 Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- UVA 378 Intersecting Lines POJ 1249【直线位置情况简单判定】
- zoj1820 Intersecting Lines
- POJ 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- poj 1269 Intersecting Lines
- POJ 1269 Intersecting Lines
- ecmall完善配送方式添加功能
- 模仿网易新闻部分界面
- 把二叉树转换成双向链表
- 删除多余的记录(面试必考,必须搞懂)
- POJ3278 Catch That Cow
- uva 378Intersecting Lines
- Android的DialogFragment的基本使用方法
- trie树
- SqlServer2005存储过程执行BCP命令
- Java生成PDF文件(依赖于iText-2.1.0.jar)
- JSF怎样根据判断条件显示不同文本
- 使用OpenGL和OpenCV分别显示三维模型和图像
- C#网站一般处理程序(ashx)中session的问题
- 关于字节对齐,和程序优化有关