Smallest Regular Polygon - UVa 12300 几何

来源:互联网 发布:绘图设计软件 编辑:程序博客网 时间:2024/05/18 02:59

Smallest Regular Polygon

Given two different points A and B, your task is to find 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 five 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 41 2 3 4 52 3 4 5 60 0 0 0 0

Output for the Sample Input

1.0000005.2573115.196152

题意:给定两个点,求经过这两个点的正n边形的最小面积。

思路:n为偶数,那么就是对角线最长,n为奇数时,相对最远点的斜对角线最长。

AC代码如下:

#include<cstdio>#include<cstring>#include<cmath>using namespace std;double eps=1e-8;int main(){    int n;    double x1,x2,y1,y2,d,r,p,S;    while(~scanf("%lf%lf%lf%lf%d",&x1,&y1,&x2,&y2,&n) && n>0)    {        d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))/2;        if(n&1)        {            p=M_PI*2*(n-1)/(2*n)/2;            r=d/sin(p);        }        else          r=d;        p=2*M_PI/n;        S=r*r*sin(p)/2*n;        printf("%.6f\n",S);    }}



0 0
原创粉丝点击