Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

1. Write a Program to Draw a Line using DDA Algorithm.

#include <stdio.h>
#include <graphics.h>

void drawLineDDA(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
int dy = y2 - y1;
int steps, k;
float xIncrement, yIncrement, x = x1, y = y1;

if (abs(dx) > abs(dy)) {


steps = abs(dx);
} else {
steps = abs(dy);
}

xIncrement = (float) dx / (float) steps;


yIncrement = (float) dy / (float) steps;

putpixel(x, y, WHITE);

for (k = 0; k < steps; k++) {


x += xIncrement;
y += yIncrement;
putpixel(x, y, WHITE);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

int x1, y1, x2, y2;


printf("Enter the coordinates of the starting point (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter the coordinates of the ending point (x2 y2): ");
scanf("%d %d", &x2, &y2);

drawLineDDA(x1, y1, x2, y2);

delay(10000);
closegraph();

return 0;
}
Output -
2. Write a Program to Draw a Line using Bresenham’s Algorithm.

#include <stdio.h>
#include <graphics.h>

void drawLineBresenham(int x1, int y1, int x2, int y2) {


int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int x = x1, y = y1;
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
int err2;

putpixel(x, y, WHITE);

while (x != x2 || y != y2) {
putpixel(x, y, WHITE);
err2 = 2 * err;

if (err2 > -dy) {


err -= dy;
x += sx;
}

if (err2 < dx) {


err += dx;
y += sy;
}
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

int x1, y1, x2, y2;


printf("Enter the coordinates of the starting point (x1 y1): ");
scanf("%d %d", &x1, &y1);
printf("Enter the coordinates of the ending point (x2 y2): ");
scanf("%d %d", &x2, &y2);

drawLineBresenham(x1, y1, x2, y2);

delay(10000);
closegraph();

return 0;
}
Output –
3. Write a Program to draw Circle using Bresenham’s Algorithm.

#include <stdio.h>
#include <graphics.h>

void drawCircleBresenham(int xc, int yc, int r) {


int x = 0, y = r;
int d = 3 - 2 * r;

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, "");

int xc, yc, r;


printf("Enter the center coordinates of the circle (xc yc): ");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of the circle: ");
scanf("%d", &r);

drawCircleBresenham(xc, yc, r);

delay(5000);
closegraph();

return 0;
}
Output –
4. Write a Program to draw Circle using MID Point Algorithm.

#include <stdio.h>
#include <graphics.h>

void drawCircleMidpoint(int xc, int yc, int r) {


int x = 0, y = r;
int d = 1 - r;

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, "");

int xc, yc, r;


printf("Enter the center coordinates of the circle (xc yc): ");
scanf("%d %d", &xc, &yc);
printf("Enter the radius of the circle: ");
scanf("%d", &r);

drawCircleMidpoint(xc, yc, r);

delay(5000);
closegraph();

return 0;
}
Output –
5. Write a Program to draw Ellipse.

#include <stdio.h>
#include <graphics.h>

void drawEllipseMidpoint(int xc, int yc, int a, int b) {


int x = 0, y = b;
float d1 = b * b - a * a * b + 0.25 * a * a;
float dx = 2 * b * b * x;
float dy = 2 * a * a * y;

while (dx < dy) {


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 (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;
}
}

float d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * 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, "");

int xc, yc, a, b;


printf("Enter the center coordinates of the ellipse (xc yc): ");
scanf("%d %d", &xc, &yc);
printf("Enter the semi-major and semi-minor axes (a b): ");
scanf("%d %d", &a, &b);

drawEllipseMidpoint(xc, yc, a, b);

delay(5000);
closegraph();

return 0;
}

Output –
6. Write a Program to demonstrate Mid Point Ellipse Drawing Algorithm.

#include <stdio.h>
#include <graphics.h>

void drawEllipseMidpoint(int xc, int yc, int a, int b) {


int x = 0, y = b;
float d1 = b * b - a * a * b + 0.25 * a * a;
float dx = 2 * b * b * x;
float dy = 2 * a * a * y;

while (dx < dy) {


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 (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;
}
}

float d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * 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, "");

int xc, yc, a, b;


printf("Enter the center coordinates of the ellipse (xc yc): ");
scanf("%d %d", &xc, &yc);
printf("Enter the semi-major and semi-minor axes (a b): ");
scanf("%d %d", &a, &b);

drawEllipseMidpoint(xc, yc, a, b);

delay(5000);
closegraph();

return 0;
}

Output –
7. Write a Program for Boundary Fill Algorithm.

#include <stdio.h>
#include <graphics.h>

