您的位置:首页 > 其它

HDU-#1086 You can Solve a Geometry Problem too(线段相交判定)

2014-08-24 14:30 453 查看
题目大意:给出线段点的坐标,判断有多少个交点?

解题思路:开始分了三种情况写的:一是线段相交(不含端点),二是一个端点在线段上,三是端点相交的情况,结果跳了半天都不对。直接使用模板一下就A了!套用模板,就不赘述了。详见code。

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1086

code:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

const int MAXN = 100+10;
const double eps = 1e-10;
int n,ans;

struct Point { double x, y; };

double min(double a, double b) { return a < b ? a : b; }
double max(double a, double b) { return a > b ? a : b; }

bool inter(Point a, Point b, Point c, Point d){
if ( min(a.x, b.x) > max(c.x, d.x) ||
min(a.y, b.y) > max(c.y, d.y) ||
min(c.x, d.x) > max(a.x, b.x) ||
min(c.y, d.y) > max(a.y, b.y) ) return 0;
double h, i, j, k;
h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}

int main(){
Point a[MAXN],b[MAXN];
while(scanf("%d",&n)!=EOF && n){
for(int i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(inter(a[i],b[i],a[j],b[j])) ans++;
printf("%d\n",ans);
}
return 0;
}


不过还是将这个code放在这,以后修改:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

const int MAXN = 100+10;
const double eps = 1e-10;
int n,ans;

struct Point{
double x,y;
Point(double x=0,double y=0):x(x),y(y){} //构造函数
};
//定义向量与点的加减乘除
Point operator + (Point A,Point B){return Point(A.x+B.x,A.y+B.y);}
Point operator - (Point A,Point B){return Point(A.x-B.x,A.y-B.y);}
Point operator * (Point A,double p){return Point(A.x*p,A.y*p);}
Point operator / (Point A,double p){return Point(A.x/p,A.y/p);}

int dcmp(double x){
if(fabs(x)<eps) return 0;
else return x<0 ? -1:1;
}
bool operator == (const Point& a,const Point& b){
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0;
}
bool operator < (const Point& a,const Point& b){
return a.x<b.x || (a.x==b.x && a.y<b.y);
}

double Dot(Point A,Point B){return A.x*B.x+A.y*B.y;} //点积
double Cross(Point A,Point B){return A.x*B.y-A.y*B.x;} //叉积

bool OnSegment(Point p,Point a1,Point a2){ //判断端点是否在在另一个线段上
return dcmp(Cross(a1-p,a2-p))==0 && dcmp(Dot(a1-p,a2-p))<0;
}

bool Segment_Intersection(Point a1,Point a2,Point b1,Point b2){
double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
if(c1==0 || c2==0) return OnSegment();//一个端点在线段上
if(a1==b1 || a1==b2 || a2==b1 || a2==b2) return true;//端点相交
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0; //线段与线段相交(端点处不交)
}

int main(){
Point a[MAXN],b[MAXN];
while(scanf("%d",&n)!=EOF && n){
for(int i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(Segment_Intersection(a[i],b[i],a[j],b[j])) ans++;
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