判断线段相交
来源:互联网 发布:demo设计软件 编辑:程序博客网 时间:2024/05/16 04:02
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<cstdlib>
- #include<iostream>
- #include<algorithm>
- #include<sstream>
- #include<fstream>
- #include<vector>
- #include<map>
- #include<stack>
- #include<list>
- #include<set>
- #include<queue>
- #define LL long long
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1 | 1
- using namespace std;
- const int maxn=100005,inf=1<<29;
- int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};
- int n,m,t;
- double EPS=1e-10;
- double add(double a,double b)
- {
- if(abs(a+b)<EPS*(abs(a)+abs(b))) return 0;
- return a+b;
- }
- struct P
- {
- double x,y;
- P(){}
- P(double x,double y):x(x),y(y){}
- P operator +(P p)
- {
- return P(add(x,p.x),add(y,p.y));
- }
- P operator -(P p)
- {
- return P(add(x,-p.x),add(y,-p.y));
- }
- P operator *(double d)
- {
- return P(x*d,y*d);
- }
- double dot(P p)//内积
- {
- return add(x*p.x,y*p.y);
- }
- double det(P p)//外积
- {
- return add(x*p.y,-p.x*y);
- }
- };
- struct v
- {
- P p,q;
- }a[maxn];
- bool on_seg(P p1,P p2,P q)//判断点q是否在线段p1p2上
- {
- return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
- }
- P intersection(P p1,P p2,P q1,P q2)//计算两线段的交点
- {
- return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
- }
- bool judge(v s1,v s2)
- {
- if((s1.p-s1.q).det(s2.p-s2.q)==0)
- {
- return on_seg(s1.p,s1.q,s2.p)||
- on_seg(s1.p,s1.q,s2.q)||
- on_seg(s2.p,s2.q,s1.p)||
- on_seg(s2.p,s2.q,s1.q);
- }
- else
- {
- P r=intersection(s1.p,s1.q,s2.p,s2.q);
- //cout<<"x = "<<r.x<<" y= "<<r.y<<endl;
- return on_seg(s1.p,s1.q,r)&&on_seg(s2.p,s2.q,r);
- }
- }
- int main()
- {
- while(cin>>n,n)
- {
- for(int i=0;i<n;i++) cin>>a[i].p.x>>a[i].p.y>>a[i].q.x>>a[i].q.y;
- //cin>>s2.p.x>>s2.p.y>>s2.q.x>>s2.q.y;
- int cnt=0;
- for(int i=0;i<n;i++)
- for(int j=i+1;j<n;j++)
- if(judge(a[i],a[j])) cnt++;
- cout<<cnt<<endl;
- }
- return 0;
- }
- 转载自:http://blog.csdn.net/u013615904/article/details/46917431
阅读全文
0 0
- 判断线段相交
- 判断线段相交程序
- 判断线段相交
- 怎样判断线段相交
- POJ1269(判断线段相交)
- 判断线段相交
- 判断线段相交
- 判断线段相交
- 判断线段相交
- 判断两个线段相交
- 判断线段相交思想
- 判断线段相交模板
- 线段相交的判断
- 判断线段是否相交
- poj3565Ants【线段相交判断】
- 判断线段相交
- 判断线段相交
- POJ2653->判断线段相交
- Dubbo_服务化改造注意事项
- Android O限制系统全屏进一步遏制手机勒索
- Dubbo源代码分析八:再说Provider线程池被EXHAUSTED
- 基于React-Native及Redux的Immutable.js引入
- children和childNodes 的区别
- 判断线段相交
- 机试各种细节
- 【STL】容器 > 顺序容器 > list(双向链表)
- Kafka学习笔记:初识Kafka
- 基于React-Native及Redux的Immutable.js引入
- 【前端】向blog或网站中添加语法高亮显示代码方法总结
- ios 消除 字符串 首尾空格
- 多因子模型之因子(信号)测试平台----因子值的处理(二)
- MySQL-5.6.13免安装版配置方法