CodeForces-1C-Ancient Berland Circus

Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things were different.

In Ancient Berland arenas in circuses were shaped as a regular (equiangular) polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges.

Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time.

You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have.

Input
The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point.

Output
Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100.

Sample test(s)
input
0.000000 0.000000
1.000000 1.000000
0.000000 1.000000
output
1.00000000

题目大意

对于一个正多边形,只给出了其中三点的坐标,求这个多边形可能的最小面积,给出的三个点一定能够组成三角形。

思路

对于给定的三角形,其外接圆半径大小是一定的;在外接圆半径大小一定的情况下,要使正多边形面积最小,就是使正多边形的边数最少,也就是使得每条边所对应的圆心角最大。

最大圆心角 = 三角形三条边所对应的三个圆心角的公约数,在下图中,也就是∠1、∠2和∠3的最大公约数。

步骤

  1. 根据三角形三个顶点的坐标求得三角形的三边长a、b、c
  2. 求外接圆半径R
    1. 海伦公式求三角形面积  
    2. 三角形面积还等于 S = a×b×sin C / 2
    3. 正弦定理 ,sin C = c / (2R)
    4. 联立得:R = abc / (4S)
  3. 求外接圆圆心到三角形三个顶点组成的三个圆心角∠1、∠2、∠3的值
    1. 余弦定理求得三个角的余弦值
    2. 通过反余弦arccos()求得三个角的值
  4. 求得三个圆心角∠1、∠2和∠3的最大公约数angle
  5. 求正多边形的面积
    1. 正多边形一共有2*PI / angle个小三角形
    2. 每个小三角形的面积为R2*sin(angle) / 2

代码

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define feq(a, b) (fabs(a - b)< 1E-4)
struct Point {
    double x;
    double y;
};
double getDistance(Point* p1, Point* p2) {
    return sqrt((p1->x - p2->x) * (p1->x - p2->x) + (p1->y - p2->y) * (p1->y - p2->y));
}
//反余弦
double ArcCos(double a, double b, double c) {
    //先用余弦定理求得∠A的余弦, 再用acos函数求得反余弦
    return acos((b*b + c*c - a*a) / (2.0*b*c));
}
//海伦公式
double Heron(double a, double b, double c) {
    double p = (a + b + c) / 2.0;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}
double fgcd(double a, double b){
     if (feq(a, 0))
         return b;
     if (feq(b, 0))
         return a;
     return fgcd(b, fmod(a, b));
}
int main() {
//    freopen("in.txt", "r", stdin);
    Point p1, p2, p3;
    double a, b, c;//三角形三条边
    cin >> p1.x >> p1.y;
    cin >> p2.x >> p2.y;
    cin >> p3.x >> p3.y;
//    1、求三角形三条边a, b, c
    a = getDistance(&p1, &p2);
    b = getDistance(&p1, &p3);
    c = getDistance(&p2, &p3);
//    2、求外接圆半径:海伦公式 + 正弦定理
    double S = Heron(a, b, c);
    /* 三角形面积S = a * b * sin C / 2
     * 正弦定理 a / sin A = b / sin B = c / sin C = 2R ==> sin C = c / (2R)
     * 联立得:S = abc/(4R) ==> R = abc/(4S)
     * */
    double R = a * b * c / (4.0 * S);
//    3、利用余弦定理求得外接圆圆心到三角形三个顶点组成的三个角的值
    double angle1 = ArcCos(a, R, R);
    double angle2 = ArcCos(b, R, R);
    double angle3 = 2 * PI -  angle1 - angle2;
//    4、求得三个角的最大公约数
    double angle = fgcd(angle1, fgcd(angle2, angle3));
//    5、正弦求面积
    double area = 0.5 * R * R*sin(angle) * (2*PI/angle);
    printf("%0.6lf", area);
    return 0;
}

 

posted @ 2014-04-12 11:55  Shanks-香克斯  阅读(407)  评论(0编辑  收藏  举报