12300 - Smallest Regular Polygon
Time limit: 1.000 seconds
Given two different points A and B, your task is to nd a regular polygon of n sides, passing through
these two points, so that the polygon area is minimized.
Input
There will be at most 100 test cases. Each case contains 5 integers xA, yA, xB, yB, n (0 xA; yA; xB; yB
100, 3 n 10000), the coordinates of A and B, and the number of sides of the regular polygon. The
two points A and B are always different. The last test case is followed by a line with ve zeros, which
should not be processed.
Output
For each test case, print the smallest area of the regular polygon to six decimal places.
Sample Input
0 0 1 1 4
1 2 3 4 5
2 3 4 5 6
0 0 0 0 0
Sample Output
1.000000
5.257311
5.196152
题目的意思就是说给你两个点
然后让你求一个最小的正n边形
能包住这两个点
问题问的是这个正多边形的面积
这里有两种情况 n为奇数
那么显然一个多边形最远的两个点为一个顶点到对着的边的距离
n为偶数
最远两点就是两个对着的顶点
然后由多边形中心可以把多边形划分成三角形
进一步通过几何关系和三角函数求出多边形的边长
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double PI=acos(-1.0);
int main()
{
int xa,ya,xb,yb,n;
while(scanf("%d%d%d%d%d",&xa,&ya,&xb,&yb,&n)!=EOF)
{
if(!xa&&!xb&&!ya&&!yb&&!n)
break;
double d=sqrt((double)(xa-xb)*(xa-xb)+(ya-yb)*(ya-yb));
double sita=PI/n;
if(n%2)
{
double r=d/2/cos(sita/2);
double l=2*r*sin(sita);//多边形的边长
double dl=r*cos(sita); //边心距
printf("%.6lf\n",l*dl/2*n);
}
else
{
double l=2*d/2*sin(sita);//多边形的边长
double dl=d/2*cos(sita); //边心距
printf("%.6lf\n",l*dl/2*n);
}
}
return 0;
}