|
| 1 | +/* |
| 2 | + * Mandelbrot for UNIX. |
| 3 | + * Copyright 2025, Andrew C. Young <[email protected]> |
| 4 | + * License: MIT |
| 5 | + * |
| 6 | + * This program was written on a Macintosh Quadra 700 running A/UX 3.1. |
| 7 | + * A/UX was Apple's first version of UNIX, based on both SYSV and BSD. |
| 8 | + * A/UX 3.1 was released in 1994, and its default C compiler did not |
| 9 | + * yet support the ANSI standard. As such, this program is written to |
| 10 | + * work with earlier K&R versions of C. It should also compile with GCC. |
| 11 | + * |
| 12 | + * It uses the curses library for screen manipulation, but it should |
| 13 | + * also work with the later ncurses library. |
| 14 | + * |
| 15 | + * My classic Mac resources can be found at m68k.club (www,gopher). |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdlib.h> |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +/* Used to write to the screen */ |
| 22 | +#include <curses.h> |
| 23 | + |
| 24 | +/* Used to measure run time */ |
| 25 | +#include <sys/types.h> |
| 26 | +#include <sys/times.h> |
| 27 | + |
| 28 | +#define MAX_ITERATIONS 16 |
| 29 | + |
| 30 | +int *pixels = 0; |
| 31 | +int cols = 80; |
| 32 | +int lines = 24; |
| 33 | + |
| 34 | +char symbols[] = {' ','-','+','=','\\','|','/','*', |
| 35 | + '#','3','8','B','=','#','+','.'}; |
| 36 | + |
| 37 | +char *header = "[ Mandelbrot by Andrew C. Young ]"; |
| 38 | +char *footer = "[ Press Any Key to Exit ]"; |
| 39 | + |
| 40 | +WINDOW *win = 0; |
| 41 | + |
| 42 | +void init() |
| 43 | +{ |
| 44 | + initscr(); |
| 45 | + nonl(); |
| 46 | + cbreak(); |
| 47 | + noecho(); |
| 48 | + |
| 49 | + /* Draw Box */ |
| 50 | + wmove(stdscr,0,0); |
| 51 | + wclear(stdscr); |
| 52 | + box(stdscr,'|','-'); |
| 53 | + wmove(stdscr,0,0); |
| 54 | + waddch(stdscr, '+'); |
| 55 | + wmove(stdscr,0,COLS-1); |
| 56 | + waddch(stdscr, '+'); |
| 57 | + wmove(stdscr,LINES-1,0); |
| 58 | + waddch(stdscr, '+'); |
| 59 | + wmove(stdscr,LINES-1,COLS-1); |
| 60 | + waddch(stdscr, '+'); |
| 61 | + wmove(stdscr,0,( (COLS/2) - (strlen(header)/2) -1 ) ); |
| 62 | + waddstr(stdscr, header); |
| 63 | + wrefresh(stdscr); |
| 64 | + |
| 65 | + lines = LINES - 2; |
| 66 | + cols = COLS - 2; |
| 67 | + win = newwin(lines,cols,1,1); |
| 68 | + pixels = (int*)malloc(cols*lines*sizeof(int)); |
| 69 | +} |
| 70 | + |
| 71 | +void cleanup() |
| 72 | +{ |
| 73 | + if (pixels != 0) |
| 74 | + { |
| 75 | + free(pixels); |
| 76 | + } |
| 77 | + endwin(); |
| 78 | +} |
| 79 | + |
| 80 | +void calculate() |
| 81 | +{ |
| 82 | + int i, col, row, iteration; |
| 83 | + double width, height, x0, y0, x, y, x_scale, y_scale, xtemp; |
| 84 | + |
| 85 | + width = (double)cols; |
| 86 | + height = (double)lines; |
| 87 | + |
| 88 | + i = 0; |
| 89 | + |
| 90 | + for (row = 0; row < lines; row++) |
| 91 | + { |
| 92 | + y0 = (row * 2.1 / height) - 1; |
| 93 | + for (col = 0; col < cols; col++) |
| 94 | + { |
| 95 | + x0 = (col * 3.5 / width) - 2.5; |
| 96 | + x = 0.0; |
| 97 | + y = 0.0; |
| 98 | + iteration = 0; |
| 99 | + while ( ((x*x) + (y*y) <= 4) && (iteration < MAX_ITERATIONS)) |
| 100 | + { |
| 101 | + xtemp = (x*x) - (y*y) + x0; |
| 102 | + y = (2*x*y) + y0; |
| 103 | + x = xtemp; |
| 104 | + iteration++; |
| 105 | + } |
| 106 | + |
| 107 | + /* printf("%X",iteration); */ |
| 108 | + pixels[i] = iteration; |
| 109 | + i++; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void display() |
| 115 | +{ |
| 116 | + int i, col, row; |
| 117 | + int value; |
| 118 | + int color; |
| 119 | + char symbol; |
| 120 | + |
| 121 | + /* Clear the screen */ |
| 122 | + wmove(win,0,0); |
| 123 | + wclear(win); |
| 124 | + |
| 125 | + i = 0; |
| 126 | + |
| 127 | + for (row = 0; row < lines; row++) |
| 128 | + { |
| 129 | + wmove(win,row,0); |
| 130 | + for (col = 0; col < cols; col++) |
| 131 | + { |
| 132 | + value = pixels[i]; |
| 133 | + value = value % 16; |
| 134 | + symbol = symbols[value]; |
| 135 | + waddch(win,symbol); |
| 136 | + i++; |
| 137 | + } |
| 138 | + } |
| 139 | + wrefresh(win); |
| 140 | +} |
| 141 | + |
| 142 | +void pause() |
| 143 | +{ |
| 144 | + wmove(stdscr,LINES-1,( (COLS/2) - (strlen(footer)/2) -1 ) ); |
| 145 | + waddstr(stdscr, footer); |
| 146 | + wmove(stdscr,LINES-1,COLS-1); |
| 147 | + wrefresh(stdscr); |
| 148 | + getchar(); |
| 149 | +} |
| 150 | + |
| 151 | +double time_in_seconds() |
| 152 | +{ |
| 153 | + struct tms buffer; |
| 154 | + clock_t t = times(&buffer); |
| 155 | + return t / 60.0; |
| 156 | +} |
| 157 | + |
| 158 | +void main() |
| 159 | +{ |
| 160 | + double start, after_calc, after_display, calc_time, display_time; |
| 161 | + |
| 162 | + init(); |
| 163 | + start = time_in_seconds(); |
| 164 | + calculate(); |
| 165 | + after_calc = time_in_seconds(); |
| 166 | + display(); |
| 167 | + after_display = time_in_seconds(); |
| 168 | + pause(); |
| 169 | + cleanup(); |
| 170 | + |
| 171 | + calc_time = (after_calc - start); |
| 172 | + display_time = (after_display - after_calc); |
| 173 | + |
| 174 | + printf("\nCalculation Time: %0.3f secs, Display Time: %0.3f secs, Total Time: %0.3f secs\n", calc_time, display_time, calc_time + display_time); |
| 175 | + |
| 176 | + exit(0); |
| 177 | +} |
0 commit comments