UVa 10005 - Packing polygons

题目:给你一个多边形,问是否能够用一个半径是r的圆包含。

分析:计算几何,最小圆覆盖。裸的最小圆包含,利用随机增量算法。

说明:终于谢了第一道最小元覆盖╮(╯▽╰)╭。

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

using namespace std;

typedef struct pnode
{
	double x,y,r;
	pnode(){}
	pnode(double X, double Y, double R){x = X;y = Y;r = R;}
	pnode(pnode a, pnode b) {
		x = (a.x+b.x)/2;y = (a.y+b.y)/2;
		r = sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))/2;
	}
}point;
point P[101];

double dist_p2p(point a, point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

//三角形外心 
point circle(point a, point b, point p)
{
	double A1 = a.x-b.x, B1 = a.y-b.y, C1 = (a.x*a.x-b.x*b.x+a.y*a.y-b.y*b.y)/2;
	double A2 = p.x-b.x, B2 = p.y-b.y, C2 = (p.x*p.x-b.x*b.x+p.y*p.y-b.y*b.y)/2;
	point c;
	c.x = (C1*B2-C2*B1)/(A1*B2-A2*B1);
	c.y = (A1*C2-A2*C1)/(A1*B2-A2*B1);
	c.r = dist_p2p(c, a);
	return c;
}

point smallest_enclosing_circle(point P[], int n)
{
	random_shuffle(P, P+n);
	point c = point(P[0], P[1]);
	for (int i = 2; i < n; ++ i)
		if (dist_p2p(P[i], c) > c.r+1e-6) {
			//smallest_enclosing_circle_2point 
			c = point(P[0], P[i]);
			for (int j = 1; j < i; ++ j)
				if (dist_p2p(P[j], c) > c.r+1e-6) {
					c = point(P[j], P[i]);
					for (int k = 0; k < j; ++ k)
						if (dist_p2p(P[k], c) > c.r+1e-6)
							c = circle(P[i], P[j], P[k]);
				}
		}
	return c;
}

int main()
{
	int 	n;
	double 	r;
	while (~scanf("%d",&n) && n) {
		for (int i = 0; i < n; ++ i)
			scanf("%lf%lf",&P[i].x,&P[i].y);
		scanf("%lf",&r);
		point c = smallest_enclosing_circle(P, n);
		if (c.r < r+1e-6)
			printf("The polygon can be packed in the circle.\n");  
		else
			printf("There is no way of packing that polygon.\n");
	}
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值