Sphere in a Tetrahedron

I came across a question in Codechef(TETRA) and SPOJ(TETRA) named ‘Sphere in a tetrahedron’. The problem asks us to calculate the radius of the sphere that fits into tetrahedron such that all its faces are tangents to the sphere.

With no prior knowledge of such concept I kept searching on net and found some topic to solve this question. That concept says that radius of sphere that could be inscribed in a tetrahedron is

 r = (3 * vol of tetrahedron)/surface area of tetrahedron

Let PQRS be the tetrahedron and a,b,c,d,e,f be 6 edge lengths of that. Let us assume PQ=a, PS=b, PR=c, QS=d, QR=e, RS=f.

Now, according to problem statement, lengths of edges of tetrahedron is given. So for given edge lengths, what would be the volume of tetrahedron?.

Formula for volume is,
volume_of_tetrahedron

Total surface area can be found by summing up area of each triangular face. For area of triangle when given lengths, can be found using
area_of_traingle

Where x,y,z are lengths of triangle and s = (x+y+z)/2.

And finally we can find the result using the formula mentioned earlier.

C code of this problem.

#include<stdio.h>
#include<math.h>
double sq(double x)
{
    return x*x;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t>0)
    {
        double a,b,c,d,e,f; /* 6 edge lengths */
        double vol; /* volume */
        scanf("%lf %lf %lf %lf %lf %lf",&a,&b,&c,&d,&e,&f);
        vol  =  sqrt(4*sq(a)*sq(b)*sq(c)-sq(a)*sq(sq(b)+sq(c)-sq(f))-sq(b)*sq(sq(a)+sq(c)-sq(e))-sq(c)*sq(sq(a)+sq(b)-sq(d))+(sq(b)+sq(c)-sq(f))*(sq(c)+sq(a)-sq(e))*(sq(a)+sq(b)-sq(d)))/12;
        double sa = 0; /* surface area */
        double t1,t2; /* temporary variables */
        double as1,as2,as3,as4; /* area of 4 faces */
        t1 = (a+b+d)/2.0;
        t2 = t1*(t1-a)*(t1-b)*(t1-d);
        as1 = sqrt(t2);
        t1 = (a+c+e)/2.0;
        t2 = t1*(t1-a)*(t1-c)*(t1-e);
        as2 = sqrt(t2);
        t1 = (b+c+f)/2.0;
        t2 = t1*(t1-b)*(t1-c)*(t1-f);
        as3 = sqrt(t2);
        t1 = (d+e+f)/2.0;
        t2 = t1*(t1-d)*(t1-e)*(t1-f);
        as4 = sqrt(t2);
        sa = as1+as2+as3+as4;
        float r = 3.0*vol/sa;
        printf("%.4lf\n",r);
        t--;
    }
    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s