uva 1447 - Malfatti Circles


題意:1つの三角形の3つの頂点の座標を与えて、3つの円を求めて、各円と三角形の2つの辺と他の2つの円がすべて正接するようにして、図のように、この3つの円の半径を出力します.
 
#include<iostream>
#include<iomanip>
#include<cmath>
#define sqr(a) ((a)*(a))
#define eps 1e-8
#define min(a,b) (a)<(b)?(a):(b)

using namespace std;

int sig(double a)
{
    return (a>eps)-(a<-eps);
}

int main()
{
    double x1,x2,x3,y1,y2,y3;
    double l1,l2,l3,th1,th2,th3;
    double l,r,mid,r1,r2,r3;
    double a,b,tmp;
    while(cin>>x1>>y1>>x2>>y2>>x3>>y3 && x1+x2+x3+y1+y2+y3)
    {
        l1=sqrt(sqr(x1-x2)+sqr(y1-y2));
        l2=sqrt(sqr(x3-x2)+sqr(y3-y2));
        l3=sqrt(sqr(x1-x3)+sqr(y1-y3));
        th1=acos((sqr(l1)+sqr(l3)-sqr(l2))/(2*l1*l3))/2;
        th2=acos((sqr(l1)+sqr(l2)-sqr(l3))/(2*l1*l2))/2;
        th3=acos((sqr(l2)+sqr(l3)-sqr(l1))/(2*l2*l3))/2;
        l=0;r=min(l1*tan(th2),l2*tan(th2));
        mid=r/2;
        while(sig(r-l)>0)
        {
            a=sqrt(mid+(l1-mid/tan(th2))/tan(th1))-sqrt(mid);
            b=sqrt(mid+(l2-mid/tan(th2))/tan(th3))-sqrt(mid);
            tmp=sqr(a)*tan(th1)+sqr(b)*tan(th3)+2*a*b*tan(th1)*tan(th3);
            if(sig(tmp-l3)==0) break;
            else if(sig(tmp-l3)<0) r=mid;
            else l=mid;
            mid=(l+r)/2;
        }
        r2=mid;
        r1=sqr(a*tan(th1));
        r3=sqr(b*tan(th3));
        cout<<fixed<<setprecision(6)<<r1<<" "<<r2<<" "<<r3<<endl;
    }
    return 0;
}