Computer Programming Contest Preparation

ToolBox - Source for: 4/438/i1.c



/home/toolbox/public_html/solutions/4/438/i1.c
    1 /* @JUDGE_ID:  823125  Prob 438 C "Use of equation of the circle to compute circumference." */
    2 /**************
    3  Solution to Problem 438
    4  by: Theodore A. Irra
    5  date: 02/09/2016
    6  **************/
    7 #include <stdio.h>
    8 #include <math.h> /* for sqrt function */
    9 #define PI 3.141592653589793 /* Since input data is accurate to only 1 decimal */
   10 /* place and circumference is to be rounded off to 2 decimal places pi should */
   11 /* be 3.142 but this value used to match sample output. Algebraic manipulation */
   12 /* of equation (x-a)^2+(y-b)^2=r^2 with substitution of the point coordinates  */
   13 /* yields system of 3 linear equations, one of which is a linear combination of */
   14 /* the other two, which by Gauss elimination and back substitution allows computation */
   15 /* of radius and circumference. Two pecial cases to avoid division by zero are handled */
   16 double r, /* radius */
   17        ctrX,ctrY, /* x-coordinate and y-coordinate of center */
   18        circ; /* circumference */
   19 double X1,Y1,X2,Y2,X3,Y3; /* input point coordinates for P1, P2, P3 */
   20 int main()
   21 {
   22     /* EOFin std input is(<ctrl+d> in UNIX, <ctrl+z> in DOS) */
   23     while(scanf("%lf %lf %lf %lf %lf %lf",&X1,&Y1,&X2,&Y2,&X3,&Y3)!=EOF)
   24         {
   25             ctrX=((((X2*X2)-(X3*X3)+(Y2*Y2)-(Y3*Y3))*((2*Y1)-(2*Y2)))-(((X1*X1)-(X2*X2)+(Y1*Y1)-(Y2*Y2))*((2*Y2)-(2*Y3))))/
   26                  ((((2*Y1)-(2*Y2))*((2*X2)-(2*X3)))-(((2*X1)-(2*X2))*((2*Y2)-(2*Y3))));
   27             if (!(Y2==Y3)) /* special case to avoid division by zero */
   28                 ctrY=(((X2*X2)-(X3*X3)+(Y2*Y2)-(Y3*Y3))/((2*Y2)-(2*Y3)))-((((2*X2)-(2*X3))*ctrX)/((2*Y2)-(2*Y3)));
   29             if (!(Y1==Y2)) /* special case to avoid division by zero */
   30                 ctrY=(((X1*X1)-(X2*X2)+(Y1*Y1)-(Y2*Y2))/((2*Y1)-(2*Y2)))-((((2*X1)-(2*X2))*ctrX)/((2*Y1)-(2*Y2)));
   31             r=sqrt(((X1-ctrX)*(X1-ctrX))+((Y1-ctrY)*(Y1-ctrY)));
   32             circ=2*PI*r;
   33             printf("%.2lfx\n",circ);
   34         }
   35     return(0);
   36 }
   37