UVa 477 Points in Figures: Rectangles and Circles

Code:
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct triangle
{
    double a,b,c,d;
};
struct circle
{
    double r,px,py;
};
struct graph
{
    int ind;
    bool tri=false,cir=false;
};
bool cmp(triangle t,double x,double y)
{
    if(x>t.a && y<t.b && x<t.c &&y>t.d) return true;
    return false;
}
bool cmpc(circle c,double x,double y)
{
    if(sqrt((x-c.px)*(x-c.px)+(y-c.py)*(y-c.py))<c.r) return true;
    return false;
}
int main()
{
    char ch=' ';
    int indr=0,indc=0,indg=0;
    triangle tri[10000];
    circle cir[10000];
    graph G[10000];
    while(cin>>ch&&ch!='*')
    {
        if(ch=='r')
        {
            scanf("%lf %lf %lf %lf",&tri[indr].a,&tri[indr].b,&tri[indr].c,&tri[indr].d);
            G[indg].tri=true,G[indg].ind=indr;
            indg++;
            indr++;
        }
        else if(ch=='c')
        {
            scanf("%lf %lf %lf",&cir[indc].px,&cir[indc].py,&cir[indc].r);
            G[indg].cir=true,G[indg].ind=indc;
            indg++;
            indc++;
        }
    }
    int pind=1;
    bool flag=false;
    double x,y;
    while(scanf("%lf%lf",&x,&y))
    {
        if(abs(x-9999.9) < 1e-9 &&abs(y-9999.-9) < 1e9) break;
        flag=false;
        for(int i=0; i<indg; i++)
        {
            if(G[i].tri==true)
            {
                if(cmp(tri[G[i].ind],x,y))
                {
                    printf("Point %d is contained in figure %d\n",pind,i+1);
                    flag=true;
                }
            }
            else if(G[i].cir==true)
            {
                 if(cmpc(cir[G[i].ind],x,y))
                {
                    printf("Point %d is contained in figure %d\n",pind,i+1);
                    flag=true;
                }
            }

        }
        if(!flag) printf("Point %d is not contained in any figure\n",pind);
        pind++;
    }
    return 0;

}

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble