题目:判断点与矩形和圆的关系。
分析:简单题。478类似物,贴代码直接过。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
typedef struct pnode{
double x,y;
}point;
typedef struct lnode{
double x,y,dx,dy;
lnode( point a, point b ){x = a.x;y = a.y;dx = b.x-a.x;dy = b.y-a.y;}
}line;
typedef struct rnode{
point p1,p2;
}rect;
typedef struct cnode{
point p;
double r;
}circ;
typedef struct tnode{
point p1,p2,p3;
}tria;
typedef struct dnode{
char type;
rect r;
circ c;
tria t;
}data;
data D[15];
//点到点距离
double dist( point a, point b )
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//点与矢量关系
double ptol( point p, line l )
{
return l.dy*(p.x-l.x)-l.dx*(p.y-l.y);
}
int main()
{
char c;
int count = 0,t = 1;
while ( cin >> c && c != '*' ) {
D[count].type = c;
switch( c ) {
case 'r': cin >> D[count].r.p1.x >> D[count].r.p1.y
>> D[count].r.p2.x >> D[count].r.p2.y;
break;
case 'c': cin >> D[count].c.p.x >> D[count].c.p.y
>> D[count].c.r;
break;
case 't': cin >> D[count].t.p1.x >> D[count].t.p1.y
>> D[count].t.p2.x >> D[count].t.p2.y
>> D[count].t.p3.x >> D[count].t.p3.y;
break;
default : break;
}
count ++;
}
point p;
while ( cin >> p.x >> p.y ) {
if ( p.x == 9999.9 && p.y == 9999.9 ) break;
int flag = 0;
double t1,t2,t3;
for ( int i = 0 ;i < count ; ++ i )
switch( D[i].type ) {
case 'r': if ( D[i].r.p1.x < p.x && D[i].r.p2.x > p.x &&
D[i].r.p2.y < p.y && D[i].r.p1.y > p.y ) {
flag = 1;
printf("Point %d is contained in figure %d\n",t,i+1);
}break;
case 'c': if ( dist( p, D[i].c.p ) < D[i].c.r ) {
flag = 1;
printf("Point %d is contained in figure %d\n",t,i+1);
}break;
case 't': t1 = ptol( p, line( D[i].t.p1, D[i].t.p2 ) );
t2 = ptol( p, line( D[i].t.p2, D[i].t.p3 ) );
t3 = ptol( p, line( D[i].t.p3, D[i].t.p1 ) );
if ( t1*t2 > 0 && t2*t3 > 0 && t1*t3 > 0 ) {
flag = 1;
printf("Point %d is contained in figure %d\n",t,i+1);
}break;
default : break;
}
if ( !flag )
printf("Point %d is not contained in any figure\n",t);
t ++;
}
return 0;
}