UVa 478 - Points in Figures: Rectangles, Circles, Triangles

题目:点与平面矩形、圆形和三角形的关系判断。

分析:计算几何。

            点与矩形关系:    判断在两个顶点之间即可。

            点与圆形关系:    点到圆心距离小于半径。

            点与三角形关系:点在矢量AB,BC,CA同侧。

注意:在边上不算在里面。

#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;
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值