Practical CG Sim
Practical CG Sim
#include <stdio.h>
#include <graphics.h>
putpixel(x, y, WHITE);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(10000);
closegraph();
return 0;
}
Output -
2. Write a Program to Draw a Line using Bresenham’s Algorithm.
#include <stdio.h>
#include <graphics.h>
putpixel(x, y, WHITE);
while (x != x2 || y != y2) {
putpixel(x, y, WHITE);
err2 = 2 * err;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(10000);
closegraph();
return 0;
}
Output –
3. Write a Program to draw Circle using Bresenham’s Algorithm.
#include <stdio.h>
#include <graphics.h>
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (d < 0) {
d += 4 * x + 6;
} else {
d += 4 * (x - y) + 10;
y--;
}
x++;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
4. Write a Program to draw Circle using MID Point Algorithm.
#include <stdio.h>
#include <graphics.h>
while (x <= y) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (d < 0) {
d += 2 * x + 3;
} else {
d += 2 * (x - y) + 5;
y--;
}
x++;
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
5. Write a Program to draw Ellipse.
#include <stdio.h>
#include <graphics.h>
if (d1 < 0) {
x++;
dx += 2 * b * b;
d1 += dx + b * b;
} else {
x++;
y--;
dx += 2 * b * b;
dy -= 2 * a * a;
d1 += dx - dy + b * b;
}
}
while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
if (d2 > 0) {
y--;
dy -= 2 * a * a;
d2 += a * a - dy;
} else {
y--;
x++;
dx += 2 * b * b;
dy -= 2 * a * a;
d2 += dx - dy + a * a;
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
6. Write a Program to demonstrate Mid Point Ellipse Drawing Algorithm.
#include <stdio.h>
#include <graphics.h>
if (d1 < 0) {
x++;
dx += 2 * b * b;
d1 += dx + b * b;
} else {
x++;
y--;
dx += 2 * b * b;
dy -= 2 * a * a;
d1 += dx - dy + b * b;
}
}
while (y >= 0) {
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
if (d2 > 0) {
y--;
dy -= 2 * a * a;
d2 += a * a - dy;
} else {
y--;
x++;
dx += 2 * b * b;
dy -= 2 * a * a;
d2 += dx - dy + a * a;
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
7. Write a Program for Boundary Fill Algorithm.
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
8. Write a Program to demonstrate Flood Fill Algorithm.
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
9. Write a Program to demonstrate Scan-Line Polygon Fill Algorithm.
#include <stdio.h>
#include <graphics.h>
struct Edge {
int ymax;
float xinter, slope_inv;
};
if (y1 != y2) {
struct Edge newEdge;
if (y1 < y2) {
newEdge.ymax = y2;
newEdge.xinter = (float)x1;
} else {
newEdge.ymax = y1;
newEdge.xinter = (float)x2;
}
newEdge.slope_inv = (float)(x2 - x1) / (y2 - y1);
insertEdge(edgeTable, &edgeTableSize, newEdge);
}
}
updateEdges(edgeTable, &edgeTableSize);
}
free(edgeTable);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int n;
printf("Enter the number of vertices of the polygon: ");
scanf("%d", &n);
int poly[n][2];
printf("Enter the vertices of the polygon (x y):\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &poly[i][0], &poly[i][1]);
}
scanLineFill(poly, n, GREEN);
delay(5000);
closegraph();
return 0;
}
Output –
10. Write a Program to demonstrate Cohen Sutherland Algorithm.
#include <stdio.h>
#include <graphics.h>
#define INSIDE 0
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
#define X_MIN 50
#define Y_MIN 50
#define X_MAX 300
#define Y_MAX 300
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;
}
while (1) {
if (!(code1 | code2)) {
accept = 1;
break;
} else if (code1 & code2) {
break;
} else {
int x, y;
int codeOut = code1 ? code1 : code2;
if (codeOut & TOP) {
x = x1 + (x2 - x1) * (Y_MAX - y1) / (y2 - y1);
y = Y_MAX;
} else if (codeOut & BOTTOM) {
x = x1 + (x2 - x1) * (Y_MIN - y1) / (y2 - y1);
y = Y_MIN;
} else if (codeOut & RIGHT) {
y = y1 + (y2 - y1) * (X_MAX - x1) / (x2 - x1);
x = X_MAX;
} else if (codeOut & LEFT) {
y = y1 + (y2 - y1) * (X_MIN - x1) / (x2 - x1);
x = X_MIN;
}
if (codeOut == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
11. Write a Program to demonstrate Liang-Barsky Line Clipping Algorithm.
#include <stdio.h>
#include <graphics.h>
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
#define X_MIN 50
#define Y_MIN 50
#define X_MAX 300
#define Y_MAX 300
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
delay(5000);
closegraph();
return 0;
}
Output –
12. Write a Program to demonstrate 2D Transformation (Translation, Scaling and
Rotation).
#include <stdio.h>
#include <graphics.h>
#include <math.h>
int originalPoints[][2] = {{100, 100}, {200, 100}, {200, 200}, {100, 200}, {100, 100}};
int numPoints = 5;
void drawObject() {
for (int i = 0; i < numPoints - 1; i++) {
line(originalPoints[i][0], originalPoints[i][1], originalPoints[i + 1][0],
originalPoints[i + 1][1]);
}
line(originalPoints[numPoints - 1][0], originalPoints[numPoints - 1][1],
originalPoints[0][0], originalPoints[0][1]);
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawObject();
delay(2000);
cleardevice();
translate(50, 50);
drawObject();
delay(2000);
cleardevice();
scale(1.5, 1.5);
drawObject();
delay(2000);
cleardevice();
rotate(60);
drawObject();
delay(5000);
closegraph();
return 0;
}
Output –
13. Write a Program to demonstrate 3D Transformation (Translation, Scaling and
Rotation).
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#define PI 3.14159265
int originalPoints[][3] = {{100, 100, 100}, {200, 100, 100}, {200, 200, 100}, {100, 200,
100},
{100, 100, 200}, {200, 100, 200}, {200, 200, 200}, {100, 200, 200}};
int numPoints = 8;
void drawObject() {
for (int i = 0; i < numPoints - 1; i++) {
line(originalPoints[i][0], originalPoints[i][1], originalPoints[i + 1][0],
originalPoints[i + 1][1]);
}
line(originalPoints[numPoints - 1][0], originalPoints[numPoints - 1][1],
originalPoints[0][0], originalPoints[0][1]);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawObject();
delay(2000);
cleardevice();
rotateX(45);
drawObject();
delay(2000);
cleardevice();
rotateY(45);
drawObject();
delay(2000);
cleardevice();
rotateZ(45);
drawObject();
delay(5000);
closegraph();
return 0;
}
Output –
14. Write a Program to demonstrate 3D Transformation (Translation, Scaling and
Rotation).
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#define PI 3.14159265
int originalPoints[][3] = {{50, 50, 50}, {100, 50, 50}, {100, 100, 50}, {50, 100, 50},
{50, 50, 100}, {100, 50, 100}, {100, 100, 100}, {50, 100, 100}};
int numPoints = 8;
void drawObject() {
for (int i = 0; i < 4; i++) {
line(originalPoints[i][0], originalPoints[i][1], originalPoints[(i + 1) % 4][0],
originalPoints[(i + 1) % 4][1]);
line(originalPoints[i + 4][0], originalPoints[i + 4][1], originalPoints[((i + 1) % 4) +
4][0], originalPoints[((i + 1) % 4) + 4][1]);
line(originalPoints[i][0], originalPoints[i][1], originalPoints[i + 4][0],
originalPoints[i + 4][1]);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawObject();
delay(2000);
cleardevice();
rotateX(45);
drawObject();
delay(2000);
cleardevice();
rotateY(45);
drawObject();
delay(2000);
cleardevice();
rotateZ(45);
drawObject();
delay(5000);
closegraph();
return 0;
}
Output –
15. Write a Program to demonstrate Oblique Projection of 3D Object.
#include <stdio.h>
#include <graphics.h>
int originalPoints[][3] = {{50, 50, 50}, {100, 50, 50}, {100, 100, 50}, {50, 100, 50},
{50, 50, 100}, {100, 50, 100}, {100, 100, 100}, {50, 100, 100}};
int numPoints = 8;
void drawObject() {
for (int i = 0; i < 4; i++) {
line(originalPoints[i][0], originalPoints[i][1], originalPoints[(i + 1) % 4][0],
originalPoints[(i + 1) % 4][1]);
line(originalPoints[i + 4][0], originalPoints[i + 4][1], originalPoints[((i + 1) % 4) +
4][0], originalPoints[((i + 1) % 4) + 4][1]);
line(originalPoints[i][0], originalPoints[i][1], originalPoints[i + 4][0],
originalPoints[i + 4][1]);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawObject();
delay(2000);
cleardevice();
delay(5000);
closegraph();
return 0;
}
Output –
16. Write a Program to demonstrate Parallel Projection of 3D Object.
#include <stdio.h>
#include <graphics.h>
int originalPoints[][3] = {{50, 50, 50}, {100, 50, 50}, {100, 100, 50}, {50, 100, 50},
{50, 50, 100}, {100, 50, 100}, {100, 100, 100}, {50, 100, 100}};
int numPoints = 8;
void drawObject() {
for (int i = 0; i < 4; i++) {
line(originalPoints[i][0], originalPoints[i][1], originalPoints[(i + 1) % 4][0],
originalPoints[(i + 1) % 4][1]);
line(originalPoints[i + 4][0], originalPoints[i + 4][1], originalPoints[((i + 1) % 4) +
4][0], originalPoints[((i + 1) % 4) + 4][1]);
line(originalPoints[i][0], originalPoints[i][1], originalPoints[i + 4][0],
originalPoints[i + 4][1]);
}
}
void parallelProjection() {
float projectionMatrix[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 1}
};
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
drawObject();
delay(2000);
cleardevice();
parallelProjection();
drawObject();
delay(5000);
closegraph();
return 0;
}
Output –