hdu 1086 You can Solve a Geometry Problem too 向量积
2013-09-10 21:21
519 查看
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const double eps=1e-14; const int maxn=111; struct node{ double x1,y1,x2,y2; }e[maxn]; double cross(double x1,double y1,double x2,double y2) { return x1*y2-x2*y1; } int find(node a,node b) { double c[4]; c[0]=cross(a.x2-a.x1,a.y2-a.y1,b.x1-a.x1,b.y1-a.y1); c[1]=cross(a.x2-a.x1,a.y2-a.y1,b.x2-a.x1,b.y2-a.y1); c[2]=cross(b.x2-b.x1,b.y2-b.y1,a.x1-b.x1,a.y1-b.y1); c[3]=cross(b.x2-b.x1,b.y2-b.y1,a.x2-b.x1,a.y2-b.y1); if(c[0]*c[1]<=0&&c[2]*c[3]<=0)return 1; return 0; } int main() { int n; while(cin>>n) { if(n==0)break; int i,j,k,ans=0; for(i=0;i<n;i++) cin>>e[i].x1>>e[i].y1>>e[i].x2>>e[i].y2; for(i=0;i<n;i++) for(j=i+1;j<n;j++) ans+=find(e[i],e[j]); cout<<ans<<endl; } return 0; } /* 向量积: a×b>0时,b在a的逆时针方向;a×b=0时,共线;a×b<0时,b在a的顺时针方向。 对于两条线段AB,CD; 只有C,D在AB所在直线的两端且A,B在CD所在直线的两端就能保证有交点。 可以得到(AB×AC)*(AB×AD)<=0&&(CA×CD)*(CB×CD)<=0时符合条件。 当然可以用直线方程的方法,将C,D点的x带入直线AB中,然后比较y,一正一负就符合。当然也要AB,CD交换下再来次才可以确定。 */
相关文章推荐
- 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-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 A - 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 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