testpat.c revision 1.4 1 /* $NetBSD: testpat.c,v 1.4 2021/01/08 15:16:04 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: testpat.c,v 1.4 2021/01/08 15:16:04 christos Exp $");
30
31 #include <sys/types.h>
32 #include <sys/time.h>
33
34 #include <curses.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <math.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42
43 static int colour_list[6] = {
44 COLOR_YELLOW,
45 COLOR_CYAN,
46 COLOR_GREEN,
47 COLOR_MAGENTA,
48 COLOR_RED,
49 COLOR_BLUE,
50 };
51 static int numcolours = (int)__arraycount(colour_list);
52
53 int main(int argc, char *argv[]) {
54 int i, col, colour, line, x_limit, y_limit, colourOK, spacing;
55 int xpos, ypos, spacing_residual, spacing_start, spacing_end;
56 int grid_x, grid_y, **circle_pos;
57 size_t ncpos;
58 float grid_unit;
59 const char *title = "NetBSD";
60 float coord_x, circle_int;
61 float a_axis, b_axis;
62
63 if (!initscr()) {
64 errx(EXIT_FAILURE, "Unknown terminal type");
65 }
66
67 curs_set(0);
68
69 if (argc > 2) {
70 endwin();
71 fprintf(stderr, "Usage: %s <title>", getprogname());
72 return EXIT_FAILURE;
73 }
74
75 if (argc == 2) {
76 title = argv[1];
77 if (strlen(title) >= (size_t)COLS) {
78 endwin();
79 errx(EXIT_FAILURE,
80 "Title string is longer than display cols");
81 }
82 }
83
84 colourOK = has_colors();
85
86 if (COLS < 13 || LINES < 13) {
87 endwin();
88 errx(EXIT_FAILURE, "Terminal size must be at least 72x25.");
89 }
90
91 if (colourOK) {
92 start_color();
93
94 init_pair(0, COLOR_WHITE, COLOR_BLACK);
95 init_pair(1, COLOR_WHITE, COLOR_RED);
96 init_pair(2, COLOR_WHITE, COLOR_GREEN);
97 init_pair(3, COLOR_WHITE, COLOR_YELLOW);
98 init_pair(4, COLOR_WHITE, COLOR_BLUE);
99 init_pair(5, COLOR_WHITE, COLOR_MAGENTA);
100 init_pair(6, COLOR_WHITE, COLOR_CYAN);
101 init_pair(7, COLOR_BLACK, COLOR_WHITE);
102
103 attrset(COLOR_PAIR(0));
104 }
105
106 x_limit = (COLS - 1) / 2;
107 x_limit = x_limit * 2;
108 y_limit = (LINES - 2) / 2;
109 y_limit = y_limit * 2;
110 spacing = 2 * y_limit / numcolours;
111 spacing_residual = ((2 * y_limit) % numcolours) / 2;
112 a_axis = y_limit / 2;
113 b_axis = y_limit;
114 grid_unit = b_axis / 13;
115 grid_y = grid_unit;
116 grid_x = grid_unit * 2;
117
118
119 ncpos = y_limit * sizeof(*circle_pos)
120 + y_limit * 2 * sizeof(**circle_pos);
121 circle_pos = malloc(ncpos);
122 if (circle_pos == NULL) {
123 endwin();
124 errx(EXIT_FAILURE, "Can't allocate circle positions");
125 }
126 for (i = 0; i < y_limit; i++) {
127 circle_pos[i] = (void *)&circle_pos[y_limit + i * 2];
128 circle_pos[i][0] = circle_pos[i][1] = -1;
129 }
130
131 for (i = 0; i < y_limit; i++) {
132 /* Draw an ellipse (looks more circular.) */
133 circle_int = (i - a_axis) / a_axis;
134 circle_int = 1 - powf(circle_int, 2);
135 circle_int = circle_int * powf(b_axis, 2);
136 #if 0
137 /* Draw a circle, commented out as elipse looks better.*/
138 circle_int = powf(a_axis, 2) - powf(i - a_axis, 2);
139 #endif
140 coord_x = sqrtf(circle_int);
141 circle_pos[i][0] = (-coord_x + ((float)x_limit / 2));
142 circle_pos[i][1] = (coord_x + ((float)x_limit / 2));
143 }
144
145 clear();
146
147 attron(A_ALTCHARSET);
148 move(0, 0);
149
150 /* Draw a grid. */
151 for (line = 1; line < y_limit; line += grid_y) {
152 for (col = 1; col < x_limit; col = col + grid_x) {
153 xpos = col;
154 while ((xpos < col + grid_x - 1) && (xpos <
155 x_limit)) {
156 mvaddch(line + grid_y - 1, xpos, 113 |
157 A_ALTCHARSET);
158 xpos++;
159 }
160 if (xpos < x_limit)
161 mvaddch(line + grid_y - 1, xpos, 110 |
162 A_ALTCHARSET);
163 }
164 ypos = line;
165 while (ypos < line + grid_y - 1) {
166 for (col = grid_x - 1; col < x_limit; col += grid_x) {
167 mvaddch(ypos, col + 1, 120 | A_ALTCHARSET);
168 }
169 ypos++;
170 }
171 }
172
173 for (line = 1; line < y_limit; line += grid_y) {
174 mvaddch(line + grid_y - 1, 0, 116 | A_ALTCHARSET);
175 mvaddch(line + grid_y - 1, x_limit, 117 | A_ALTCHARSET);
176
177 ypos = line;
178 while (ypos < line + grid_y - 1) {
179 mvaddch(ypos, 0, 120 | A_ALTCHARSET);
180 mvaddch(ypos, x_limit, 120 | A_ALTCHARSET);
181 ypos++;
182 }
183 }
184
185 for (col = 1; col < x_limit; col += grid_x) {
186 mvaddch(0, col + grid_x - 1, 119 | A_ALTCHARSET);
187 mvaddch(y_limit, col + grid_x - 1, 118 | A_ALTCHARSET);
188
189 xpos = col;
190 while ((xpos < col + grid_x - 1) && (xpos < x_limit)) {
191 mvaddch(0, xpos, 113 | A_ALTCHARSET);
192 mvaddch(y_limit, xpos, 113 | A_ALTCHARSET);
193 xpos++;
194 }
195 }
196
197 mvaddch(0, 0, 108 | A_ALTCHARSET);
198 mvaddch(0, x_limit, 107 | A_ALTCHARSET);
199 mvaddch(y_limit, 0, 109 | A_ALTCHARSET);
200 mvaddch(y_limit, x_limit, 106 | A_ALTCHARSET);
201
202 /* Draw a white circle. */
203 for (i = 1; i < y_limit; i++) {
204 for (col = circle_pos[i][0]; col <= circle_pos[i][1]; col++) {
205 mvaddch(i, col, 32 | A_REVERSE);
206 }
207 }
208
209 /* Add title segment. */
210 for (i = roundf(1 * grid_unit); i < roundf(2 * grid_unit); i++) {
211 if (colourOK)
212 attrset(COLOR_PAIR(COLOR_BLACK));
213
214 for (col = roundf((4 * grid_unit * 2) +
215 circle_pos[y_limit / 2][0]); col <= roundf((9 * grid_unit
216 * 2) + circle_pos[y_limit / 2][0]); col++)
217 mvaddch(i, col, ' ');
218 }
219
220 i = roundf(1.4 * grid_unit);
221
222 col = y_limit - (strlen(title) / 2) + circle_pos[y_limit / 2][0];
223 mvprintw(i, col, "%s", title);
224
225 /* Add black segments at top. */
226 for (line = roundf(2 * grid_unit); line < 4 * grid_unit; line++) {
227 if (colourOK)
228 attrset(COLOR_PAIR(COLOR_BLACK));
229
230 for (col = 0; col <= roundf((3.5 * grid_unit * 2)); col++) {
231 xpos = col + circle_pos[y_limit / 2][0];
232 if (xpos >= circle_pos[line][0] &&
233 xpos <= circle_pos[line][1])
234 mvaddch(line, xpos, ' ');
235 }
236
237 for (col = roundf((9.5 * grid_unit * 2)); col <
238 roundf((13 * grid_unit * 2)); col++) {
239 xpos = col + circle_pos[y_limit / 2][0];
240 if (xpos >= circle_pos[line][0] &&
241 xpos <= circle_pos[line][1])
242 mvaddch(line, xpos, ' ');
243 }
244 }
245
246 /* Add black and white squares close to top. */
247 int gap = (circle_pos[(int)(5 * grid_unit)][1] -
248 circle_pos[(int)(5 * grid_unit)][0]) / 13;
249
250 for (i = roundf(3 * grid_unit); i < roundf(4 * grid_unit); i++) {
251 for (xpos = 0; xpos <= x_limit; xpos += 2 * gap) {
252 if (colourOK)
253 attrset(COLOR_PAIR(COLOR_BLACK));
254
255 for (col = xpos; col < xpos + gap; col++) {
256 if (col >= circle_pos[i][0] &&
257 col <= circle_pos[i][1])
258 mvaddch(i, col, ' ');
259 }
260
261 if (colourOK)
262 attrset(COLOR_PAIR(COLOR_WHITE));
263
264 for (col = xpos + gap ; col < xpos + (2 * gap);
265 col++) {
266 if (col >= circle_pos[i][0] &&
267 col <= circle_pos[i][1])
268 mvaddch(i, col, ' ');
269 }
270 }
271 }
272
273 /* Add colour bars. */
274 for (i = 0; i < numcolours; i++) {
275 colour = colour_list[i];
276 if (colourOK)
277 attrset(COLOR_PAIR(colour));
278
279 if (i == 0)
280 spacing_start = 0;
281 else
282 spacing_start = (spacing * i) + spacing_residual;
283
284 if (i == numcolours - 1)
285 spacing_end = circle_pos[y_limit / 2][1];
286 else
287 spacing_end = (spacing * (i + 1)) + spacing_residual;
288
289 for (line = roundf(4 * grid_unit); line < (y_limit / 2);
290 line++) {
291 for (col = spacing_start; col < spacing_end; col++) {
292 xpos = col + circle_pos[y_limit / 2][0];
293 if (xpos >= circle_pos[line][0] &&
294 xpos <= circle_pos[line][1])
295 mvprintw(line, xpos, " ");
296 }
297 }
298 }
299
300 /* Add black segment under centre line. */
301 for (line = y_limit / 2; line < (9.5 * grid_unit); line++) {
302 if (colourOK)
303 attrset(COLOR_PAIR(COLOR_BLACK));
304
305 for (col = circle_pos[line][0]; col <= circle_pos[line][1];
306 col++)
307 mvaddch(line, col, ' ');
308
309 for (col = roundf((1.5 * grid_unit * 2)); col <
310 roundf((4.3 * grid_unit * 2)); col++) {
311 xpos = col + circle_pos[y_limit / 2][0];
312 if (xpos >= circle_pos[line][0] &&
313 xpos < circle_pos[line][1])
314 mvaddch(line, xpos, 120 | A_ALTCHARSET);
315 }
316
317 for (col = roundf((4.3 * grid_unit * 2)); col <
318 roundf((7.6 * grid_unit * 2)); col++) {
319 xpos = col + circle_pos[y_limit / 2][0];
320 if (xpos >= circle_pos[line][0] &&
321 xpos < circle_pos[line][1])
322 mvaddch(line, xpos, '|');
323 }
324
325 for (col = roundf((7.6 * grid_unit * 2)); col <
326 roundf((11.5 * grid_unit * 2)); col++) {
327 xpos = col + circle_pos[y_limit / 2][0];
328 if (xpos >= circle_pos[line][0] &&
329 xpos < circle_pos[line][1])
330 mvaddch(line, xpos, 97 | A_ALTCHARSET);
331 }
332 }
333
334 /* Add black segment close to bottom. */
335 for (line = roundf(9.5 * grid_unit); line <= (10.5 * grid_unit);
336 line++) {
337 if (colourOK)
338 attrset(COLOR_PAIR(COLOR_BLACK));
339
340 for (col = roundf((0 * grid_unit * 2)); col <
341 roundf((4 * grid_unit * 2)); col++) {
342 xpos = col + circle_pos[y_limit / 2][0];
343 if (xpos >= circle_pos[line][0] &&
344 xpos < circle_pos[line][1])
345 mvaddch(line, xpos, ' ');
346 }
347
348 for (col = roundf((4 * grid_unit * 2)); col <
349 roundf((6.5 * grid_unit * 2)); col++) {
350 xpos = col + circle_pos[y_limit / 2][0];
351 if (xpos >= circle_pos[line][0] &&
352 xpos < circle_pos[line][1])
353 mvaddch(line, xpos, 97 | A_ALTCHARSET);
354 }
355
356 if (colourOK)
357 attrset(COLOR_PAIR(COLOR_WHITE));
358
359 for (col = roundf((6.5 * grid_unit * 2)); col <
360 roundf((9 * grid_unit * 2)); col++) {
361 xpos = col + circle_pos[y_limit / 2][0];
362 if (xpos >= circle_pos[line][0] &&
363 xpos < circle_pos[line][1])
364 mvaddch(line, xpos, 97 | A_ALTCHARSET);
365 }
366
367 for (col = roundf((9 * grid_unit * 2)); col <
368 roundf((13 * grid_unit * 2)); col++) {
369 xpos = col + circle_pos[y_limit / 2][0];
370 if (xpos >= circle_pos[line][0] &&
371 xpos < circle_pos[line][1])
372 mvaddch(line, xpos, ' ');
373 }
374 }
375
376 /* Add name segment close to bottom. */
377 for (line = roundf(10.5 * grid_unit); line < (12 * grid_unit);
378 line++) {
379 if (colourOK)
380 attrset(COLOR_PAIR(COLOR_BLACK));
381
382 for (col = roundf(3.5 * grid_unit * 2); col <= roundf(9.5 *
383 grid_unit * 2); col++) {
384 xpos = col + circle_pos[y_limit / 2][0];
385 if (xpos >= circle_pos[line][0] &&
386 xpos < circle_pos[line][1])
387 mvaddch(line, xpos, ' ');
388 }
389
390 if (colourOK)
391 attrset(COLOR_PAIR(COLOR_WHITE));
392
393 for (col = roundf(0 * grid_unit * 2); col <= roundf(3.5 *
394 grid_unit * 2); col++) {
395 xpos = col + circle_pos[y_limit / 2][0];
396 if (xpos >= circle_pos[line][0] &&
397 xpos < circle_pos[line][1])
398 mvaddch(line, xpos, ' ');
399 }
400
401 for (col = roundf(9.5 * grid_unit * 2); col <= roundf(13 *
402 grid_unit * 2); col++) {
403 xpos = col + circle_pos[y_limit / 2][0];
404 if (xpos >= circle_pos[line][0] &&
405 xpos < circle_pos[line][1])
406 mvaddch(line, xpos, ' ');
407 }
408 }
409
410 /* Add yellow segment at bottom. */
411 for (line = 12 * grid_unit; line < y_limit; line++) {
412 if (colourOK)
413 attrset(COLOR_PAIR(COLOR_YELLOW));
414
415 for (col = circle_pos[line][0]; col <= circle_pos[line][1];
416 col++)
417 mvaddch(line, col, ' ');
418
419 if (colourOK)
420 attrset(COLOR_PAIR(COLOR_RED));
421
422 for (col = roundf((6 * grid_unit * 2)); col <
423 roundf((7 * grid_unit * 2)); col++) {
424 xpos = col + circle_pos[y_limit / 2][0];
425 if (xpos >= circle_pos[line][0] &&
426 xpos < circle_pos[line][1])
427 mvaddch(line, xpos, ' ');
428 }
429 }
430
431 if (colourOK)
432 attrset(COLOR_PAIR(COLOR_BLACK));
433
434 for (line = 6 * grid_unit; line <= (7 * grid_unit) + 1; line++) {
435 if (colourOK)
436 attrset(COLOR_PAIR(COLOR_BLACK));
437
438 col = x_limit / 2;
439 if (line != a_axis) {
440 mvaddch(line, col - 1, ' ');
441 mvaddch(line, col, 120 | A_ALTCHARSET);
442 mvaddch(line, col + 1, ' ');
443 }
444 }
445
446 line = y_limit / 2;
447 for (col = 1; col < x_limit; col = col + grid_x) {
448 xpos = col;
449 while (xpos < col + grid_x - 1) {
450 if (xpos >= circle_pos[line][0]
451 && xpos < circle_pos[line][1])
452 mvaddch(line, xpos, 113 | A_ALTCHARSET);
453 xpos++;
454 }
455 if (xpos >= circle_pos[line][0] && xpos < circle_pos[line][1])
456 mvaddch(line, xpos, 110 | A_ALTCHARSET);
457 }
458
459 line = y_limit / 2;
460 col = x_limit / 2;
461 mvaddch(line, col, 110 | A_ALTCHARSET);
462 mvaddch(line, col - 1, 113 | A_ALTCHARSET);
463 mvaddch(line, col + 1, 113 | A_ALTCHARSET);
464
465 refresh();
466
467 getch();
468
469 endwin();
470
471 return EXIT_SUCCESS;
472 }
473
474