HDU 1086.You can Solve a Geometry Problem too【判断两线段相交】【数学题】【12月30】
来源:互联网 发布:淘宝店铺外包有用吗 编辑:程序博客网 时间:2024/06/06 09:11
You can Solve a Geometry Problem too
Problem Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the number of intersections, and one line one case.
Sample Input
20.00 0.00 1.00 1.000.00 1.00 1.00 0.0030.00 0.00 1.00 1.000.00 1.00 1.00 0.0000.00 0.00 1.00 0.000
Sample Output
13
#include<iostream>#include<cstdio>#include<cmath>using namespace std;const int maxn = 110;struct ss{ double x1, x2, y1, y2; double k; bool flag;};bool check(ss a, ss b){ //无斜率平行 if(a.flag == b.flag && a.flag == false) return false; //有一直线无斜率 if(a.flag == false || b.flag == false) { //a无斜率 if(a.flag == false) { double y = b.k*a.x1 + b.y1 -b.k*b.x1; if(y < min(b.y1, b.y2) || y > max(b.y1, b.y2)) return false; else return true; } else//b无斜率 { double y = a.k*b.x1 + a.y1 -a.k*a.x1; if(y < min(a.y1, a.y2) || y > max(a.y1, a.y2)) return false; else return true; } } //有斜率平行 if(a.k == b.k) return false; else//不平行 { double x = (b.y1-b.k*b.x1-a.y1+a.k*a.x1)/(a.k-b.k); if(x < min(a.x1, a.x2) || x > max(a.x1, a.x2) || x < min(b.x1, b.x2) || x > max(b.x1, b.x2)) return false; else return true; }}int N, ans;int main(){ while(scanf("%d", &N) != EOF && N) { ss f[maxn]; ans = 0; for(int i = 0;i < N; ++i) { scanf("%lf %lf %lf %lf", &f[i].x1, &f[i].y1, &f[i].x2, &f[i].y2); if(f[i].x1 > f[i].x2) { swap(f[i].x1, f[i].x2); swap(f[i].y1, f[i].y2); } if(f[i].x1 == f[i].x2) f[i].flag = false;//无斜率 else { f[i].flag = true;//有斜率 f[i].k = (f[i].y2 - f[i].y1)/(f[i].x2 - f[i].x1); } } for(int i = 1;i < N; ++i) for(int j = 0;j < i; ++j) { if(check(f[i], f[j])) ans ++; } cout << ans << endl; } return 0;}
0 0
- HDU 1086.You can Solve a Geometry Problem too【判断两线段相交】【数学题】【12月30】
- hdu You can Solve a Geometry Problem too(判断两条线段相交)
- HDU 1086 You can Solve a Geometry Problem too 判断任意两线段是否相交
- HDU ACM 1086 You can Solve a Geometry Problem too->判断两线段是否相交
- HDU 1086 You can Solve a Geometry Problem too(判断线段是否相交,非规范相交)
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
- hdu1086 You can Solve a Geometry Problem too (判断两线段是否相交)
- You can Solve a Geometry Problem too----判断两线段是否相交
- HDU 1086 You can Solve a Geometry Problem too(判断两线段是否相交)跨立实验
- HDU 1086 You can Solve a Geometry Problem too 两线段相交模板
- Hdu 1086 You can Solve a Geometry Problem too[判断线段相交,完整版]
- HDU 1086 You can Solve a Geometry Problem too(判断线段相交)
- HDU 1086You can Solve a Geometry Problem too(判断线段相交模板题)
- HDU 1086 You can Solve a Geometry Problem too (计算几何 判断线段相交)
- HDU 1086 You can Solve a Geometry Problem too(判断两条直线是否相交)
- You can Solve a Geometry Problem too(线段相交问题)
- hdoj1086You can Solve a Geometry Problem too【线段相交判断】
- hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交
- 太上感应篇0013
- 一个40岁程序员的无奈
- 树链剖分
- **LeetCode 24. Swap Nodes in Pairs
- LeetCode Contains Duplicate II(hash)
- HDU 1086.You can Solve a Geometry Problem too【判断两线段相交】【数学题】【12月30】
- 看看下边的问题你能够回答出来多少?
- 使用automake生成静态库
- 太上感应篇0014
- 【跟我学spring 4.0 】之第八节-Spring 表达式语言(Spring EL)
- 正规军与土八路的较量(抽象类与接口比较分析)
- iOS常用的第三方框架
- 《HTTP权威指南》学习笔记-HTTP概述和URL
- 3.库(资源包)