Algorithm
Problem link- https://www.spoj.com/problems/TETRA/
TETRA - Sphere in a tetrahedron
Of course a Sphere Online Judge System is bound to have some tasks about spheres. So here is one. Given the lengths of the edges of a tetrahedron calculate the radius of a sphere inscribed in that tetrahedron (i.e. a sphere tangent to all the faces).
Input
Number N of test cases in a single line. ( N <= 30 ) Each of the next N lines consists of 6 integer numbers -- the lengths of the edges of a tetrahedron separated by single spaces. The edges are not longer than 1000 and for the tetrahedron WXYZ, the order of the edges is: WX, WY, WZ, XY, XZ, YZ.
Output
N lines, each consisting of a real number given with four digits decimal precision equal to the radius of a sphere inscribed in the given tetrahedron.
Example
Input: 2 1 1 1 1 1 1 1000 999 998 5 5 6 Output: 0.2041 1.4189
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
using namespace std;
double surfacearea(double a1,double a2,double a3)
{
double s=(a1+a2+a3)/2.0;
return sqrt(s*(s-a1)*(s-a2)*(s-a3));
}
int main(){
int t=0;
scanf("%d",&t);
double U,V,W,u,v,w;
while(t--){
scanf("%lf%lf%lf%lf%lf%lf",&u,&W,&V,&v,&w,&U);
double uu = (v*v) + (w*w) - (U*U);
double vv = (w*w) + (u*u) - (V*V);
double ww = (u*u) + (v*v) - (W*W);
double vol = (4*u*u*v*v*w*w)-(u*u*uu*uu)-(v*v*vv*vv)-(w*w*ww*ww)+(uu*vv*ww);
double volume = (sqrt(vol)) / 12;
double surface = surfacearea(u,v,W)+surfacearea(v,w,U)+surfacearea(u,V,w)+surfacearea(W,V,U);
printf("%.4lf\n", 3.0*volume/surface);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 1 1 1 1 1
1000 999 998 5 5 6
Output
1.4189
#2 Code Example with Java Programming
Code -
Java Programming
#include <bits/stdc++.h>
using namespace std;
double area(double a,double b,double c)
{
double s=(a+b+c)/2.0;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
int main() {
int t;scanf("%d",&t);
while(t--)
{
double u,v,w,U,V,W;
cin>>u>>v>>w>>W>>V>>U;
double u1=v*v+w*w-U*U;
double v1=u*u+w*w-V*V;
double w1=u*u+v*v-W*W;
double vol=(sqrt(4*u*u*v*v*w*w-u*u*u1*u1-v*v*v1*v1-w*w*w1*w1+u1*v1*w1))/12;
double ar=0;
ar=area(U,V,W)+area(v,u,W)+area(w,v,U)+area(u,w,V);
double volt=3*vol/ar;
cout<<fixed<<setprecision(4)<<volt<<endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 1 1 1 1 1
1000 999 998 5 5 6
Output
1.4189
Demonstration
SPOJ Solution-TETRA Sphere in a tetrahedron-Solution in C, C++, Java, Python