void boundaryFill(int x, int y, int fill_color, int boundary_color) {


if (getpixel(x, y) != boundary_color && getpixel(x, y) != fill_color) {
putpixel(x, y, fill_color);
boundaryFill(x + 1, y, fill_color, boundary_color);
boundaryFill(x - 1, y, fill_color, boundary_color);
boundaryFill(x, y + 1, fill_color, boundary_color);
boundaryFill(x, y - 1, fill_color, boundary_color);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

rectangle(100, 100, 200, 200);

boundaryFill(150, 150, RED, WHITE);

delay(5000);
closegraph();

return 0;
}

Output –
8. Write a Program to demonstrate Flood Fill Algorithm.

#include <stdio.h>
#include <graphics.h>

void floodFill(int x, int y, int old_color, int new_color) {


if (getpixel(x, y) == old_color) {
putpixel(x, y, new_color);
floodFill(x + 1, y, old_color, new_color);
floodFill(x - 1, y, old_color, new_color);
floodFill(x, y + 1, old_color, new_color);
floodFill(x, y - 1, old_color, new_color);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

rectangle(100, 100, 200, 200);

floodFill(150, 150, BLACK, GREEN);

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;
};

void insertEdge(struct Edge edgeTable[], int *edgeTableSize, struct Edge newEdge) {


int i, j;
for (i = 0; i < *edgeTableSize; i++) {
if (newEdge.xinter < edgeTable[i].xinter) {
break;
}
}

for (j = *edgeTableSize - 1; j >= i; j--) {


edgeTable[j + 1] = edgeTable[j];
}
edgeTable[i] = newEdge;
(*edgeTableSize)++;
}

void removeEdge(struct Edge edgeTable[], int *edgeTableSize, int index) {


for (int i = index; i < *edgeTableSize - 1; i++) {
edgeTable[i] = edgeTable[i + 1];
}
(*edgeTableSize)--;
}

void updateEdges(struct Edge edgeTable[], int *edgeTableSize) {


for (int i = 0; i < *edgeTableSize; i++) {
edgeTable[i].xinter += edgeTable[i].slope_inv;
}
}

void scanLineFill(int poly[][2], int n, int fill_color) {


int y_min = INT_MAX, y_max = INT_MIN;
for (int i = 0; i < n; i++) {
if (poly[i][1] < y_min) {
y_min = poly[i][1];
}
if (poly[i][1] > y_max) {
y_max = poly[i][1];
}
}
struct Edge *edgeTable = (struct Edge *)malloc((y_max - y_min + 1) * sizeof(struct
Edge));
int edgeTableSize = 0;

for (int i = 0; i < n; i++) {


int next = (i + 1) % n;
int prev = (i - 1 + n) % n;
int y1 = poly[i][1], y2 = poly[next][1];
int x1 = poly[i][0], x2 = poly[next][0];

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);
}
}

for (int y = y_min; y <= y_max; y++) {


for (int i = 0; i < edgeTableSize; i++) {
if (y == edgeTable[i].ymax) {
removeEdge(edgeTable, &edgeTableSize, i);
i--;
}
}

for (int i = 0; i < edgeTableSize; i += 2) {


int x1 = edgeTable[i].xinter, x2 = edgeTable[i + 1].xinter;
for (int x = x1; x <= x2; x++) {
putpixel(x, y, fill_color);
}
}

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

int computeCode(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 cohenSutherland(int x1, int y1, int x2, int y2) {


int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0;

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, "");

rectangle(X_MIN, Y_MIN, X_MAX, Y_MAX);

int x1, y1, x2, y2;


printf("Enter the endpoints of the line segment (x1 y1 x2 y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
line(x1, y1, x2, y2);

cohenSutherland(x1, y1, x2, y2);

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 clip(float p, float q, float *t1, float *t2) {


float r;
int returnValue = 1;
if (p < 0) {
r = q / p;
if (r > *t2) returnValue = 0;
else if (r > *t1) *t1 = r;
} else if (p > 0) {
r = q / p;
if (r < *t1) returnValue = 0;
else if (r < *t2) *t2 = r;
} else if (q < 0) returnValue = 0;
return returnValue;
}

void liangBarsky(int x1, int y1, int x2, int y2) {


float t1 = 0, t2 = 1;
int dx = x2 - x1, dy = y2 - y1;
if (clip(-dx, x1 - X_MIN, &t1, &t2) && clip(dx, X_MAX - x1, &t1, &t2) &&
clip(-dy, y1 - Y_MIN, &t1, &t2) && clip(dy, Y_MAX - y1, &t1, &t2)) {
int x_start = x1 + t1 * dx;
int y_start = y1 + t1 * dy;
int x_end = x1 + t2 * dx;
int y_end = y1 + t2 * dy;
line(x_start, y_start, x_end, y_end);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

rectangle(X_MIN, Y_MIN, X_MAX, Y_MAX);


int x1, y1, x2, y2;
printf("Enter the endpoints of the line segment (x1 y1 x2 y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
line(x1, y1, x2, y2);

liangBarsky(x1, y1, x2, y2);

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]);
}

void translate(int dx, int dy) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] += dx;
originalPoints[i][1] += dy;
}
}

void scale(float sx, float sy) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] *= sx;
originalPoints[i][1] *= sy;
}
}

