-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
283 lines (258 loc) · 9.77 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <math.h>
#include "display.h"
///=============================================================================
///==== Creates empty display with specified size ==============================
///=============================================================================
void makeEmptyDisplay(Display d){
int line = 0;
while(line < d.size.lines){
int column = 0;
while (column < d.size.columns){
pushCharToPoint(' ', line, column, d, defaultColor);
column++;
}
line++;
}
}
///=============================================================================
///==== Pushes char to specified point in display's array ======================
///=============================================================================
void pushCharToPoint(char c, int ln, int col, Display d, Color color){
if(ln < d.size.lines && col < d.size.columns && ln >= 0 && col >= 0){
d.array[ln][col].c = c;
d.array[ln][col].color = color;
}
}
///=============================================================================
///==== Creates lines and columns of specified length ==========================
///=============================================================================
void createLine(char c, Display d, int ln, int beg, int end, Color color){
for(int i = beg; i < end; i++){
pushCharToPoint(c, ln, i, d, color);
}
}
void createColumn(char c, Display d, int col, int beg, int end, Color color){
for(int i = beg; i < end; i++){
pushCharToPoint(c, i, col, d, color);
}
}
///=============================================================================
///==== Creates lines and columns from strings =================================
///=============================================================================
void createLineText(char* c, Display d, int ln, int beg, Color color){
for(int i = 0; i < d.size.columns-1; i++){
if(c[i] == '\0'){
break;
}
pushCharToPoint(c[i], ln, i+beg, d, color);
}
}
void createColumnText(char* c, Display d, int col, int beg, Color color){
for(int i = 0; i < d.size.lines-1; i++){
if(c[i] == '\0'){
break;
}
pushCharToPoint(c[i], i+beg, col, d, color);
}
}
///=============================================================================
///==== Calculates and creates diagonals from start and end points =============
///=============================================================================
void createDiagonal(char c, Display d, int xBeg, int yBeg, int xEnd, int yEnd, Color color){
float xB = (float) xBeg;
float yB = (float) yBeg;
float xE = (float) xEnd;
float yE = (float) yEnd;
for(int x = xBeg; x <= xEnd; x++){
float tga = (yE-yB)/(xE - xB);
int y = floor(tga*((float)x) + (yE - tga*xE));
pushCharToPoint(c, y, x, d, color);
}
}
///=============================================================================
///==== Creates 2D boxes =======================================================
///=============================================================================
void createBox(char c, Display d, int xBeg, int yBeg, int xEnd, int yEnd, Color color){
if (xBeg == xEnd && yBeg == yEnd) { pushCharToPoint(c, yBeg, xBeg, d, color); }
else if (xBeg == xEnd) { createColumn(c, d, xBeg, yBeg, yEnd+1, color); }
else if (yBeg == yEnd) { createLine(c, d, yBeg, xBeg, xEnd, color); }
else {
createColumn(c, d, xBeg, yBeg, yEnd, color);
createColumn(c, d, xEnd, yBeg, yEnd, color);
createLine(c, d, yBeg, xBeg, xEnd+1, color);
createLine(c, d, yEnd, xBeg, xEnd+1, color);
}
}
///=============================================================================
///==== Creates 2D circle ======================================================
///=============================================================================
int reversePythagorean(int x, int r){
double y = sqrt(pow(r,2)-pow(x,2));
return ((y)>=0?(int)((y)+0.5):(int)((y)-0.5));
}
void createCircle(char c, Display d, int xBeg, int yBeg, int radius, Color color){
for(int x = 0; x <= radius; x++){
int y = reversePythagorean(x,radius);
pushCharToPoint(c, yBeg + y, xBeg + x, d, color);
pushCharToPoint(c, yBeg + y, xBeg - x, d, color);
pushCharToPoint(c, yBeg - y, xBeg + x, d, color);
pushCharToPoint(c, yBeg - y, xBeg - x, d, color);
}
for(int y = 0; y <= radius; y++){
int x = reversePythagorean(y,radius);
pushCharToPoint(c, yBeg + y, xBeg + x, d, color);
pushCharToPoint(c, yBeg + y, xBeg - x, d, color);
pushCharToPoint(c, yBeg - y, xBeg + x, d, color);
pushCharToPoint(c, yBeg - y, xBeg - x, d, color);
}
}
void createWheel(char c, Display d, int xBeg, int yBeg, int radius, Color color){
for(int x = 0; x <= radius; x++){
int y = reversePythagorean(x,radius);
createColumn(c, d, xBeg + x, yBeg - y, yBeg + y, color);
createColumn(c, d, xBeg - x, yBeg - y, yBeg + y, color);
}
}
void createSemiWheelDn(char c, Display d, int xBeg, int yBeg, int radius, Color color){
for(int x = 0; x <= radius; x++){
int y = reversePythagorean(x,radius);
createColumn(c, d, xBeg + x, yBeg , yBeg + y, color);
createColumn(c, d, xBeg - x, yBeg , yBeg + y, color);
}
}
void createSemiWheelUp(char c, Display d, int xBeg, int yBeg, int radius, Color color){
for(int x = 0; x <= radius; x++){
int y = reversePythagorean(x,radius);
createColumn(c, d, xBeg + x, yBeg - y, yBeg, color);
createColumn(c, d, xBeg - x, yBeg - y, yBeg, color);
}
}
///=============================================================================
///==== Creates a frame ========================================================
///=============================================================================
void createFrameDeprecated(char c, Display d, Color color){
int line = 0;
while(line < d.size.lines){
int column = 0;
while (column < d.size.columns){
if (column == 0 || column == d.size.columns - 1 ||
line == 0 || line == d.size.lines - 1) {
pushCharToPoint(c, line, column, d,color);
} else {
pushCharToPoint(' ', line, column, d, color);
}
column++;
}
line++;
}
}
void createFrame(char c, Display d, Color color){
createBox(c, d, 0, 0, d.size.columns-1, d.size.lines-1, color);
}
///=============================================================================
///==== Builds (prints out) display ============================================
///=============================================================================
void buildDisplay(Display d, int isColorMode){
if (isColorMode) {
buildColorDisplay(d);
} else {
buildMonochromeDisplay(d);
}
}
void buildMonochromeDisplay(Display d){
int line = 0;
char str[d.size.columns*d.size.lines+1];
while(line < d.size.lines){
int column = 0;
while (column < d.size.columns){
char c = d.array[line][column].c;
// Fix for non-readable ASCII characters
if (c < 32){
c = ' ';
}
str[d.size.columns*line + column] = c;
column++;
}
line++;
}
str[d.size.columns*d.size.lines] = '\0';
printf("%s", str);
for (int i = 0; i < d.size.columns*d.size.lines-1; i++) {
printf("\b");
}
}
void buildColorDisplay(Display d){
int line = 0;
Color previousColor = defaultColor;
while(line < d.size.lines){
int column = 0;
while (column < d.size.columns){
Point p = d.array[line][column];
// Fix for non-readable ASCII characters
if (p.c < 32){
p.c = ' ';
}
if (previousColor == p.color){
printf("%c", p.c);
} else {
if (p.c != ' ') {
setColor(p.color);
previousColor = p.color;
}
printf("%c", p.c);
}
column++;
}
line++;
}
resetColor();
for (int i = 0; i < d.size.columns*d.size.lines-1; i++) {
printf("\b");
}
}
///=============================================================================
///==== Management of 2D Array & Display =======================================
///=============================================================================
Display initializeDisplay(){
Size size = getTerminalSize();
Point** pointsArray = initializeArray(size.columns, size.lines);
Display display = {size, pointsArray};
makeEmptyDisplay(display);
return display;
}
Point ** initializeArray(int m, int n){
Point* values = calloc(m*n, sizeof(Point));
Point** rows = malloc(n*sizeof(Point*));
for (int i=0; i<n; ++i){
rows[i] = values + i*m;
}
return rows;
}
void destroyDisplay(Display d){
free(*d.array);
free(d.array);
}
///=============================================================================
///==== Gets the current terminal size properties ==============================
///=============================================================================
Size getTerminalSize(void){
#ifdef TIOCGSIZE
struct ttysize ts;
ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
int cols = ts.ts_cols;
int lines = ts.ts_lines;
Size r = {lines, cols};
return r;
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
int columns = ts.ws_col;
int lines = ts.ws_row;
Size r = { columns, lines};
return r;
#endif
}