向量判断HDU 1086 线段交点 You can Solve a Geometry Problem too
最近一直在学习向量判断之类的问题,现在正好有机会和大家讨论一下.
标题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086
分析:判断AB和CD两线段是否有交点:
同时满意两个条件,1.C点D点分别在AB的两侧.(向量(ABxAC)*(ABxAD)<=0)
2.A点和B点分别在CD两侧.(向量(CDxCA)*(CDxCB)<=0)
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iomanip>
using namespace std;
const int maxn=100000;
struct point{
double x,y;
void read(){
cin>>x>>y;
}
};
struct line{
point st, en;
void read(){
st.read();
en.read();
}
}f[maxn];
double cross(point p0,point p1,point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int work(line h,line g){
double x = cross(h.st,h.en,g.st);
double y = cross(h.st,h.en,g.en);
if(x*y<=0) return 1;
return 0;
}
int main(){
int n;
while(cin>>n,n){
int sum=0;
for(int i=0;i<n;++i){
f[i].read();
for(int j=0;j<i;++j)
sum += ( work(f[i],f[j])&& work(f[j],f[i]) );
}
cout<<sum<<endl;
}
return 0;
}
文章结束给大家分享下程序员的一些笑话语录: 女人篇
有的女人就是Windows虽然很优秀,但是安全隐患太大。
有的女人就是MFC她条件很好,然而不是谁都能玩的起。
有的女人就是C#长的很漂亮,但是家务活不行。
有的女人就是C++,她会默默的为你做很多的事情。
有的女人就是汇编虽然很麻烦,但是有的时候还得求它。
有的女人就是SQL,她会为你的发展带来莫大的帮助。
--------------------------------- 原创文章 By 向量和判断 ---------------------------------

浙公网安备 33010602011771号