//还不错的计算几何模板,mark一下~ http://www.cnblogs.com/luyi0619/archive/2011/02/21/1959775.html
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct POINT
{
double x,y;
POINT() {}
POINT(double d1,double d2)
{
x=d1;
y=d2;
}
POINT operator - (const POINT p) const
{
POINT ans;
ans.x= x-p.x;
ans.y =y-p.y;
return ans;
}
double operator * (const POINT p) const
{
return x*p.y-y*p.x;
}
};
struct Line
{
POINT px,py;
Line() {}
Line (POINT p1,POINT p2)
{
px=p1;
py=p2;
}
} line[1010];
double direction(POINT pi,POINT pj,POINT pk)
{
return (pk-pi)*(pj-pi);
}
bool on_segments(POINT pi,POINT pj,POINT pk)
{
if(min(pi.x,pj.x)<=pk.x && pk.x<=max(pi.x,pj.x) && min(pi.y,pj.y)<=pk.y && pk.y<=max(pi.y,pj.y))
return true;
else
return false;
}
bool segments_intersect(POINT p1,POINT p2,POINT p3,POINT p4)
{
double d1=direction(p3,p4,p1);
double d2=direction(p3,p4,p2);
double d3=direction(p1,p2,p3);
double d4=direction(p1,p2,p4);
if(((d1>0 && d2<0)||(d1<0 && d2>0))&&((d3>0 && d4<0)||(d3<0 && d4>0)))
return true;
else if(d1==0 && on_segments(p3,p4,p1))
return true;
else if(d2==0 && on_segments(p3,p4,p2))
return true;
else if(d3==0 && on_segments(p1,p2,p3))
return true;
else if(d4==0 && on_segments(p1,p2,p4))
return true;
else
return false;
}
int ans[1010],to[1010];
bool cmp(Line l1,Line l2)
{
return l1.px.x < l2.px.x;
}
int main()
{
int n,m;
double x1,y1,x2,y2;
while(scanf("%d %d %lf %lf %lf %lf",&n,&m,&x1,&y1,&x2,&y2)==6)
{
memset(ans,0,sizeof(ans));
memset(to,0,sizeof(to));
line[0] = Line(POINT(x1,y2),POINT(x1,y1));
for(int i=1; i<=n; i++)
{
double ui,li;
scanf("%lf %lf",&ui,&li);
line[i] = Line(POINT(li,y2),POINT(ui,y1));
}
line[n+1] = Line(POINT(x2,y2),POINT(x2,y1));
sort(line,line+n+2,cmp);
for(int i=1; i<=m; i++)
{
double x,y;
scanf("%lf %lf",&x,&y);
Line now(POINT(x1,y2),POINT(x,y));
int low = 0,high = n,mid,res;
while(low<=high)
{
mid = (low + high)/2;
if(segments_intersect(now.px,now.py,line[mid].px,line[mid].py))
{
low = mid + 1;
res = mid;
}
else
high = mid-1;
}
ans[res]++;
}
for(int i=0; i<=n; i++)
if(ans[i]>0)
to[ans[i]]++;
printf("Box\n");
for(int i=0; i<=n; i++)
{
if(to[i]>0)
printf("%d: %d\n",i,to[i]);
}
}
return 0;
}计算几何样例
最新推荐文章于 2026-06-20 19:48:58 发布
1383

被折叠的 条评论
为什么被折叠?



