点在哪里

来源:互联网 发布:java开发中间件 编辑:程序博客网 时间:2024/06/10 02:55

题目描述:

输出一组图形(矩形或圆)和一组点的数据,判断点的位置。

 

输入格式:

输入一组图形的数据,其中每行以“c”开头表示圆,以“r”开头表示矩形,其中矩形是依次给出左下脚和右上角坐标,圆是给出圆心坐标及半径,图形数据以输入另起一行的“*”结束,接下来是给出点的坐标(xy),整个输入文件以9999.9  9999.9结束,每行中的各个数据以空格分隔。

 

输出格式:

输出点的位置如下信息:直接输出到屏幕上,如某点在图形上,则输出所有图形上的信息:Point i is conatined in figure j;如果点不在任一图形内,则输出:Point i is not contained in any figure

 

输入输出样例:

Sample input

Output for the input

r 0.0 0.0 5.5 10.3

c -5.0 -5.0 3.7

r 2.5 2.5 12.5 12.5

*

2.0 2.0

4.7 5.3

9999.9 9999.9

Point 1 is contained in figure 1

Point 2 is contained in figure 1

Point 2 is contained in figure 3

 

Sample input

Output for the input

r 8.5 -8.5 25.5 17.0

c 20.2 7.3 5.8

r 0.0 0.0 5.5 10.3

c -5.0 -5.0 3.7

r 2.5 2.5 12.5 12.5

c 5.0 15.0 7.2

*

6.9 11.2

20.0 20.0

17.6 3.2

-5.2 -7.8

4.7 5.3

2.0 2.0

9999.9 9999.9

Point 1 is contained in figure 5

Point 1 is contained in figure 6

Point 2 is not contained in any figure

Point 3 is contained in figure 1

Point 3 is contained in figure 2

Point 4 is contained in figure 4

Point 5 is contained in figure 3

Point 5 is contained in figure 5

Point 6 is contained in figure 3

 

解:

如果点(x,y)在矩形(x1,y1),(x2,y2)内,一定满足条件:x1<=x<=x2 && y1<=y<=y2

如果点(x,y)在圆(x1,y1,r)内,一定满足条件:sqrt((x-x1)2+(y-y1)2)<=r

                                                                                                                  .

 

代码:

 

 
#include<stdio.h>
#include
<math.h>
#include
<string.h>
#include
<stdlib.h>


#define MAXLEN 1000
#define RGET(TYPE,VAR,LOC) ((TRectangle *)TYPE[LOC].data)->VAR
#define CGET(TYPE,VAR,LOC) ((TCircle *)TYPE[LOC].data)->VAR

struct TRectangle{    //矩形
    float x1,x2,y1,y2;
}
;

struct TCircle{    //
    float x,y,r;
}
;

class Figure{
public:
    
void *data;
    
char type;    //type='r' 表示矩形;type='c' 表示圆
}
;

int figlen;
Figure figure[MAXLEN];


inline 
void
swap(
float &a,float &b){    //交换a和b的值
    float c;
    c
=a;
    a
=b;
    b
=c;
}


inline 
bool
equal(
const float a,const float b)//判断俩浮点数是否相等
    return fabs(a-b)<1e-6;
}


inline 
void 
readRecord()
{    //读取图形数据
    char type;
    
while(1){
        type
=getchar();
        
if(type=='*'return;
        figure[figlen].type
=type;
        
if(type=='r'){
            figure[figlen].data
=new TRectangle;
            scanf(
"%f%f%f%f",&RGET(figure,x1,figlen),&RGET(figure,y1,figlen),&RGET(figure,x2,figlen),&RGET(figure,y2,figlen));
            figlen
++;
        }

        
else if(type=='c'){
            figure[figlen].data
=new TCircle;
            scanf(
"%f%f%f",&CGET(figure,x,figlen),&CGET(figure,y,figlen),&CGET(figure,r,figlen));
            figlen
++;
        }

    }

}


inline 
void 
caculate()
{    //输入点并输出所在图形
    float a,b,c,d;
    
int i,no=0;
    
bool ok;
    
while(1){
        scanf(
"%f%f",&a,&b);
        
if(equal(a,9999.9f&& equal(b,9999.9f)) return;
        
++no;
        ok
=false;
        
for(i=0;i<figlen;i++)
            
if(figure[i].type=='r'){
                
if(RGET(figure,x1,i)<&& RGET(figure,x2,i)>&& RGET(figure,y1,i)<&& RGET(figure,y2,i)>b){
                    printf(
"Point %d is contained in figure %d ",no,i+1);
                    ok
=true;
                }

            }

            
else{
                c
=CGET(figure,x,i)-a;
                c
*=c;
                d
=CGET(figure,y,i)-b;
                d
*=d;
                
if(sqrt(c+d)<CGET(figure,r,i) || equal((float)sqrt(c+d),CGET(figure,r,i))){
                    printf(
"Point %d is contained in figure %d ",no,i+1);
                    ok
=true;
                }

            }

        
if(ok==false) printf("Point %d is not contained in any figure ",no);
    }

}


void
main()
{
    
int fd;
    fd
=open("test.txt",O_RDONLY);
    dup2(fd,
0);
    fd
=open("result.txt",O_TRUNC|O_WRONLY);
    dup2(fd,
1);
    figlen
=0;
    readRecord();
    caculate();
}

原创粉丝点击