码迷,mamicode.com
首页 > 其他好文 > 详细

TOJ 2392 Bounding box(已知正n边形三点求最小矩形覆盖面积)

时间:2019-01-20 13:55:14      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:names   NPU   矩形   man   题意   ace   struct   circle   log   

描述

The Archeologists of the Current Millenium (ACM) now and then discover ancient artifacts located at vertices of regular polygons. The moving sand dunes of the desert render the excavations difficult and thus once three vertices of a polygon are discovered there is a need to cover the entire polygon with protective fabric.

输入

Input contains multiple cases. Each case describes one polygon. It starts with an integer n <= 50, the number of vertices in the polygon, followed by three pairs of real numbers giving the x and y coordinates of three vertices of the polygon. The numbers are separated by whitespace. The input ends with a n equal 0, this case should not be processed.

输出

For each line of input, output one line in the format shown below, giving the smallest area of a rectangle which can cover all the vertices of the polygon and whose sides are parallel to the x and y axes.

样例输入

4
10.00000 0.00000
0.00000 -10.00000
-10.00000 0.00000
6
22.23086 0.42320
-4.87328 11.92822
1.76914 27.57680
23
156.71567 -13.63236
139.03195 -22.04236
137.96925 -11.70517
0

样例输出

Polygon 1: 400.000
Polygon 2: 1056.172
Polygon 3: 397.673

题意

已知正n边形三点求最小矩形覆盖面积。

题解

正n边形上三点,求出正n边形上唯一外接圆

顺时针绕圆心旋转圆心角得到每个顶点的坐标

然后答案就是(最大的x-最小的x)*(最大的y-最小的y)

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Point
 5 {
 6     double x,y;
 7     Point(double x=0,double y=0):x(x),y(y){}
 8 };
 9 Point tcircle(Point pt1,Point pt2,Point pt3,double &radius)
10 { 
11     double x1=pt1.x,x2=pt2.x,x3=pt3.x;
12     double y1=pt1.y,y2 =pt2.y,y3=pt3.y;
13     double a=x1-x2,b=y1-y2,c=x1-x3,d=y1-y3;
14     double e=((x1*x1-x2*x2)+(y1*y1-y2*y2))/2.0;
15     double f=((x1*x1-x3*x3)+(y1*y1-y3*y3))/2.0;
16     double det=b*c-a*d;
17     double x0=(b*f-d*e)/det;
18     double y0=(c*e-a*f)/det;
19     radius=hypot(x1-x0,y1-y0);
20     return Point(x0,y0);
21 }
22 Point rotateShun(Point p,Point dx,double selt)
23 {
24     double xx=(p.x-dx.x)*cos(-selt)-(p.y-dx.y)*sin(-selt)+dx.x;
25     double yy=(p.x-dx.x)*sin(-selt)+(p.y-dx.y)*cos(-selt)+dx.y;
26     return Point(xx,yy);
27 }
28 Point p[55];
29 int main()
30 {
31     int n,ca=1;
32     double PI=acos(-1);
33     while(scanf("%d",&n),n)
34     {
35         for(int i=1;i<=3;i++)
36             scanf("%lf%lf",&p[i].x,&p[i].y);
37         double r=0.;
38         Point cir=tcircle(p[1],p[2],p[3],r);
39         double selt=2.*PI/n;
40         double maxx=p[1].x,minx=p[1].x,maxy=p[1].y,miny=p[1].y;
41         for(int i=2;i<=n;i++)
42         {
43             p[i]=rotateShun(p[i-1],cir,selt);
44             maxx=max(maxx,p[i].x);
45             minx=min(minx,p[i].x);
46             maxy=max(maxy,p[i].y);
47             miny=min(miny,p[i].y);
48         }
49         double ans=(maxx-minx)*(maxy-miny);
50         printf("Polygon %d: %.3f\n",ca++,ans);
51     }
52     return 0;
53 }

TOJ 2392 Bounding box(已知正n边形三点求最小矩形覆盖面积)

标签:names   NPU   矩形   man   题意   ace   struct   circle   log   

原文地址:https://www.cnblogs.com/taozi1115402474/p/10294276.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!