poj 1269 Intersecting Lines[直线的关系]
来源:互联网 发布:那些引擎源码能用 编辑:程序博客网 时间:2024/05/20 06:28
题目链接: poj 1269
题目的意思很是简单,两条直线的关系,平行,共线,或者给出交点。
Code:
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-8;struct POINT{ double x, y;};double cross(POINT o, POINT a, POINT b){ return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);}double cross(POINT a, POINT b, POINT c, POINT d){ return (b.x - a.x) * (d.y - c.y) - (d.x - c.x) * (b.y - a.y);}double f(POINT a, POINT b, double x){ return x * (b.y - a.y) + a.y * b.x - a.x * b.y;}void TheRelationshipOfTwoLine(POINT p1, POINT p2, POINT p3,POINT p4){ //p1p2,p3p4 if(fabs(cross(p1, p2, p3)) < eps && fabs(cross(p1, p2, p4)) < eps){// line puts("LINE"); } else if(fabs(cross(p1, p2, p3, p4)) < eps){// // puts("NONE"); } else { if(p2.x == p1.x && p3.x != p4.x){// special judge p1p2 |x printf("POINT %.2f %.2f\n", p2.x, f(p3, p4, p2.x) / (p4.x - p3.x)); } else if(p3.x == p4.x && p1.x != p2.x){ // special judge p3p4 |x printf("POINT %.2f %.2f\n", p3.x, f(p1, p2, p3.x) / (p2.x - p1.x)); } else{ double k1 = (p2.y - p1.y) / (p2.x - p1.x), k2 = (p4.y - p3.y) / (p4.x - p3.x); double x = (p1.x * k1 - p3.x * k2 + p3.y - p1.y) / (k1 - k2); double y = k1 * (x - p1.x) + p1.y; printf("POINT %.2f %.2f\n",x, y); } }}int main(){ int T; scanf("%d", &T); puts("INTERSECTING LINES OUTPUT"); while(T --){ POINT p1, p2, p3, p4; scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y, &p4.x, &p4.y); TheRelationshipOfTwoLine(p1, p2, p3, p4); } puts("END OF OUTPUT"); return 0;<span style="font-size:18px;">}</span>
留下小小的模板吧。。。!
代码比较优美的又写了一遍,不过这次用到的不是求斜率来求解交点。而是应用到了定比分点公式来进行求解。。。
Code:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int N = 20;const double eps = 1e-8;const double pi = acos(-1);//点struct POINT{ double x, y; POINT(){ } POINT(double a, double b){ x = a; y = b; }};//直线struct Line{ POINT a, b; Line() {} Line(POINT x, POINT y){ a = x; b = y; }}l1, l2;//叉乘double cross(POINT o, POINT a, POINT b){ return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y);}int Line_cross(Line l1, Line l2){ //两条直线平行 if(fabs(cross(l1.a, l2.a, l2.b) - cross(l1.b, l2.a, l2.b)) < eps && fabs(cross(l1.a, l2.a, l2.b)) > eps) return 0; //重合.. if(fabs(cross(l1.a, l2.a, l2.b)) < eps && fabs(cross(l1.b, l2.a, l2.b)) < eps) return 2; //有交点/// return 1;}POINT Inter(Line l1, Line l2){ double k = fabs(cross(l1.a, l2.a, l2.b)) / fabs(cross(l1.b, l2.a, l2.b)); return POINT((l1.a.x + l1.b.x * k) / (1 + k), (l1.a.y + l1.b.y * k) / (1 + k));}int main(){ int n; while(~scanf("%d", &n)){ puts("INTERSECTING LINES OUTPUT"); for(int i = 0; i < n; i ++){ scanf("%lf %lf %lf %lf", &l1.a.x, &l1.a.y, &l1.b.x, &l1.b.y); scanf("%lf %lf %lf %lf", &l2.a.x, &l2.a.y, &l2.b.x, &l2.b.y); int cnt = Line_cross(l1, l2); if(cnt == 0){ puts("NONE"); } else if(cnt == 1){ POINT ans = Inter(l1, l2); printf("POINT %.2f %.2f\n", ans.x, ans.y); } else { puts("LINE"); } } puts("END OF OUTPUT"); } return 0;}
0 0
- poj 1269 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(直线位置关系,水)
- POJ 1269 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(简单计算几何,判断直线的关系)
- POJ 1269 Intersecting Lines(判断两直线的位置关系)
- POJ 1269 Intersecting Lines(判断两条直线的位置关系)
- POJ 1269 Intersecting Lines(直线相交的判断)
- poj 1269 Intersecting Lines--直线相交
- adb shell dumpsys的使用
- 再测JAVA性能比C/C++好问题
- 紫外线杀菌器:uv紫外线杀菌技术原理
- R语言程序设计week4
- Android震动vibrator系统开发全过程
- poj 1269 Intersecting Lines[直线的关系]
- ubuntu恢复unity桌面
- 二叉树的非递归遍历
- 第十八周周报
- 骨灰级泡妞高手白皮书之升级版
- 学习Swift笔记 (十一)Swift的属性
- NSOperation 的使用(下载相关) 图片和文件都是可以的 断点续传 图片逐渐显示
- socket通信
- MyEclipse优化全攻略