HDU 1086 You can Solve a Geometry Problem too 判断任意两线段是否相交
来源:互联网 发布:淘宝网卖家信用高流量 编辑:程序博客网 时间:2024/05/17 11:05
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
题目大意:给出n条线段,求一共有多少个交点,忽略相交于同一点。
解题思路:设有线段AB,CD
判断AB和CD知否有交点:(同时满足,*表示叉乘)
1.C点和D点分别在AB的两侧 ---> 向量(AB*AC)*(AB*AD)<=0
2.A点和B点分别在CD的两侧 ---> 向量(CD*CA)*(CD*CB)<=0
2.A点和B点分别在CD的两侧 ---> 向量(CD*CA)*(CD*CB)<=0
代码:
#include<iostream>#include<cstdio>using namespace std;#define N 210typedef struct{ double x,y;}Point;Point p[N];///叉乘double Judge(double x1,double y1,double x2,double y2){ return (x1*y2-x2*y1);}int main(){ int n; while(scanf("%d",&n)&&n) { int ans=0; if(n==1) ; else{ for(int i=0;i<2*n;i+=2){ scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+1].x,&p[i+1].y); } for(int i=0;i<2*n;i+=2){ for(int j=i+2;j<2*n;j+=2){ if( ( Judge(p[i+1].x-p[i].x, p[i+1].y-p[i].y, p[j].x-p[i].x, p[j].y-p[i].y) * Judge(p[i+1].x-p[i].x, p[i+1].y-p[i].y, p[j+1].x-p[i].x, p[j+1].y-p[i].y) )<=0.0 ){ if( ( Judge(p[j+1].x-p[j].x, p[j+1].y-p[j].y, p[i].x-p[j].x, p[i].y-p[j].y) * Judge(p[j+1].x-p[j].x, p[j+1].y-p[j].y, p[i+1].x-p[j].x, p[i+1].y-p[j].y) ) <=0.0 ) ans++; } } } } printf("%d\n",ans); } return 0;}
0 0
- 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(判断线段是否相交,非规范相交)
- HDU 1086 You can Solve a Geometry Problem too(判断两线段是否相交)跨立实验
- HDU 1086 You can Solve a Geometry Problem too(判断两条直线是否相交)
- hdu1086 You can Solve a Geometry Problem too (判断两线段是否相交)
- You can Solve a Geometry Problem too----判断两线段是否相交
- hdu You can Solve a Geometry Problem too(判断两条线段相交)
- hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交
- 杭电 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 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 (计算几何 判断线段相交)
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
- protel 99se 中间区域不敷铜
- 关于ArrayList与数组对象的直接输出
- 2034 人见人爱A-B
- android应用开发详解(十四)--------------Intent的Action属性(续)
- 9.19 工程日志
- HDU 1086 You can Solve a Geometry Problem too 判断任意两线段是否相交
- uva10245The Closest Pair Problem
- 局部变量地址的返回
- @RequestBody应用
- [Amazon笔试]求比数n大的最小回文数的字符串形式
- 怎么使float保留两位小数或多位小数
- sublime使用笔记
- Codeforces 474 E. Pillars
- 心情日记 2014.10.09