Computer Programming Contest Preparation

ToolBox - Source for: 109/10991/b.c



/home/toolbox/public_html/solutions/109/10991/b.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 20221018
   21  * Purpose: fun
   22  * Problem: 10991 - Region
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  *
   28  *
   29  * This problem is to find the ara outsied three circles that intersect each other at two points each
   30  * Imagine pushing three circles together so that each circle touches each other circle in exactly 2
   31  * points.
   32  *
   33  * You can calculate the area of the triangle formed with verticies at the center of each circle.
   34  * You can then determine how much area of each of the three circles is in each triangle bu calculating
   35  * the area of the circle and then multiplying that times the percentage of the circle in the triangle.
   36  * Now sum these three ara and subtract from area of triangle to get desireed area (Area G in the
   37  * problem description).
   38  *
   39  * triangle area is 1/2 * b * h
   40  *
   41  */
   42 
   43 int numberOfTimes;
   44 double r1;
   45 double r2;
   46 double r3;
   47 double triangleArea;
   48 double t1;
   49 double t2;
   50 double t3;
   51 double PI = (double) 3.141592653589793;
   52 
   53 void init()
   54 {
   55     /* FUNCTION init */
   56     scanf("%d ", &numberOfTimes);
   57 } /* FUNCTION init */
   58 
   59 void dump()
   60 {
   61     /* FUNCTION dump */
   62 } /* FUNCTION dump */
   63 
   64 void getInput()
   65 {
   66     /* FUNCTION getInput */
   67     scanf(" %lf %lf %lf ", &r1, &r2, &r3);
   68     printf("(r1 %lf) (r2 %lf) (r3 %lf)\n", r1, r2, r3);
   69 } /* FUNCTION getInput */
   70 
   71 double calcTriangleArea(double r1, double r2, double r3)
   72 {
   73     /* FUNCTION calcTriangleArea */
   74     double ret;
   75     double s;
   76     double a;
   77     double b;
   78     double c;
   79 
   80 
   81     /* heron's formula tells us how to get area of arbitrary triangle
   82      * a, b and c are the length of the sides
   83      * s = (a + b + c) / 2 (perimiter)
   84      * area = sqrt(s * (s-a) * (s-b) * (s-c)
   85      */
   86 
   87     a = r1 + r2;
   88     b = r2 + r3;
   89     c = r3 + r1;
   90 
   91     /* here is the normal calculation
   92     s = (a + b + c) / 2;
   93     or
   94     s = ((r1 + r2) + (r2 + r3) + (r3 + r1)) / 2;
   95     which simplies to:
   96     s = r1 + r2 + r3;
   97     */
   98 
   99     s = r1 + r2 + r3;
  100 
  101     ret = (sqrt(s * (s - a) * (s - b) * (s - c)));
  102     printf("(a %lf) (b %lf) (c %lf) (s %lf) (area %lf)\n", a, b, c, s, ret);
  103     return ret;
  104 } /* FUNCTION calcTriangleArea */
  105 
  106 double calcAngle(double a, double b, double c)
  107 {
  108     /* FUNCTION calcAngle */
  109     /* this will use law of cosines to calculate angle
  110      * c*c = a*a + b*b - 2*a*b*cos(c)
  111      * 2*a*b*cos(c) = a*a + b*b - c*c
  112      * cos(c) = (a*a + b*b - c*c) / (2*a*b)
  113      * angle = arccos(((a * a) + (b * b) - (c * c) / 2 * a * b))
  114      */
  115     double top;
  116     double bottom;
  117     double angle;
  118     double angleDegrees;
  119 
  120     top = (a * a) + (b * b) - (c * c);
  121     bottom = 2 * a * b;
  122     angle = (acos(top / bottom));
  123     angleDegrees = angle * 180 / PI;
  124     printf("calcAngle: (a %lf) (b %lf) (c %lf) (top %lf) (bottom %lf) (div %lf) (angle %lf %lf)\n", a, b, c, top, bottom, (top/bottom), angle, angleDegrees);
  125     return angleDegrees;
  126 } /* FUNCTION calcAngle */
  127 
  128 void process()
  129 {
  130     /* FUNCTION process */
  131     double s1;
  132     double s2;
  133     double s3;
  134     double angle1;
  135     double angle2;
  136     double angle3;
  137     double area1;
  138     double area2;
  139     double area3;
  140     triangleArea = calcTriangleArea(r1, r2, r3);
  141     printf("triangle area = %lf\n", triangleArea);
  142     s1 = r1 + r2;
  143     s2 = r2 + r3;
  144     s3 = r1 + r3;
  145     /* calculate the 3 angles in radians */
  146     angle1 = calcAngle(s1, s2, s3);
  147     angle2 = calcAngle(s2, s3, s1);
  148     angle3 = calcAngle(s3, s1, s2);
  149     printf(" sum of angles: %lf + %lf + %lf = %lf\n", angle1, angle2, angle3, angle1 + angle2 + angle3);
  150     /* calculate sub area for each slice */
  151     /* area1 = (PI * r1 * r1) * (angle1 / (2 * PI)) simplifies to r1 * r1 * angle1 / 2 */
  152     area1 = PI * r2 * r2 * angle1 / 360;
  153     area2 = PI * r3 * r3 * angle2 / 360;
  154     area3 = PI * r1 * r1 * angle3 / 360;
  155     printf("cirle: %lf  area %lf  angle: %lf  portion: %lf\n", r1, (PI*r1*r1), angle1, area1);
  156     printf("cirle: %lf  area %lf  angle: %lf  portion: %lf\n", r2, (PI*r2*r2), angle2, area2);
  157     printf("cirle: %lf  area %lf  angle: %lf  portion: %lf\n", r3, (PI*r3*r3), angle3, area3);
  158     printf("(sides: %lf,%lf,%lf  angle: %lf  area: %lf)\n", s1, s2, s3, angle1, area1);
  159     printf("(sides: %lf,%lf,%lf  angle: %lf  area: %lf)\n", s2, s3, s1, angle2, area2);
  160     printf("(sides: %lf,%lf,%lf  angle: %lf  area: %lf)\n", s3, s1, s2, angle3, area3);
  161     printf("%lf\n", triangleArea - area1 - area2 - area3);
  162 } /* FUNCTION process */
  163 
  164 int main()
  165 {
  166     /* main */
  167     int i;
  168 
  169     init();
  170     for (i=0; i<numberOfTimes; i++)
  171         {
  172             /* while */
  173             getInput();
  174             process();
  175         } /* while */
  176 
  177     return EXIT_SUCCESS;
  178 } /* main */
  179 
  180