/home/toolbox/public_html/solutions/1/143/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 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (TRUE)
15
16 /*
17 * Author: Isaac Traxler
18 * Date: 2021-10-26
19 * Purpose: fun
20 * Problem: 143
21 */
22
23 /*
24 * This template reads data until a terminating value is reached.
25 */
26
27 /*
28 * zR - expand all zM - closw all
29 * zo - open a fold zc - close a fold
30 * za - toggle fold
31 */
32
33 #define CLOCKWISE 1
34 #define COUNTER_CLOCKWISE -1
35
36 typedef struct pointStruct
37 {
38 /* begin pointStruct */
39 double x;
40 double y;
41 } /* end pointStruct */
42 pointStruct;
43
44 typedef struct triangleStruct
45 {
46 /* begin triangleStruct */
47 pointStruct s1;
48 pointStruct s2;
49 pointStruct s3;
50 } /* end triangleStruct */
51 triangleStruct;
52
53 triangleStruct tri;
54 int triangleDirection;
55
56 void init()
57 {
58 /* FUNCTION init */
59 } /* FUNCTION init */
60
61 void dump()
62 {
63 /* FUNCTION dump */
64 } /* FUNCTION dump */
65
66 int triMax(double a, double b, double c)
67 {
68 /* FUNCTION triMax */
69 int toReturn;
70
71 if (a > b)
72 {
73 /* a is bigger */
74 if (a > c)
75 {
76 /* a is bigger */
77 toReturn = (a + 0.5);
78 } /* a is bigger */
79 else
80 {
81 /* c is bigger */
82 toReturn = (c + 0.5);
83 } /* c is bigger */
84 } /* a is bigger */
85 else
86 {
87 /* b is bigger */
88 if (b > c)
89 {
90 /* b is bigger */
91 toReturn = (b + 0.5);
92 } /* b is bigger */
93 else
94 {
95 /* c is bigger */
96 toReturn = (c + 0.5);
97 } /* c is bigger */
98 } /* a is bigger */
99 if (99 < toReturn)
100 {
101 toReturn = 99;
102 }
103 return toReturn;
104 } /* FUNCTION triMax */
105
106 int triMin(double a, double b, double c)
107 {
108 /* FUNCTION triMin */
109 int toReturn;
110
111 if (a < b)
112 {
113 /* a is smaller */
114 if (a < c)
115 {
116 /* a is smaller */
117 toReturn = (a - 0.5);
118 } /* a is smaller */
119 else
120 {
121 /* c is smaller */
122 toReturn = (c - 0.5);
123 } /* c is smaller */
124 } /* a is smaller */
125 else
126 {
127 /* b is smaller */
128 if (b < c)
129 {
130 /* b is smaller */
131 toReturn = (b - 0.5);
132 } /* b is smaller */
133 else
134 {
135 /* c is smaller */
136 toReturn = (c - 0.5);
137 } /* c is smaller */
138 } /* a is smaller */
139 if (1 > toReturn)
140 {
141 toReturn = 1;
142 }
143 return toReturn;
144 } /* FUNCTION triMin */
145
146 int getInput()
147 {
148 /* FUNCTION getInput */
149 int dataReadFlag;
150
151 scanf(" %lf %lf %lf %lf %lf %lf ", &tri.s1.x, &tri.s1.y, &tri.s2.x, &tri.s2.y, &tri.s3.x, &tri.s3.y);
152 dataReadFlag = ((0 != tri.s1.x) || (0 != tri.s1.y) ||
153 (0 != tri.s2.x) || (0 != tri.s2.y) ||
154 (0 != tri.s3.x) || (0 != tri.s3.y));
155 return (dataReadFlag);
156 } /* FUNCTION getInput */
157
158 int direction(pointStruct p0, pointStruct p1, pointStruct p2)
159 {
160 /* FUNCTION direction */
161 /* p0 an p2 are triangle, p1 is point to test */
162 /* algorithm form Cormen page 888 */
163 double tmpD;
164 int tmp;
165 int toReturn;
166
167 DEBUG printf("(((p1.x[%lf] - p0.x[%lf]) * (p2.y[%lf] - p0.y[%lf])) - ((p2.x[%lf] - p0.x[%lf]) * (p1.y[%lf] - p0.y[%lf])))\n", p1.x, p0.x, p2.y, p0.y, p2.x, p0.x, p1.y, p0.y);
168 tmpD = (((p1.x - p0.x) * (p2.y - p0.y)) - ((p2.x - p0.x) * (p1.y - p0.y)));
169 if (0 > tmpD)
170 {
171 /* positive */
172 tmp = COUNTER_CLOCKWISE;
173 } /* positive */
174 else if (0 < tmpD)
175 {
176 /* negative */
177 tmp = CLOCKWISE;
178 } /* negative */
179 else
180 {
181 /* zero */
182 tmp = 0;
183 } /* zero */
184 DEBUG printf("(%lf, %lf - %lf, %lf) (%lf, %lf) %d ?= %d\n", p0.x, p0.y, p2.x, p2.y, p1.x, p1.y, tmp, triangleDirection);
185 toReturn = tmp;
186 } /* FUNCTION direction */
187
188 int inside(int i, int j)
189 {
190 /* FUCNTION inside */
191 pointStruct tmp;
192 int pointInside = FALSE;
193
194 tmp.x = i;
195 tmp.y = j;
196 if ((triangleDirection != direction(tri.s1, tmp, tri.s2)) &&
197 (triangleDirection != direction(tri.s2, tmp, tri.s3)) &&
198 (triangleDirection != direction(tri.s3, tmp, tri.s1)))
199 {
200 /* found an interior point */
201 pointInside = TRUE;
202 } /* found an interior point */
203 return pointInside;
204 } /* FUCNTION inside */
205
206 int linearTriangle()
207 {
208 /* FUNCTION linearTriangle */
209 /* unfortunately the authours have decided that triangles with an area of 0 are triangles
210 * In other words, if all three verices are on the same line, it is still a triangle
211 */
212 } /* FUNCTION linearTriangle */
213
214 void process()
215 {
216 /* FUNCTION process */
217 int minX;
218 int minY;
219 int maxX;
220 int maxY;
221 int i;
222 int j;
223 int cnt = 0;
224
225 minX = triMin(tri.s1.x, tri.s2.x, tri.s3.x);
226 minY = triMin(tri.s1.y, tri.s2.y, tri.s3.y);
227 maxX = triMax(tri.s1.x, tri.s2.x, tri.s3.x);
228 maxY = triMax(tri.s1.y, tri.s2.y, tri.s3.y);
229
230 triangleDirection = - direction(tri.s1, tri.s3, tri.s2);
231 DEBUG printf("X(%d-%d) Y*%d-%d)\n", minX, maxX, minY, maxY);
232 for (i=minX; i<=maxX; i++)
233 {
234 /* try all possible X values */
235 for (j=minY; j<=maxY; j++)
236 {
237 /* try all possible Y values */
238 DEBUG printf("Trying (%d, %d)\n", i, j);
239 if (inside(i, j))
240 {
241 /* point is inside */
242 cnt++;
243 DEBUG printf("inside (cnt %d)\n", cnt);
244 } /* point is inside */
245 } /* try all possible Y values */
246 } /* try all possible X values */
247 DEBUG printf("Answer: ");
248 printf("%4d\n", cnt);
249 } /* FUNCTION process */
250
251 int main()
252 {
253 /* main */
254 int moreToDo;
255
256 init();
257 moreToDo = getInput();
258 while (moreToDo)
259 {
260 /* while */
261 process();
262 moreToDo = getInput();
263 } /* while */
264
265 return EXIT_SUCCESS;
266 } /* main */
267