void rotate(float angle) {


float radians = angle * (M_PI / 180.0);
int cx = originalPoints[0][0], cy = originalPoints[0][1];
for (int i = 1; i < numPoints; i++) {
int x = originalPoints[i][0], y = originalPoints[i][1];
originalPoints[i][0] = cx + (x - cx) * cos(radians) - (y - cy) * sin(radians);
originalPoints[i][1] = cy + (x - cx) * sin(radians) + (y - cy) * cos(radians);
}
}

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]);

for (int i = 0; i < numPoints; i++) {


line(originalPoints[i][0], originalPoints[i][1], originalPoints[i][0],
originalPoints[i][2]);
}
}

void translate(int dx, int dy, int dz) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] += dx;
originalPoints[i][1] += dy;
originalPoints[i][2] += dz;
}
}

void scale(float sx, float sy, float sz) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] *= sx;
originalPoints[i][1] *= sy;
originalPoints[i][2] *= sz;
}
}

void rotateX(float angle) {


float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int y = originalPoints[i][1], z = originalPoints[i][2];
originalPoints[i][1] = y * cos(radians) - z * sin(radians);
originalPoints[i][2] = y * sin(radians) + z * cos(radians);
}
}

void rotateY(float angle) {


float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int x = originalPoints[i][0], z = originalPoints[i][2];
originalPoints[i][0] = x * cos(radians) + z * sin(radians);
originalPoints[i][2] = -x * sin(radians) + z * cos(radians);
}
}

void rotateZ(float angle) {


float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int x = originalPoints[i][0], y = originalPoints[i][1];
originalPoints[i][0] = x * cos(radians) - y * sin(radians);
originalPoints[i][1] = x * sin(radians) + y * cos(radians);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

drawObject();
delay(2000);
cleardevice();

translate(50, 50, 50);


drawObject();
delay(2000);
cleardevice();

scale(1.5, 1.5, 1.5);


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]);
}
}

void translate(int dx, int dy, int dz) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] += dx;
originalPoints[i][1] += dy;
originalPoints[i][2] += dz;
}
}

void scale(float sx, float sy, float sz) {


for (int i = 0; i < numPoints; i++) {
originalPoints[i][0] *= sx;
originalPoints[i][1] *= sy;
originalPoints[i][2] *= sz;
}
}

void rotateX(float angle) {


float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int y = originalPoints[i][1], z = originalPoints[i][2];
originalPoints[i][1] = y * cos(radians) - z * sin(radians);
originalPoints[i][2] = y * sin(radians) + z * cos(radians);
}
}
void rotateY(float angle) {
float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int x = originalPoints[i][0], z = originalPoints[i][2];
originalPoints[i][0] = x * cos(radians) + z * sin(radians);
originalPoints[i][2] = -x * sin(radians) + z * cos(radians);
}
}

void rotateZ(float angle) {


float radians = angle * (PI / 180.0);
for (int i = 0; i < numPoints; i++) {
int x = originalPoints[i][0], y = originalPoints[i][1];
originalPoints[i][0] = x * cos(radians) - y * sin(radians);
originalPoints[i][1] = x * sin(radians) + y * cos(radians);
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

drawObject();
delay(2000);
cleardevice();

translate(50, 50, 50);


drawObject();
delay(2000);
cleardevice();

scale(1.5, 1.5, 1.5);


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]);
}
}

void obliqueProjection(float alpha) {


float projectionMatrix[3][3] = {
{1, 0, 0},
{0, 1, 0},
{-cos(alpha), -sin(alpha), 0}
};

for (int i = 0; i < numPoints; i++) {


int x = originalPoints[i][0], y = originalPoints[i][1], z = originalPoints[i][2];
originalPoints[i][0] = x + (z * cos(alpha));
originalPoints[i][1] = y + (z * sin(alpha));
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

drawObject();
delay(2000);
cleardevice();

obliqueProjection(45 * (3.14 / 180));


drawObject();

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}
};

for (int i = 0; i < numPoints; i++) {


int x = originalPoints[i][0], y = originalPoints[i][1], z = originalPoints[i][2];
originalPoints[i][0] = x;
originalPoints[i][1] = y;
}
}

int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");

drawObject();
delay(2000);
cleardevice();

parallelProjection();
drawObject();

delay(5000);
closegraph();
return 0;
}

Output –

You might also like