/home/toolbox/public_html/solutions/108/10823/e.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
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14 #define DEBUG1 if (FALSE)
15
16 #define MAX_OBJECTS 512
17 #define SQUARE 1
18 #define CIRCLE 2
19
20 /*
21 * Author: Isaac Traxler
22 * Date: 20120730
23 * Purpose: Fun
24 * Problem: Of Circles and Squares (10823)
25 */
26
27 struct colorStruct
28 {
29 /* STRUCT colorStruct */
30 int R;
31 int G;
32 int B;
33 }; /* STRUCT colorStruct */
34
35 struct pointStruct
36 {
37 /* STRUCT pointStruct */
38 int x;
39 int y;
40 }; /* STRUCT pointStruct */
41
42 struct objectStruct
43 {
44 /* STRUCT objStruct */
45 int knd; /* 0 undefined, 1 square, 2 circle */
46 struct pointStruct p;
47 int lngth;
48 struct colorStruct c;
49 }; /* STRUCT objStruct */
50
51 int numberOfTimes;
52 int numObjects;
53 int numQueries;
54 struct objectStruct obj[MAX_OBJECTS];
55 struct pointStruct queries[MAX_OBJECTS];
56
57 void init()
58 {
59 /* FUNCTION init */
60 scanf("%d ", &numberOfTimes);
61 } /* FUNCTION init */
62
63 void getInput()
64 {
65 /* FUNCTION getInput */
66 int i;
67 char kind[10];
68
69 scanf(" %d %d ", &numObjects, &numQueries);
70
71 /* get objects */
72 for (i=0; i<numObjects; i++)
73 {
74 /* for each object */
75 obj[i].knd = 0;
76 scanf("%s %d %d %d %d %d %d ", kind, &obj[i].p.x, &obj[i].p.y, &obj[i].lngth, &obj[i].c.R, &obj[i].c.G, &obj[i].c.B);
77 if ('S' == kind[0])
78 {
79 obj[i].knd = 1;
80 }
81 else if ('C' == kind[0])
82 {
83 obj[i].knd = 2;
84 }
85 else
86 {
87 exit(1); /* bail if invalid input */
88 }
89 } /* for each object */
90
91 /* get query points -- this could be done one at a time in process */
92 for (i=0; i<numQueries; i++)
93 {
94 /* load each query */
95 scanf(" %d %d ", &queries[i].x, &queries[i].y);
96 } /* load each query */
97 } /* FUNCTION getInput */
98
99 int onSquareBorder(int idx, struct pointStruct q)
100 {
101 /* FUNCTION onSquareBorder */
102 int toReturn;
103 /* check horizontals, then check verticals */
104 toReturn = ((q.x >= obj[idx].p.x) &&
105 (q.x <= (obj[idx].p.x + obj[idx].lngth)) &&
106 ((q.y == obj[idx].p.y) || (q.y == (obj[idx].p.y + obj[idx].lngth))))
107 ||
108 ((q.y >= obj[idx].p.y) &&
109 (q.y <= (obj[idx].p.y + obj[idx].lngth)) &&
110 ((q.x == obj[idx].p.x) || (q.x == (obj[idx].p.x + obj[idx].lngth))));
111 /* toReturn = toReturn && (obj[idx].lngth > 0); */
112 return toReturn;
113 } /* FUNCTION onSquareBorder */
114
115 int onCircleBorder(int idx, struct pointStruct q)
116 {
117 /* FUNCTION onCircleBorder */
118 int toReturn;
119 int d2;
120
121 /* do distance as squares to avoid squareroot */
122 d2 = obj[idx].lngth * obj[idx].lngth;
123 /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
124 toReturn = ((q.x-obj[idx].p.x)*(q.x-obj[idx].p.x) + (q.y-obj[idx].p.y)*(q.y-obj[idx].p.y) == d2);
125 return toReturn;
126 } /* FUNCTION onCircleBorder */
127
128 int inSquare(int idx, struct pointStruct q)
129 {
130 /* FUNCTION inSquare */
131 int toReturn;
132
133 toReturn = (q.x > obj[idx].p.x) && (q.x < (obj[idx].p.x + obj[idx].lngth)) &&
134 (q.y > obj[idx].p.y) && (q.y < (obj[idx].p.y + obj[idx].lngth));
135 return toReturn;
136 } /* FUNCTION inSquare */
137
138 int inCircle(int idx, struct pointStruct q)
139 {
140 /* FUNCTION inCircle */
141 int toReturn;
142 int d2;
143
144 /* do distance as squares to avoid squareroot */
145 d2 = obj[idx].lngth * obj[idx].lngth;
146 /* if radius^2 == (x-px)^2 + (y-py)^2 then it is on border */
147 toReturn = (((q.x-obj[idx].p.x)*(q.x-obj[idx].p.x) + (q.y-obj[idx].p.y)*(q.y-obj[idx].p.y)) < d2);
148 return toReturn;
149 } /* FUNCTION inCircle */
150
151 void process()
152 {
153 /* FUNCTION process */
154 int i;
155 int j;
156 int boundary;
157 int interior;
158 struct colorStruct color;
159 int colorCnt;
160
161 for (i=0; i<numQueries; i++)
162 {
163 /* do each query */
164 boundary = FALSE;
165 for (j=0; (j<numObjects) && (!boundary); j++)
166 {
167 /* for each object */
168 if (SQUARE == obj[j].knd)
169 {
170 boundary = onSquareBorder(j, queries[i]);
171 }
172 if (CIRCLE == obj[j].knd)
173 {
174 boundary = onCircleBorder(j, queries[i]);
175 }
176 } /* for each object */
177 if (boundary)
178 {
179 /* on boundary */
180 printf("(0, 0, 0)\n");
181 } /* on boundary */
182 else
183 {
184 /* check interior */
185 color.R = 0;
186 color.G = 0;
187 color.B = 0;
188 colorCnt = 0;
189 for (j=0; j<numObjects; j++)
190 {
191 /* for each object */
192 interior = FALSE;
193 if (SQUARE == obj[j].knd)
194 {
195 /* check for inside SQUARE */
196 interior = inSquare(j, queries[i]);
197 } /* check for inside SQUARE */
198 if (CIRCLE == obj[j].knd)
199 {
200 /* check for inside CIRCLE */
201 interior = inCircle(j, queries[i]);
202 } /* check for inside CIRCLE */
203 if (interior)
204 {
205 /* point is in interior of object */
206 color.R = color.R + obj[j].c.R;
207 color.G = color.G + obj[j].c.G;
208 color.B = color.B + obj[j].c.B;
209 colorCnt++;
210 } /* point is in interior of object */
211 } /* for each object */
212 if (0 == colorCnt)
213 {
214 /* not inside any objects */
215 printf("(255, 255, 255)\n");
216 } /* not inside any objects */
217 else
218 {
219 /* inside 1 or more objects */
220 DEBUG1 printf("R: %d G: %d B: %d Count: %d\n", color.R, color.G, color.B, colorCnt);
221 color.R = (2 * color.R + colorCnt) / (2 * colorCnt);
222 color.G = (2 * color.G + colorCnt) / (2 * colorCnt);
223 color.B = (2 * color.B + colorCnt) / (2 * colorCnt);
224 printf("(%d, %d, %d)\n", color.R, color.G, color.B);
225 } /* inside 1 or more objects */
226 } /* check interior */
227 } /* do each query */
228 } /* FUNCTION process */
229
230 int main ()
231 {
232 /* main */
233 int i;
234
235 init();
236 for (i=1; i<=numberOfTimes; i++)
237 {
238 /* while */
239 if (1 != i)
240 {
241 printf("\n");
242 }
243 getInput();
244 printf("Case %d:\n", i);
245 process();
246 } /* while */
247
248 return EXIT_SUCCESS;
249 } /* main */
250
251