POJ1927 Area in Triangle (计算几何)

版权声明:转载请说明出处:https://blog.csdn.net/hanyanwei123 https://blog.csdn.net/hanyanwei123/article/details/81782952

Language:Default
Area in Triangle

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1694 Accepted: 832

Description

Given a triangle field and a rope of a certain length (Figure-1), you are required to use the rope to enclose a region within the field and make the region as large as possible.



Input

The input has several sets of test data. Each set is one line containing four numbers separated by a space. The first three indicate the lengths of the edges of the triangle field, and the fourth is the length of the rope. Each of the four numbers have exactly four digits after the decimal point. The line containing four zeros ends the input and should not be processed. You can assume each of the edges are not longer than 100.0000 and the length of the rope is not longer than the perimeter of the field.

Output

Output one line for each case in the following format:


Case i: X


Where i is the case number, and X is the largest area which is rounded to two digits after the decimal point.

Sample Input

12.0000 23.0000 17.0000 40.0000 
84.0000 35.0000 91.0000 210.0000
100.0000 100.0000 100.0000 181.3800
0 0 0 0

Sample Output

Case 1: 89.35 
Case 2: 1470.00
Case 3: 2618.00

Source

题意:
给定三角形的边a,b,c,和绳子长度d,求绳子在三角形内能围成的最大面积。
如果任意给定一条绳子,当绳子围成圆时面积最大。
三种情况

1当绳子长度大于等于三角形周长时,最大面积是三角形面积
2当绳子长度小于等于三角形内切圆周长时,最大面积是绳子围成的圆的面积
3介于两者之间时,尽量沿三角形边展开

这里写图片描述

此时面积是三角形面积S-小三角形面积s+小三角形内切圆面积s’

#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;

double solve(double a,double b,double c,double d){
    double Pi = acos(-1.0);
    double L = a+b+c;
    double cosA = (b*b+c*c-a*a)/(2*b*c);
    double S=sqrt(1-cosA*cosA)*0.5*b*c;
    double r = S*2/L;
    //printf("d---%.2lf L----%.2lf 2*Pi*r----%.2lf S----%.2lf\n",d,L,2*Pi*r,S);
    if(d>=L){
        return S;
    }
    else if(2*Pi*r>=d) return d*d/(4*Pi);//周长不如内切圆大
    else {
        double t = (L-d)/(L-2*Pi*r);
        //double t = (L-d)/L;
        double rr = r*t;
        return S-S*t*t+Pi*rr*rr;

    }
}
int main()
{
    double a,b,c,d;
    int cas=0;
    while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
    {

        if(a==0&&b==0&&c==0&&d==0) break;
        else{
                double ans;
                ans = solve(a,b,c,d);
                printf("Case %d: %.2lf\n",++cas,ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hanyanwei123/article/details/81782952