uva5984(简单计算几何)

题意:

给出n个图形,三角形、正方形和圆形,问这些图形覆盖多少个整数点。


思路:

每读入一个图形,就去计算这个图形覆盖了多少个点,用vis[][]来标记哪些点被覆盖过。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<cmath>

using namespace std;

#define maxn 500
#define eps 1e-8

struct point
{
    float x,y;
};

bool vis[maxn][maxn];
int ans;

int dis(int x1,int y1,int x2,int y2)
{
    return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}

void checkC(int x,int y,int r)
{
	for(int i=x-r;i<=x+r;i++)
		for(int j=y-r;j<=y+r;j++)
			if(!vis[i][j]&&dis(i,j,x,y)<=r*r)
			{
				vis[i][j]=1;
				ans++;
			}
	return ;
}

void checkS(int x,int y,int l)
{
	for(int i=x;i<=x+l;i++)
		for(int j=y;j<=y+l;j++)
			if(!vis[i][j])
			{
				vis[i][j]=true;
				ans++;
			}
	return ;
}

double area(point pt0,point pt1,point pt2)
{
//	double a,b,c,p,s;
//	a=sqrt((pt0.x-pt1.x)*(pt0.x-pt1.x)+(pt0.y-pt1.y)*(pt0.y-pt1.y));
//	b=sqrt((pt0.x-pt2.x)*(pt0.x-pt2.x)+(pt0.y-pt2.y)*(pt0.y-pt2.y));
//	a=sqrt((pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
//	p=(a+b+c)/2;
//	s=sqrt(p*(p-a)*(p-b)*(p-c));
//	return s;
	point t1,t2;
	t1.x=pt1.x-pt0.x;
	t1.y=pt1.y-pt0.y;
	t2.x=pt2.x-pt1.x;
	t2.y=pt2.y-pt1.y;
	return fabs((t1.x*t2.y-t1.y*t2.x))/2.0;
}

void checkT(const point A,const point B,const point C)
{
    int minx=min(min((int)A.x,(int)B.x),(int)C.x);
    int miny=min(min((int)A.y,(int)B.y),(int)C.y);
    int maxx=max(max((int)A.x,(int)B.x),(int)C.x);
    int maxy=max(max((int)A.y,(int)B.y),(int)C.y);
    for(int i=minx; i<=maxx; i++)
        for(int j=miny; j<=maxy; j++)
            if(!vis[i][j])
            {
				point D;
                D.x=i*1.0;
				D.y=j*1.0;
				double SABC=area(A,B,C);
                double SumSquar=area(A,D,B)+area(B,D,C)+area(A,D,C);
                if(fabs(SABC-SumSquar)<eps)
                {
                    vis[i][j]=true;
                    ans++;
                }
            }
	return ;
}

int main()
{
    int t;
    char s[2];
    scanf("%d",&t);
    while(t--)
    {
		int n;
        scanf("%d",&n);
        ans=0;
        memset(vis,false,sizeof(vis));
        while(n--)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
				int x,y,r;
                scanf("%d%d%d",&x,&y,&r);
                x+=100;
				y+=100;
                checkC(x,y,r);
            }
            else if(s[0]=='S')
            {
				int x,y,l;
                scanf("%d%d%d",&x,&y,&l);
                x+=100;
				y+=100;
                checkS(x,y,l);
            }
            else if(s[0]=='T')
            {
                point A,B,C;
                scanf("%f%f%f%f%f%f",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);
                A.x+=100.0;
				A.y+=100.0;
                B.x+=100.0;
				B.y+=100.0;
                C.x+=100.0;
				C.y+=100.0;
                checkT(A,B,C);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值