Computer Programming Contest Preparation

ToolBox - Source for: 4/478/a.c



/home/toolbox/public_html/solutions/4/478/a.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 <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015-03-25
   18  * Purpose:
   19  * Problem: 478
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_FIGURES 11
   27 #define RECTANGLE 1
   28 #define CIRCLE 2
   29 #define TRIANGLE 3
   30 
   31 /*
   32  *  Author: Isaac Traxler
   33  *    Date: 20120730
   34  * Purpose: Fun
   35  * Problem: Of Circles and Squares (10823)
   36  */
   37 
   38 struct pointStruct
   39 {
   40     /* STRUCT pointStruct */
   41     double x;
   42     double y;
   43 }; /* STRUCT pointStruct */
   44 
   45 struct figureStruct
   46 {
   47     /* STRUCT figureStruct */
   48     int knd;  /* 0 undefined, 1 square, 2 circle */
   49     struct pointStruct p[3];
   50     /* rectangle             circle           triangle
   51        [0] is upper left     [0] is center    [0] vertex 1
   52        [1] is lower right    [1].x is radius  [1] vertex 2
   53                                               [2] vertex 3 */
   54 }; /* STRUCT figureStruct */
   55 
   56 struct figureStruct figures[MAX_FIGURES];
   57 int figureCnt = 0;
   58 double x;
   59 double y;
   60 int pointCnt = 0;
   61 
   62 void init()
   63 {
   64     /* FUNCTION init */
   65     char ch;
   66 
   67     scanf("%c ", &ch);
   68     while ('*' != ch)
   69         {
   70             /* while */
   71             if ('r' == ch)
   72                 {
   73                     /* rectangle */
   74                     figures[figureCnt].knd = RECTANGLE;
   75                     scanf(" %lf %lf %lf %lf ", &figures[figureCnt].p[0].x, &figures[figureCnt].p[0].y, &figures[figureCnt].p[1].x, &figures[figureCnt].p[1].y);
   76                 } /* rectangle */
   77             else if ('t' == ch)
   78                 {
   79                     /* triangle */
   80                     figures[figureCnt].knd = TRIANGLE;
   81                     scanf(" %lf %lf %lf %lf %lf %lf ", &figures[figureCnt].p[0].x, &figures[figureCnt].p[0].y, &figures[figureCnt].p[1].x, &figures[figureCnt].p[1].y, &figures[figureCnt].p[2].x, &figures[figureCnt].p[2].y);
   82                 } /* triangle */
   83             else
   84                 {
   85                     /* circle */
   86                     figures[figureCnt].knd = CIRCLE;
   87                     scanf(" %lf %lf %lf ", &figures[figureCnt].p[0].x, &figures[figureCnt].p[0].y, &figures[figureCnt].p[1].x);
   88                 } /* circle */
   89             figureCnt++;
   90             scanf("%c ", &ch);
   91         } /* while */
   92 } /* FUNCTION init */
   93 
   94 void dump()
   95 {
   96     /* FUNCTION dump */
   97 } /* FUNCTION dump */
   98 
   99 int getInput()
  100 {
  101     /* FUNCTION getInput */
  102     int dataReadFlag;
  103 
  104     scanf(" %lf %lf ", &x, &y);
  105     DEBUG printf(" %lf %lf\n", x, y);
  106     dataReadFlag = (x != 9999.9) || (y != 9999.9);
  107     pointCnt++;
  108     return (dataReadFlag);
  109 } /* FUNCTION getInput */
  110 
  111 int sign(double x, double y, struct pointStruct v1, struct pointStruct v2)
  112 {
  113     /* FUNCTION sign */
  114     return (x - v2.x) * (v1.y - v2.y) - (v1.x - v2.x) * (y - v2.y);
  115 } /* FUNCTION sign */
  116 
  117 int triangleCheck(double x, double y, struct pointStruct v1, struct pointStruct v2, struct pointStruct v3)
  118 {
  119     /* FUNCTION triangleCheck */
  120     int b1;
  121     int b2;
  122     int b3;
  123 
  124     b1 = sign(x, y, v1, v2) < 0.0;
  125     b2 = sign(x, y, v2, v3) < 0.0;
  126     b3 = sign(x, y, v3, v1) < 0.0;
  127     DEBUG printf("%d %d %d\n", b1, b2, b3);
  128 
  129     return ((b1 == b2) && (b2 == b3));
  130 } /* FUNCTION triangleCheck */
  131 
  132 int check(double x, double y, struct figureStruct f)
  133 {
  134     /* FUNCTION check */
  135     int toReturn;
  136     double t1;
  137     double t2;
  138     double tmp;
  139 
  140     if (RECTANGLE == f.knd)
  141         {
  142             /* rectangle */
  143             toReturn = (x>f.p[0].x) && (x<f.p[1].x) && (y<f.p[0].y) && (y>f.p[1].y);
  144         } /* rectangle */
  145     else if (TRIANGLE == f.knd)
  146         {
  147             /* triangle */
  148             toReturn = triangleCheck(x, y, f.p[0], f.p[1], f.p[2]);
  149         } /* triangle */
  150     else
  151         {
  152             /* circle */
  153             t1 = x - f.p[0].x;
  154             t2 = y - f.p[0].y;
  155             tmp = sqrt((t1 * t1) + (t2 * t2));
  156             toReturn = (tmp < f.p[1].x);
  157         } /* circle */
  158     return toReturn;
  159 } /* FUNCTION check */
  160 
  161 void process()
  162 {
  163     /* FUNCTION process */
  164     int i;
  165     int found = FALSE;
  166 
  167     for (i=0; i<figureCnt; i++)
  168         {
  169             /* for */
  170             if (check(x, y, figures[i]))
  171                 {
  172                     /* contained */
  173                     printf("Point %d is contained in figure %d\n", pointCnt, i+1);
  174                     found = TRUE;
  175                 } /* contained */
  176         } /* for */
  177     if (! found)
  178         {
  179             /* not found */
  180             printf("Point %d is not contained in any figure\n", pointCnt);
  181         } /* not found */
  182 } /* FUNCTION process */
  183 
  184 int main()
  185 {
  186     /* main */
  187     int moreToDo;
  188 
  189     init();
  190     moreToDo = getInput();
  191     while (moreToDo)
  192         {
  193             /* while */
  194             process();
  195             moreToDo = getInput();
  196         } /* while */
  197 
  198     return EXIT_SUCCESS;
  199 } /* main */
  200