import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Carpet Stains * * Consider a point P in the middle of an equilateral triangle, * such that the distance from P to each of the vertices is a, b and c. * * We want to find the area of the triangle. Since it's equilateral, * all three sides have the same length. Let's call it x. If we can find x, * then the area is just x*x*sqrt(3)/4. * * Well, x forms a triangle with a and b, with b and c, and with a and c. * We need to find an x such that the central angles add up to 360 degrees (or 2pi radians). * * We'll use a bisection root finding method to find x. * * @author vanb */ public class carpet_vanb { public Scanner sc; public PrintStream ps; public static final double TWOPI = Math.PI + Math.PI; public static final double SQRT3OVER4 = Math.sqrt( 3.0 ) / 4.0; public static final double EPSILON = 0.00001; public float a, b, c; /** * Given the lengths of three sides of a triangle, return one of its angles. * The three lengths are a, b and c. This method returns the angle between a and b, opposite c. * * Use the Law of Cosines. * * c*c = a*a + b*b - 2*a*b*cos(theta) * where theta is the angle opposite c. * * @param a One leg of a triangle * @param b Another leg of a triangle * @param c Third leg of a triangle * @return The angle between legs a and b, opposite c */ public static double angle( double a, double b, double c ) { double angle = 0.0; if( Math.abs(c-a-b)a+b, then x, a, b cannot form a triangle. double hi = Math.min( Math.min( a+b, a+c ), b+c ); // Default, if we can't find an answer double area = -1.0; //System.err.println( lo + " " + hi ); if( lo<=hi ) //System.err.println( f(lo) + " " + f(hi) ); // Check to see if it's even possible. if( lo<=hi+EPSILON && f(lo)*f(hi)<=EPSILON ) { // Use bisection method until lo & hi are the same (within epsilon) while( hi-lo > 0.000000001 ) { double mid = (lo+hi)/2.0; if( f(mid)<0.0 ) lo=mid; else hi=mid; } // lo is the x we're looking for. // So is hi, since they're close enough (within epsilon) area = lo*hi*SQRT3OVER4; } ps.printf( "%.3f", area ); ps.println(); // This code was used by the judges to minimize problems with roundoff error // // String answer = String.format( "%.5f", area ); // if( answer.endsWith( "50" ) || answer.endsWith( "49" ) ) // System.err.println( "PANIC!! Too close to a cusp!" + answer ); } } /** * @param args */ public static void main( String[] args ) throws Exception { new carpet_vanb().doit(); } }