0% found this document useful (0 votes)
3 views

Computer Graphics

Uploaded by

Huzefa zeenwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Graphics

Uploaded by

Huzefa zeenwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Department of Computer

Computer Graphics
Science and Application

Experiment No. 1

Study about computer graphics library and draw a hut by using it.
Ans:
#include <graphics.h>
#include <stdio.h>

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

// Draw the base of the hut


rectangle(200, 300, 400, 500);
setfillstyle(SOLID_FILL, GREEN);
floodfill(201, 301, WHITE);

// Draw the roof


line(200, 300, 300, 200);
line(300, 200, 400, 300);
setfillstyle(SOLID_FILL, RED);
floodfill(300, 250, WHITE);

// Draw the door


rectangle(270, 400, 330, 500);
setfillstyle(SOLID_FILL, BROWN);
floodfill(271, 401, WHITE);

// Draw the window


rectangle(220, 350, 260, 390);
setfillstyle(SOLID_FILL, BLUE);
floodfill(221, 351, WHITE);

getch();
closegraph();
return 0;
}

Experiment No.2

1 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

Write a program to implement DDA line generation algorithm?


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

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


int dx = x2 - x1, dy = y2 - y1;
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
float xIncrement = dx / (float)steps;
float yIncrement = dy / (float)steps;

float x = x1, y = y1;


for (int i = 0; i <= steps; i++) {
putpixel(round(x), round(y), WHITE);
x += xIncrement;
y += yIncrement;
}
}

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

int x1, y1, x2, y2;


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

drawLineDDA(x1, y1, x2, y2);

2 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

getch();
closegraph();
return 0;
}

Experiment No.3

Write a program to implement Bresenham’s line generation algorithm.?


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

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


int dx = x2 - x1;
int dy = y2 - y1;
int p = 2 * dy - dx;
int x = x1, y = y1;

putpixel(x, y, WHITE);

while (x < x2) {


x++;
if (p < 0) {
p += 2 * dy;
} else {
y++;
p += 2 * (dy - dx);
}
putpixel(x, y, WHITE);
}
}

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

3 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

int x1, y1, x2, y2;


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

drawLineBresenham(x1, y1, x2, y2);

getch();
closegraph();
return 0;
}

Experiment No.4

Write a program to implement Bresenham’s circle generation algorithm?


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

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


int x = 0, y = r;
int p = 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 (p < 0) {

4 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

p += 4 * x + 6;
} else {
p += 4 * (x - y) + 10;
y--;
}
x++;
}
}

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

int xc, yc, r;


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

drawCircleBresenham(xc, yc, r);

getch();
closegraph();
return 0;
}

Experiment No.5

Write a program to implement Boundary fill algorithm?


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

void boundaryFill(int x, int y, int fillColor, int boundaryColor) {


int currentColor = getpixel(x, y);

5 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

if (currentColor != fillColor && currentColor != boundaryColor) {


putpixel(x, y, fillColor);
boundaryFill(x + 1, y, fillColor, boundaryColor);
boundaryFill(x - 1, y, fillColor, boundaryColor);
boundaryFill(x, y + 1, fillColor, boundaryColor);
boundaryFill(x, y - 1, fillColor, boundaryColor);
}
}

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

// Draw a rectangle as the boundary


rectangle(100, 100, 300, 200);

// Use boundary fill to fill the rectangle


int fillColor = RED, boundaryColor = WHITE;
boundaryFill(150, 150, fillColor, boundaryColor);

getch();
closegraph();
return 0;
}

Experiment No.6

Write a program for translation of a triangle using 2d transformation.?


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

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);

6 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

line(x3, y3, x1, y1);


}

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

int x1, y1, x2, y2, x3, y3;


int tx, ty;

// Input vertices of the triangle


printf("Enter coordinates of the triangle:\n");
printf("Point 1 (x1, y1): ");
scanf("%d %d", &x1, &y1);
printf("Point 2 (x2, y2): ");
scanf("%d %d", &x2, &y2);
printf("Point 3 (x3, y3): ");
scanf("%d %d", &x3, &y3);

// Input translation factors


printf("Enter translation factors (tx, ty): ");
scanf("%d %d", &tx, &ty);

// Draw the original triangle


setcolor(WHITE);
drawTriangle(x1, y1, x2, y2, x3, y3);

// Translate the triangle


x1 += tx;
y1 += ty;
x2 += tx;
y2 += ty;

7 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

x3 += tx;
y3 += ty;

// Draw the translated triangle


setcolor(RED);
drawTriangle(x1, y1, x2, y2, x3, y3);

getch();
closegraph();
return 0;
}

Experiment No.7

Write a program for Scaling of a triangle using 2d transformation? Ans


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

void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

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

int x1, y1, x2, y2, x3, y3;


float sx, sy;

// Input vertices of the triangle


printf("Enter coordinates of the triangle:\n");
printf("Point 1 (x1, y1): ");
scanf("%d %d", &x1, &y1);
8 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

printf("Point 2 (x2, y2): ");


scanf("%d %d", &x2, &y2);
printf("Point 3 (x3, y3): ");
scanf("%d %d", &x3, &y3);

// Input scaling factors


printf("Enter scaling factors (sx, sy): ");
scanf("%f %f", &sx, &sy);

// Draw the original triangle


setcolor(WHITE);
drawTriangle(x1, y1, x2, y2, x3, y3);

// Scale the triangle (using origin as the reference point)


x1 = x1 * sx;
y1 = y1 * sy;
x2 = x2 * sx;
y2 = y2 * sy;
x3 = x3 * sx;
y3 = y3 * sy;

// Draw the scaled triangle


setcolor(RED);
drawTriangle(x1, y1, x2, y2, x3, y3);

getch();
closegraph();
return 0;
}

Experiment No.8

Write a program for Scaling of a triangle using 3d transformation?


Ans

#include <stdio.h>

9 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

// Function to apply 3D scaling on the triangle's vertices

void scaleTriangle(float x[], float y[], float z[], float sx, float sy, float sz) {

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

x[i] *= sx;

y[i] *= sy;

z[i] *= sz;

int main() {

float x[3], y[3], z[3];

float sx, sy, sz;

// Input the vertices of the triangle

printf("Enter the coordinates of the triangle:\n");

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

printf("Point %d (x, y, z): ", i + 1);

scanf("%f %f %f", &x[i], &y[i], &z[i]);

// Input the scaling factors

printf("Enter the scaling factors (sx, sy, sz): ");

scanf("%f %f %f", &sx, &sy, &sz);

// Display the original coordinates

printf("\nOriginal Triangle Coordinates:\n");

10 Huzefa Zeenwala
Department of Computer
Computer Graphics
Science and Application

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

printf("Point %d: (%.2f, %.2f, %.2f)\n", i + 1, x[i], y[i], z[i]);

// Apply scaling

scaleTriangle(x, y, z, sx, sy, sz);

// Display the scaled coordinates

printf("\nScaled Triangle Coordinates:\n");

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

printf("Point %d: (%.2f, %.2f, %.2f)\n", i + 1, x[i], y[i], z[i]);

return 0;

11 Huzefa Zeenwala

You might also like