2013
12-11

# Area in Triangle

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.

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 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.

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

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

1.当自由线的长度不小于三角形的周长时，自由线所围成的面积就是三角形的面积；

2.当自由线的在三角形内能围成一个圆的时候，则这个圆的面积就是自由线所能围成的面积，且这个最大的圆恰好就是三角形的内切圆
3.当自由线从内切圆那种情况继续膨胀到能与三角形的边贴近但长度小于三角形周长时，将这个已经围成的面积划分为三个部分：

#include<stdio.h>
#include<math.h>
#define eps 1e-9
#define pi acos(-1.0)
int main()
{
int T=0;
double a,b,c,len,p0,p1,S,R,r;
//len为自由线的长度；p0为原三角形的周长；p1为原三角形的半周长；
//R为原三角形的内切圆半径；r为相似三角形的内切圆半径。
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&len),a+b+c+len)
{
printf("Case %d: ",++T);
p0=a+b+c;
p1=p0/2;
S=sqrt(p1*(p1-a)*(p1-b)*(p1-c));
if(len>=p0)  {printf("%.2lf\n",S);continue;} //自由线长大于三角形周长
R=2*S/p0; //三角形内切圆公式S=p0*R/2; R为内切圆半径
if(2*pi*R-len>eps)
{
R=len/pi/2;
S=pi*R*R;
printf("%.2lf\n",S);
continue;
}
//利用的就是三角形相似的原理；公式；p0/R*(R-r)=len-2*pi*R;左边是通过内切圆半径与周长的关系求
//得小三角形的周长；右边是通过自由线的长度减掉三段弧得到相似三角形的周长；
r=(p0-len)/(p0/R-2*pi);//三角形相似
a=a/R*(R-r); b=b/R*(R-r);  c=c/R*(R-r);
p=(a+b+c)/2;
S=pi*r*r+sqrt(p*(p-a)*(p-b)*(p-c))+r*(a+b+c);//分三个部分求面积
printf("%.2lf\n",S);
}
}