Last active
April 21, 2019 21:44
-
-
Save pncnmnp/999c6e9abe1de7a5bb8c0b4b1241cb91 to your computer and use it in GitHub Desktop.
"computer graphics" lab's code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
#include<math.h> | |
// with math module don't forget to use '-lm' | |
// For a triangle | |
int trans_sx = 30; | |
int trans_sy = 30; | |
int scaling_sx = 2; | |
int scaling_sy = 1; | |
int rotate = 90; | |
void rotation(int x0, int y0, int x1, int y1, int x2, int y2) { | |
double t = 3.141592653589*rotate/180; | |
x0 = abs(x0*cos(t)-y0*sin(t)); | |
y0 = abs(x0*sin(t)+y0*cos(t)); | |
x1 = abs(x1*cos(t)-y1*sin(t)); | |
y1 = abs(x1*sin(t)+y1*cos(t)); | |
x2 = abs(x2*cos(t)-y2*sin(t)); | |
y2 = abs(x2*sin(t)+y2*cos(t)); | |
setcolor(BLUE); | |
line(x0, y0, x1, y1); | |
line(x0, y0, x2, y2); | |
line(x1, y1, x2, y2); | |
delay(1000); | |
} | |
void scaling(int x0, int y0, int x1, int y1, int x2, int y2) { | |
x0 *= scaling_sx; | |
x1 *= scaling_sx; | |
x2 *= scaling_sx; | |
y0 *= scaling_sy; | |
y1 *= scaling_sy; | |
y2 *= scaling_sy; | |
setcolor(YELLOW); | |
line(x0, y0, x1, y1); | |
line(x0, y0, x2, y2); | |
line(x1, y1, x2, y2); | |
delay(1000); | |
} | |
void translation(int x0, int y0, int x1, int y1, int x2, int y2) { | |
x0 += trans_sx; | |
x1 += trans_sx; | |
x2 += trans_sx; | |
y0 += trans_sy; | |
y1 += trans_sy; | |
y2 += trans_sy; | |
setcolor(RED); | |
line(x0, y0, x1, y1); | |
line(x0, y0, x2, y2); | |
line(x1, y1, x2, y2); | |
delay(1000); | |
} | |
void draw_original(int x0, int y0, int x1, int y1, int x2, int y2) { | |
line(x0, y0, x1, y1); | |
line(x0, y0, x2, y2); | |
line(x1, y1, x2, y2); | |
} | |
int main(void) { | |
int x0 = 100, y0 = 100, | |
x1 = 200, y1 = 100, | |
x2 = 100, y2 = 200; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
draw_original(x0, y0, x1, y1, x2, y2); | |
// translation(x0, y0, x1, y1, x2, y2); | |
// scaling(x0, y0, x1, y1, x2, y2); | |
rotation(x0, y0, x1, y1, x2, y2); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
void boundary_fill(int x, int y, int f_col, int b_col) { | |
if(getpixel(x, y) != b_col && getpixel(x, y) != f_col) { | |
putpixel(x, y, f_col); | |
delay(1); | |
boundary_fill(x + 1, y, f_col, b_col); | |
boundary_fill(x, y + 1, f_col, b_col); | |
boundary_fill(x - 1, y, f_col, b_col); | |
boundary_fill(x, y - 1, f_col, b_col); | |
} | |
} | |
int main(void) { | |
int x = 200, y = 200, r = 30; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
circle(x, y, r); | |
boundary_fill(x, y, 4, 15); | |
delay(1000); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
void bresen_circle(int x, int y, int r, int xc, int yc) { | |
int S = 3 - 2*r; | |
int X = xc; | |
int Y = yc; | |
while(x < y) { | |
if (S > 0) { | |
x += 1; | |
y -= 1; | |
S += 4*(x - y) + 10; | |
} | |
else if (S < 0) { | |
x += 1; | |
y = y; | |
S += 4*x + 6; | |
} | |
putpixel(X+x, Y+y, RED); | |
putpixel(Y+y, X+x, RED); | |
putpixel(X+x, Y-y, RED); | |
putpixel(X-x, Y+y, RED); | |
putpixel(X-x, Y-y, RED); | |
putpixel(Y-y, X-x, RED); | |
putpixel(Y-y, X+x, RED); | |
putpixel(Y+y, X-x, RED); | |
delay(10); | |
} | |
delay(1000); | |
} | |
int main(void) { | |
int x = 0, y = 0, r = 50, xc = 250, yc = 250; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
bresen_circle(x, y+r, r, xc, yc); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
int abs(int n) { | |
return (n > 0 ? n : -1*n); | |
} | |
void bresenham(int x0, int y0, int x1, int y1) { | |
int dx = (x1 - x0); | |
int dy = (y1 - y0); | |
float g = 2*dy - dx; | |
float x = x0; | |
float y = y0; | |
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy); | |
float xi = dx / (float) steps; | |
float yi = dy / (float) steps; | |
while(x < x1) { | |
putpixel(x, y, RED); | |
delay(10); | |
if(g >= 0) { | |
y += yi; | |
g += 2*(dy - dx); | |
} | |
else if(g < 0) { | |
g += 2*(dy); | |
} | |
x += xi; | |
} | |
} | |
int main(void) { | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
int x0 = 100, y0 = 100, x1 = 300, y1 = 400; | |
rectangle(x0, y0, x1, y1); | |
bresenham(x0, y0, x1, y1); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
int INSIDE = 0; | |
int LEFT = 1; | |
int RIGHT = 2; | |
int BOTTOM = 4; | |
int TOP = 8; | |
int x_max = 300; | |
int x_min = 100; | |
int y_max = 300; | |
int y_min = 100; | |
int compute_code(int x, int y) { | |
int code = INSIDE; | |
if (x < x_min) | |
code |= LEFT; | |
else if (x > x_max) | |
code |= RIGHT; | |
if (y < y_min) | |
code |= BOTTOM; | |
else if (y > y_max) | |
code |= TOP; | |
return code; | |
} | |
void cohen_sutherland(int x0, int y0, int x1, int y1) { | |
int x0_ini = x0, y0_ini = y0, x1_ini = x1, y1_ini = y1; | |
int code1 = compute_code(x0, y0); | |
int code2 = compute_code(x1, y1); | |
int code_out; | |
int x, y; | |
int done = 0; | |
while(1) { | |
if ((code1 | code2) == 0 ) { | |
done = 1; | |
break; | |
} | |
else if ((code1 & code2) != 0) { | |
break; | |
} | |
else { | |
if (code1 != 0) | |
code_out = code1; | |
else | |
code_out = code2; | |
if (code_out & TOP) { | |
x = x0 + (x1 - x0)*(y_max - y0)/(y1 - y0); | |
y = y_max; | |
} | |
else if (code_out & BOTTOM) { | |
x = x0 + (x1 - x0)*(y_min - y0)/(y1 - y0); | |
y = y_min; | |
} | |
else if (code_out & RIGHT) { | |
y = y0 + (y1 - y0)*(x_max - x0)/(x1 - x0); | |
x = x_max; | |
} | |
else if (code_out & LEFT) { | |
y = y0 + (y1 - y0)*(x_min - x0)/(x1 - x0); | |
x = x_min; | |
} | |
} | |
if (code_out == code1) { | |
x0 = x; | |
y0 = y; | |
code1 = compute_code(x0, y0); | |
} | |
else if (code_out == code2) { | |
x1 = x; | |
y1 = y; | |
code2 = compute_code(x1, y1); | |
} | |
} | |
if (done) { | |
printf("from %d %d to %d %d\n", x0, y0, x1, y1); | |
rectangle(x_min, y_max, x_max, y_min); | |
line(x0_ini, y0_ini, x1_ini, y1_ini); | |
delay(1000); | |
setcolor(RED); | |
line(x0, y0, x1, y1); | |
delay(1000); | |
} | |
else { | |
printf("outside\n"); | |
} | |
} | |
int main(void) { | |
int x0 = 200, y0 = 200, x1 = 400, y1 = 200; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
cohen_sutherland(x0, y0, x1, y1); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
int abs(int n) { | |
return (n > 0 ? n : -1*n); | |
} | |
void DDA(int x0, int y0, int x1, int y1) { | |
int dx = x1 - x0; | |
int dy = y1 - y0; | |
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy); | |
float xi = dx / (float) steps; | |
float yi = dy / (float) steps; | |
float x = x0; | |
float y = y0; | |
for(int i = 0; i <= steps; i++) { | |
putpixel(x, y, RED); | |
x += xi; | |
y += yi; | |
delay(10); | |
} | |
} | |
int main(void) { | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
int x0 = 100, y0 = 100, x1 = 300, y1 = 400; | |
DDA(x0, y0, x1, y1); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
void flood_fill(int x, int y, int f_col, int b_col) { | |
if(getpixel(x, y) == f_col) { | |
putpixel(x, y, b_col); | |
delay(1); | |
flood_fill(x + 1, y, f_col, b_col); | |
flood_fill(x, y + 1, f_col, b_col); | |
flood_fill(x - 1, y, f_col, b_col); | |
flood_fill(x, y - 1, f_col, b_col); | |
} | |
} | |
int main(void) { | |
int x = 200, y = 200, r = 30; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
circle(x, y, r); | |
flood_fill(x, y, 0, 15); | |
delay(1000); | |
closegraph(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<graphics.h> | |
void mid_point_circle(int x, int y, int r, int xc, int yc) { | |
int P = 1 - r; | |
int X = xc; | |
int Y = yc; | |
// putpixel(x+X, X+x+r, RED); | |
// putpixel(X+x+r, X+x, RED); | |
// putpixel(X+x-r, X+x, RED); | |
// putpixel(X+x, X+x-r, RED); | |
while(x < y) { | |
if(P < 0) { | |
x += 1; | |
y = y; | |
P += 2*x + 1; | |
} | |
else if (P >= 0) { | |
x += 1; | |
y -= 1; | |
P += 2*(x - y) + 1; | |
} | |
putpixel(X+x, Y+y, RED); | |
putpixel(Y+y, X+x, RED); | |
putpixel(X+x, Y-y, RED); | |
putpixel(X-x, Y+y, RED); | |
putpixel(X-x, Y-y, RED); | |
putpixel(Y-y, X-x, RED); | |
putpixel(Y-y, X+x, RED); | |
putpixel(Y+y, X-x, RED); | |
delay(10); | |
} | |
delay(1000); | |
} | |
int main(void) { | |
int x = 0, y = 0, r = 50, xc = 250, yc = 250; | |
int gd = DETECT, gm; | |
initgraph(&gd, &gm, NULL); | |
mid_point_circle(x, y+r, r, xc, yc); | |
closegraph(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment