-
Notifications
You must be signed in to change notification settings - Fork 4
/
graphics_utils.h
362 lines (342 loc) · 9.47 KB
/
graphics_utils.h
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#define MAX_X 640
#define MAX_Y 480
#define SCALE 10
#define AXIS_COLOR GREEN
#define COLOR BLUE
#define PI 3.14159265
const int TOP = 8;
const int BOTTOM = 4;
const int RIGHT = 2;
const int LEFT = 1;
void grid();
void initialiseGraph(){
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
}
void grid(){
int x,y;
setbkcolor(WHITE);
setcolor(BLACK);
for(x=0; x<MAX_X; x+=SCALE) {
if(x==MAX_X/2){
setcolor(AXIS_COLOR);
setlinestyle(0, 0, 2);
line(x,0,x,MAX_Y);
setcolor(BLACK);
setlinestyle(0, 0, 1);
continue;
}
line(x,0,x,MAX_Y);
}
for(y=0; y<MAX_Y; y+=SCALE) {
if(y==MAX_Y/2){
setcolor(AXIS_COLOR);
setlinestyle(0, 0, 2);
line(0,y,MAX_X,y);
setcolor(BLACK);
setlinestyle(0, 0, 1);
continue;
}
line(0,y,MAX_X,y);
}
}
// Pixel drawing
int getPixelColor(int x, int y){
int X = x*SCALE + MAX_X/2;
int Y = -y*SCALE + MAX_Y/2;
return getpixel(X + SCALE/2, Y - SCALE/2);
}
void drawPixel(int x, int y){
int color = COLOR;
int x_range = MAX_X / SCALE;
int y_range = MAX_Y / SCALE;
int X = x*SCALE + MAX_X/2;
int Y = -y*SCALE + MAX_Y/2;
setcolor(color);
rectangle(X+1,Y-1,X+SCALE-1,Y-SCALE+1);
floodfill(X+2,Y-2, color);
setcolor(BLACK);
}
void drawColorPixel(int x, int y, int color){
int x_range = MAX_X / SCALE;
int y_range = MAX_Y / SCALE;
int X = x*SCALE + MAX_X/2;
int Y = -y*SCALE + MAX_Y/2;
setcolor(color);
rectangle(X+1,Y-1,X+SCALE-1,Y-SCALE+1);
floodfill(X+2,Y-2, color);
setcolor(BLACK);
}
// Line Drawing
void dda(int x1, int y1, int x2, int y2){
if(x1==x2 && y1==y2) {
drawPixel(x1,y1);
} else if(x1==x2) {
int i;
int ymin = y1<y2?y1:y2;
int ymax = y1<y2?y2:y1;
for(i=ymin;i<=ymax;i++) {
drawPixel(x1, i);
}
} else if (y1==y2) {
int i;
int xmin = x1<x2?x1:x2;
int xmax = x1<x2?x2:x1;
for(i=xmin;i<=xmax;i++) {
drawPixel(i, y1);
}
} else {
float dx, dy, len;
if(abs(x1-x2)>abs(y1-y2)) {
len = abs(x1-x2);
} else {
len = abs(y1-y2);
}
dx = (float)(x2-x1)/len;
dy = (float)(y2-y1)/len;
float x = x1;
float y = y1;
int i=0;
while (i<=len) {
drawPixel((int)x,(int)y);
x += dx;
y += dy;
i++;
}
}
}
void bresenham(int x1, int y1, int x2, int y2){
int x, y, dx, dy, s1, s2, interchange, e;
x = x1;
y = y1;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
// Initialze signs
s1 = x1==x2?0:(x2>x1?1:-1);
s2 = y1==y2?0:(y2>y1?1:-1);
// Set interchange flag
if(dy > dx ){
interchange = 1;
int temp = dx;
dx = dy;
dy = temp;
}else{
interchange=0;
}
e = 2 * dy - dx;
int i;
for(i=1;i<=dx;i++){
drawPixel(x,y);
while(e>0){
if(interchange){
x = x + s1;
}else{
y = y + s2;
}
e = e - 2*dx;
}
if(interchange){
y = y + s2;
}else{
x = x + s1;
}
e = e + 2*dy;
}
}
void bresenhamColor(int x1, int y1, int x2, int y2, int color){
int x, y, dx, dy, s1, s2, interchange, e;
x = x1;
y = y1;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
// Initialze signs
s1 = x1==x2?0:(x2>x1?1:-1);
s2 = y1==y2?0:(y2>y1?1:-1);
// Set interchange flag
if(dy > dx ){
interchange = 1;
int temp = dx;
dx = dy;
dy = temp;
}else{
interchange=0;
}
e = 2 * dy - dx;
int i;
for(i=1;i<=dx;i++){
drawColorPixel(x,y,color);
while(e>0){
if(interchange){
x = x + s1;
}else{
y = y + s2;
}
e = e - 2*dx;
}
if(interchange){
y = y + s2;
}else{
x = x + s1;
}
e = e + 2*dy;
}
}
// Polygon Drawing
void drawPoly(int* points, int n){
int i;
for(i=0;i<n-1;i++){
bresenham(points[2*i], points[2*i+1], points[2*i+2], points[2*i+3]);
}
bresenham(points[2*n-2], points[2*n-1], points[0], points[1]);
}
void drawColorPoly(int* points, int n, int color){
int i;
for(i=0;i<n-1;i++){
bresenhamColor(points[2*i], points[2*i+1], points[2*i+2], points[2*i+3], color);
}
bresenhamColor(points[2*n-2], points[2*n-1], points[0], points[1], color);
}
// Line Clipping
int computeCode(int x, int y, int* clipWindow){
int x_min = clipWindow[0];
int x_max = clipWindow[1];
int y_min = clipWindow[2];
int y_max = clipWindow[3];
int code = 0;
if(x<x_min)
code = code | LEFT;
else if (x>x_max)
code = code | RIGHT;
if (y<y_min)
code = code | TOP;
else if (y>y_max)
code = code | BOTTOM;
return code;
}
void suderland_cohen(int x1, int y1, int x2, int y2, int* clipWindow){
int code1 = computeCode(x1, y1, clipWindow);
int code2 = computeCode(x2, y2, clipWindow);
int x_min = clipWindow[0];
int x_max = clipWindow[1];
int y_min = clipWindow[2];
int y_max = clipWindow[3];
if(code1==0 & code2==0) {
bresenham(x1, y1, x2, y2);
} else if(code1 & code2) {
bresenhamColor(x1, y1, x2, y2, YELLOW);
} else {
int code_out;
double x, y;
// At least one endpoint is outside the
// rectangle, pick it.
if (code1 != 0)
code_out = code1;
else
code_out = code2;
// Find intersection point;
// using formulas y = y1 + slope * (x - x1),
// x = x1 + (1 / slope) * (y - y1)
if (code_out & TOP)
{
// point is above the clip rectangle
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM)
{
// point is below the rectangle
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT)
{
// point is to the right of rectangle
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT)
{
// point is to the left of rectangle
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
// Now intersection point x,y is found
// We replace point outside rectangle
// by intersection point
if (code_out == code1)
{
x1 = x;
y1 = y;
suderland_cohen(x1, y1, x2, y2, clipWindow);
}
else
{
x2 = x;
y2 = y;
suderland_cohen(x1, y1, x2, y2, clipWindow);
}
}
}
void midpoint_clip(int x1, int y1, int x2, int y2, int* clipWindow){
int code1 = computeCode(x1, y1, clipWindow);
int code2 = computeCode(x2, y2, clipWindow);
int x_min = clipWindow[0];
int x_max = clipWindow[1];
int y_min = clipWindow[2];
int y_max = clipWindow[3];
if(abs(x1-x2)<=1 && abs(y1-y2)<=1){
return;
}
if(code1==0 & code2==0) {
bresenham(x1, y1, x2, y2);
// delay(4000);
} else if(code1 & code2) {
bresenhamColor(x1, y1, x2, y2, YELLOW);
// delay(4000);
} else {
int m_x = (x2+x1)>>1;
int m_y = (y2+y1)>>1;
midpoint_clip(x1, y1, m_x, m_y, clipWindow);
midpoint_clip(m_x, m_y, x2, y2, clipWindow);
}
}
//2D Transformation
void rotate(int* points, int n, double theta){
int i;
for(i=0;i<n;i++){
double x = points[2*i+0];
double y = points[2*i+1];
points[2*i+0] = (int)(x*cos(theta)-y*sin(theta));
points[2*i+1] = (int)(x*sin(theta)+y*cos(theta));
}
}
void translate(int* points, int n, int shift_x, int shift_y){
int i;
for(i=0;i<n;i++){
double x = points[2*i+0];
double y = points[2*i+1];
points[2*i+0] = (int)(x+shift_x);
points[2*i+1] = (int)(y+shift_y);
}
}
void scale(int* points, int n, double scale_x, double scale_y){
int i;
for(i=0;i<n;i++){
double x = points[2*i+0];
double y = points[2*i+1];
points[2*i+0] = (int)(x*scale_x);
points[2*i+1] = (int)(y+scale_y);
}
}
void shear(int* points, int n, double shear_x, double shear_y){
int i;
for(i=0;i<n;i++){
double x = points[2*i+0];
double y = points[2*i+1];
points[2*i+0] = (int)(x + y*shear_x);
points[2*i+1] = (int)(y + x*shear_y);
}
}