Home | History | Annotate | Line # | Download | only in testpat
testpat.c revision 1.1
      1 /* $NetBSD: testpat.c,v 1.1 2021/01/02 03:21:39 nat 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 
     29 #include <curses.h>
     30 #include <err.h>
     31 #include <errno.h>
     32 #include <math.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <time.h>
     37 #include <sys/time.h>
     38 #include <sys/types.h>
     39 
     40 int main(int argc, char *argv[]) {
     41 	int i, col, colour, line, x_limit, y_limit, colourOK, spacing;
     42 	int xpos, ypos, spacing_residual, spacing_start, spacing_end;
     43 	int grid_x, grid_y;
     44 	float grid_unit;
     45 
     46 	char title[255] = "NetBSD";
     47 	int numcolours = 6;
     48 
     49 	int colour_list[6] = {
     50 		COLOR_YELLOW,
     51 		COLOR_CYAN,
     52 		COLOR_GREEN,
     53 		COLOR_MAGENTA,
     54 		COLOR_RED,
     55 		COLOR_BLUE,
     56 	};
     57 
     58 	float coord_x, circle_int;
     59 	float a_axis, b_axis;
     60 
     61 	if (!(initscr())) {
     62 		printf("\nUnknown terminal type.");
     63 		printf("\n");
     64 		return EXIT_FAILURE;
     65 	}
     66 
     67 	if (argc > 2) {
     68 		endwin();
     69 		errx(EINVAL, "usage: testpat [title]");
     70 	}
     71 
     72 	if (argc == 2 && strlen(argv[1]) < (size_t)COLS)
     73 		snprintf(title, sizeof(title), "%s", argv[1]);
     74 	else if (argc == 2 && (int)strlen(argv[1]) > COLS) {
     75 		endwin();
     76 		errx(EINVAL, "title string must be less than display columns");
     77 	}
     78 
     79 	colourOK = has_colors();
     80 
     81 	if (COLS < 13 || LINES < 13) {
     82 		endwin();
     83 		printf("\nTerminal size must be at least 72x25.");
     84 		printf("\n");
     85 		return EXIT_FAILURE;
     86 	}
     87 
     88 	if (colourOK) {
     89 		start_color();
     90 
     91 	    	init_pair( 0, COLOR_WHITE, COLOR_BLACK );
     92 	    	init_pair( 1, COLOR_WHITE, COLOR_RED );
     93 	    	init_pair( 2, COLOR_WHITE, COLOR_GREEN );
     94 	    	init_pair( 3, COLOR_WHITE, COLOR_YELLOW );
     95 	    	init_pair( 4, COLOR_WHITE, COLOR_BLUE );
     96 	    	init_pair( 5, COLOR_WHITE, COLOR_MAGENTA );
     97 	    	init_pair( 6, COLOR_WHITE, COLOR_CYAN );
     98 	    	init_pair( 7, COLOR_BLACK, COLOR_WHITE );
     99 
    100 		attrset(COLOR_PAIR(0));
    101 	}
    102 
    103 	x_limit = (COLS - 1) / 2;
    104 	x_limit = x_limit * 2;
    105 	y_limit = (LINES - 2) / 2;
    106 	y_limit = y_limit * 2;
    107 	spacing = 2 * y_limit / numcolours;
    108 	spacing_residual = ((2 * y_limit) % numcolours) / 2;
    109 	a_axis = y_limit / 2;
    110 	b_axis = y_limit;
    111 	grid_unit = b_axis / 13;
    112 	grid_y = grid_unit;
    113 	grid_x = grid_unit * 2;
    114 
    115 	int circle_pos[y_limit][2];
    116 	memset(circle_pos, -1, sizeof(circle_pos));
    117 
    118 	for (i = 0; i < y_limit; i++) {
    119 		/* Draw an elipse (looks more circular.) */
    120 		circle_int = (i - a_axis) / a_axis;
    121 		circle_int = 1 - powf(circle_int, 2);
    122 		circle_int = circle_int * powf(b_axis, 2);
    123 #if 0
    124 		/* Draw a circle, commented out as elipse looks better.*/
    125 		circle_int = powf(a_axis, 2) - powf(i - a_axis, 2);
    126 #endif
    127 		coord_x = sqrtf(circle_int);
    128 		circle_pos[i][0] = (-coord_x + ((float)x_limit / 2));
    129 		circle_pos[i][1] = (coord_x + ((float)x_limit / 2));
    130 	}
    131 
    132 	clear();
    133 
    134 	attron(A_ALTCHARSET);
    135 	move(0, 0);
    136 
    137 	/* Draw a grid. */
    138 	for (line = 1; line < y_limit; line += grid_y) {
    139 		for (col = 1; col < x_limit; col = col + grid_x) {
    140 			xpos = col;
    141 			while ((xpos < col + grid_x - 1) && (xpos <
    142 			    x_limit)) {
    143 				mvaddch(line + grid_y - 1, xpos, 113 |
    144 				    A_ALTCHARSET);
    145 				xpos++;
    146 			}
    147 			if (xpos < x_limit)
    148 				mvaddch(line + grid_y - 1, xpos, 110 |
    149 				    A_ALTCHARSET);
    150 		}
    151 		ypos = line;
    152 		while (ypos < line + grid_y - 1) {
    153 			for (col = grid_x - 1; col < x_limit; col += grid_x) {
    154 				mvaddch(ypos, col + 1, 120 | A_ALTCHARSET);
    155 			}
    156 			ypos++;
    157 		}
    158 	}
    159 
    160 	for (line = 1; line < y_limit; line += grid_y) {
    161 		mvaddch(line + grid_y - 1, 0, 116 | A_ALTCHARSET);
    162 		mvaddch(line + grid_y - 1, x_limit, 117 | A_ALTCHARSET);
    163 
    164 		ypos = line;
    165 		while (ypos < line + grid_y - 1) {
    166 			mvaddch(ypos, 0, 120 | A_ALTCHARSET);
    167 			mvaddch(ypos, x_limit, 120 | A_ALTCHARSET);
    168 			ypos++;
    169 		}
    170 	}
    171 
    172 	for (col = 1; col < x_limit; col += grid_x) {
    173 		mvaddch(0, col + grid_x - 1, 119 | A_ALTCHARSET);
    174 		mvaddch(y_limit, col + grid_x - 1, 118 | A_ALTCHARSET);
    175 
    176 		xpos = col;
    177 		while ((xpos < col + grid_x - 1) && (xpos < x_limit)) {
    178 			mvaddch(0, xpos, 113 | A_ALTCHARSET);
    179 			mvaddch(y_limit, xpos, 113 | A_ALTCHARSET);
    180 			xpos++;
    181 		}
    182 	}
    183 
    184 	mvaddch(0, 0, 108 | A_ALTCHARSET);
    185 	mvaddch(0, x_limit, 107 | A_ALTCHARSET);
    186 	mvaddch(y_limit, 0, 109 | A_ALTCHARSET);
    187 	mvaddch(y_limit, x_limit, 106 | A_ALTCHARSET);
    188 
    189 	/* Draw a white circle. */
    190 	for (i = 1; i < y_limit; i++) {
    191 		for (col = circle_pos[i][0]; col <= circle_pos[i][1]; col++) {
    192 			mvaddch(i, col, 32 | A_REVERSE);
    193 		}
    194 	}
    195 
    196 	/* Add title segment. */
    197 	for (i = roundf(1 * grid_unit); i < roundf(2 * grid_unit); i++) {
    198 		if (colourOK)
    199     			attrset(COLOR_PAIR(COLOR_BLACK));
    200 
    201 		for (col = roundf((4 * grid_unit * 2) +
    202 		    circle_pos[y_limit /2][0]); col <= roundf((9 * grid_unit
    203 		    * 2) + circle_pos[y_limit /2][0]); col++)
    204 			mvaddch(i, col, ' ');
    205 	}
    206 
    207 	i = roundf(1.4 * grid_unit);
    208 
    209 	col = y_limit - (strlen(title) / 2) + circle_pos[y_limit / 2][0];
    210 		mvprintw(i, col, "%s", title);
    211 
    212 	/* Add black segments at top. */
    213 	for (line = roundf(2 * grid_unit); line < 4 * grid_unit; line++) {
    214 		if (colourOK)
    215     			attrset(COLOR_PAIR(COLOR_BLACK));
    216 
    217 		for (col = 0; col <= roundf((3.5 * grid_unit * 2)); col++) {
    218 				xpos = col + circle_pos[y_limit / 2][0];
    219 				if (xpos >= circle_pos[line][0] &&
    220 				    xpos <= circle_pos[line][1])
    221 					mvaddch(line, xpos, ' ');
    222 		}
    223 
    224 		for (col = roundf((9.5 * grid_unit * 2)); col <
    225 		    roundf((13 * grid_unit * 2)); col++) {
    226 				xpos = col + circle_pos[y_limit / 2][0];
    227 				if (xpos >= circle_pos[line][0] &&
    228 				    xpos <= circle_pos[line][1])
    229 					mvaddch(line, xpos, ' ');
    230 		}
    231 	}
    232 
    233 	/* Add black and white squares close to top. */
    234 	int gap = (circle_pos[(int)(5 * grid_unit)][1] -
    235 	    circle_pos[(int)(5 * grid_unit)][0]) / 13;
    236 
    237 	for (i = roundf(3 * grid_unit); i < roundf(4 * grid_unit); i++) {
    238 		for (xpos = 0; xpos <= x_limit; xpos += 2 * gap) {
    239 			if (colourOK)
    240     				attrset(COLOR_PAIR(COLOR_BLACK));
    241 
    242 			for (col = xpos; col < xpos + gap; col++) {
    243 				if (col >= circle_pos[i][0] &&
    244 				    col <= circle_pos[i][1])
    245 					mvaddch(i, col, ' ');
    246 			}
    247 
    248 			if (colourOK)
    249     				attrset(COLOR_PAIR(COLOR_WHITE));
    250 
    251 			for (col = xpos + gap ; col < xpos + (2 * gap);
    252 			    col++) {
    253 				if (col >= circle_pos[i][0] &&
    254 				    col <= circle_pos[i][1])
    255 					mvaddch(i, col, ' ');
    256 			}
    257 		}
    258 	}
    259 
    260 	/* Add colour bars. */
    261 	for (i = 0; i < numcolours; i++) {
    262 		colour = colour_list[i];
    263 		if (colourOK)
    264 	    		attrset(COLOR_PAIR(colour));
    265 
    266 		if (i == 0)
    267 			spacing_start = 0;
    268 		else
    269 			spacing_start = (spacing * i) + spacing_residual;
    270 
    271 		if (i == numcolours - 1)
    272 			spacing_end = circle_pos[y_limit / 2][1];
    273 		else
    274 			spacing_end = (spacing * (i + 1)) + spacing_residual;
    275 
    276 	    	for (line = roundf(4 * grid_unit); line < (y_limit / 2);
    277 		    line++) {
    278 			for (col = spacing_start; col < spacing_end; col++) {
    279 				xpos = col + circle_pos[y_limit / 2][0];
    280 				if (xpos >= circle_pos[line][0] &&
    281 				    xpos <= circle_pos[line][1])
    282 	    				mvprintw(line, xpos, " ");
    283 			}
    284 	    	}
    285 	}
    286 
    287 	/* Add black segment under centre line. */
    288 	for (line = y_limit / 2; line < (9.5 * grid_unit); line++) {
    289 		if (colourOK)
    290     			attrset(COLOR_PAIR(COLOR_BLACK));
    291 
    292 		for (col = circle_pos[line][0]; col <= circle_pos[line][1];
    293 		    col++)
    294 			mvaddch(line, col, ' ');
    295 
    296 		for (col = roundf((1.5 * grid_unit * 2)); col <
    297 		    roundf((4.3 * grid_unit * 2)); col++) {
    298 				xpos = col + circle_pos[y_limit / 2][0];
    299 				if (xpos >= circle_pos[line][0] &&
    300 				    xpos < circle_pos[line][1])
    301 					mvaddch(line, xpos, 120 | A_ALTCHARSET);
    302 		}
    303 
    304 		for (col = roundf((4.3 * grid_unit * 2)); col <
    305 		    roundf((7.6 * grid_unit * 2)); col++) {
    306 				xpos = col + circle_pos[y_limit / 2][0];
    307 				if (xpos >= circle_pos[line][0] &&
    308 				    xpos < circle_pos[line][1])
    309 					mvaddch(line, xpos, '|');
    310 		}
    311 
    312 		for (col = roundf((7.6 * grid_unit * 2)); col <
    313 		    roundf((11.5 * grid_unit * 2)); col++) {
    314 				xpos = col + circle_pos[y_limit / 2][0];
    315 				if (xpos >= circle_pos[line][0] &&
    316 				    xpos < circle_pos[line][1])
    317 					mvaddch(line, xpos, 97 | A_ALTCHARSET);
    318 		}
    319 	}
    320 
    321 	/* Add black segment close to bottom. */
    322 	for (line = roundf(9.5 * grid_unit); line <= (10.5 * grid_unit);
    323 	    line++) {
    324 		if (colourOK)
    325     			attrset(COLOR_PAIR(COLOR_BLACK));
    326 
    327 		for (col = roundf((0 * grid_unit * 2)); col <
    328 		    roundf((4 * grid_unit * 2)); col++) {
    329 				xpos = col + circle_pos[y_limit / 2][0];
    330 				if (xpos >= circle_pos[line][0] &&
    331 				    xpos < circle_pos[line][1])
    332 					mvaddch(line, xpos, ' ');
    333 		}
    334 
    335 		for (col = roundf((4 * grid_unit * 2)); col <
    336 		    roundf((6.5 * grid_unit * 2)); col++) {
    337 				xpos = col + circle_pos[y_limit / 2][0];
    338 				if (xpos >= circle_pos[line][0] &&
    339 				    xpos < circle_pos[line][1])
    340 					mvaddch(line, xpos, 97 | A_ALTCHARSET);
    341 		}
    342 
    343 		if (colourOK)
    344     			attrset(COLOR_PAIR(COLOR_WHITE));
    345 
    346 		for (col = roundf((6.5 * grid_unit * 2)); col <
    347 		    roundf((9 * grid_unit * 2)); col++) {
    348 				xpos = col + circle_pos[y_limit / 2][0];
    349 				if (xpos >= circle_pos[line][0] &&
    350 				    xpos < circle_pos[line][1])
    351 					mvaddch(line, xpos, 97 | A_ALTCHARSET);
    352 		}
    353 
    354 		for (col = roundf((9 * grid_unit * 2)); col <
    355 		    roundf((13 * grid_unit * 2)); col++) {
    356 				xpos = col + circle_pos[y_limit / 2][0];
    357 				if (xpos >= circle_pos[line][0] &&
    358 				    xpos < circle_pos[line][1])
    359 					mvaddch(line, xpos, ' ');
    360 		}
    361 	}
    362 
    363 	/* Add name segment close to bottom. */
    364 	for (line = roundf(10.5 * grid_unit); line < (12 * grid_unit);
    365 	    line++) {
    366 		if (colourOK)
    367     			attrset(COLOR_PAIR(COLOR_BLACK));
    368 
    369 		for (col = roundf(3.5 * grid_unit * 2); col <= roundf(9.5 *
    370 		    grid_unit * 2); col++) {
    371 			xpos = col + circle_pos[y_limit / 2][0];
    372 			if (xpos >= circle_pos[line][0] &&
    373 			    xpos < circle_pos[line][1])
    374 				mvaddch(line, xpos, ' ');
    375 		}
    376 
    377 		if (colourOK)
    378     			attrset(COLOR_PAIR(COLOR_WHITE));
    379 
    380 		for (col = roundf(0 * grid_unit * 2); col <= roundf(3.5 *
    381 		    grid_unit * 2); col++) {
    382 			xpos = col + circle_pos[y_limit / 2][0];
    383 			if (xpos >= circle_pos[line][0] &&
    384 			    xpos < circle_pos[line][1])
    385 				mvaddch(line, xpos, ' ');
    386 		}
    387 
    388 		for (col = roundf(9.5 * grid_unit * 2); col <= roundf(13 *
    389 		    grid_unit * 2); col++) {
    390 			xpos = col + circle_pos[y_limit / 2][0];
    391 			if (xpos >= circle_pos[line][0] &&
    392 			    xpos < circle_pos[line][1])
    393 				mvaddch(line, xpos, ' ');
    394 		}
    395 	}
    396 
    397 	/* Add yellow segment at bottom. */
    398 	for (line = 12 * grid_unit; line < y_limit; line++) {
    399 		if (colourOK)
    400     			attrset(COLOR_PAIR(COLOR_YELLOW));
    401 
    402 		for (col = circle_pos[line][0]; col <= circle_pos[line][1];
    403 		    col++)
    404 			mvaddch(line, col, ' ');
    405 
    406 		if (colourOK)
    407     			attrset(COLOR_PAIR(COLOR_RED));
    408 
    409 		for (col = roundf((6 * grid_unit * 2)); col <
    410 		    roundf((7 * grid_unit * 2)); col++) {
    411 				xpos = col + circle_pos[y_limit / 2][0];
    412 				if (xpos >= circle_pos[line][0] &&
    413 				    xpos < circle_pos[line][1])
    414 					mvaddch(line, xpos, ' ');
    415 		}
    416 	}
    417 
    418 	if (colourOK)
    419     		attrset(COLOR_PAIR(COLOR_BLACK));
    420 
    421 	for (line = 6 * grid_unit; line <= (7 * grid_unit) + 1; line++) {
    422 		if (colourOK)
    423     			attrset(COLOR_PAIR(COLOR_BLACK));
    424 
    425 		col = x_limit / 2;
    426 		if (line != a_axis) {
    427 			mvaddch(line, col - 1, ' ');
    428 			mvaddch(line, col, 120 | A_ALTCHARSET);
    429 			mvaddch(line, col + 1, ' ');
    430 		}
    431 	}
    432 
    433 	line = y_limit / 2;
    434 	for (col = 1; col < x_limit; col = col + grid_x) {
    435 		xpos = col;
    436 		while (xpos < col + grid_x - 1) {
    437 			if (xpos >= circle_pos[line][0] && xpos < circle_pos[line][1])
    438 				mvaddch(line, xpos, 113 | A_ALTCHARSET);
    439 			xpos++;
    440 		}
    441 		if (xpos >= circle_pos[line][0] && xpos < circle_pos[line][1])
    442 			mvaddch(line, xpos, 110 | A_ALTCHARSET);
    443 	}
    444 
    445 	line = y_limit / 2;
    446 	col = x_limit / 2;
    447 	mvaddch(line, col, 110 | A_ALTCHARSET);
    448 	mvaddch(line, col - 1, 113 | A_ALTCHARSET);
    449 	mvaddch(line, col + 1, 113 | A_ALTCHARSET);
    450 
    451 	refresh();
    452 
    453 	getch();
    454 
    455 	endwin();
    456 
    457 	return EXIT_SUCCESS;
    458 }
    459 
    460