Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Computer Graphics and Animation

The document contains programs for drawing various shapes and images using computer graphics algorithms. It includes programs for drawing a line between two points using DDA and Bresenham's line algorithm, drawing a circle using the midpoint circle algorithm, drawing an ellipse using the midpoint ellipse algorithm, and filling shapes using flood fill and boundary fill algorithms.

Uploaded by

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

Computer Graphics and Animation

The document contains programs for drawing various shapes and images using computer graphics algorithms. It includes programs for drawing a line between two points using DDA and Bresenham's line algorithm, drawing a circle using the midpoint circle algorithm, drawing an ellipse using the midpoint ellipse algorithm, and filling shapes using flood fill and boundary fill algorithms.

Uploaded by

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

Kabhre Multiple Campus

Banepa-6, Kavre
LAB Report on Computer Graphics and Animation

Prepared by: Submitted to:


Prashant Dahal Prabek Rajbanshi
Roll No: 16 Department of IT
List of Programs

1. Digital differential analyzer (DDA) is used for linear interpolation of variables over an interval
between given start, end points and for rasterization of lines, triangles and polygons. Using DDA
Algorithm, write a C-Program to draw a line segment between two given points?

2. Bresenham’s line algorithm is an algorithm which determines which order to form a close
approximation to a straight line between two given points. Write a C program for determining
Pixel activation list between two given points in order to draw line segments using Bresenham’s
Line drawing algorithm?

3. Using the Midpoint circle generation algorithm which is a variant of Bresenham’s line algorithm,
write a C Program to generate a pixel activation list for drawing a circle with given center of
circle P(x,y) and a radius r?

4. Using the Midpoint ellipse generation algorithm which is a variant of Bresenham’s line
algorithm, write a C Program to generate a pixel activation list for drawing an ellipse?

5. By using the concept of flood fill algorithm, Write a C- program for filling a given rectangle
object with color?

6. By using the concept of Boundary fill algorithm, Write a C- program for filling a given rectangle
object with color?

7. Write a C-program for performing the basic 2D transformations such as translation, Scaling,
Rotation, shearing and reflection for a given 2D object?
Table of Contents
SN Title
Line
1 Drawing using Bresenham’s Line drawing algorithm

2 Line Drawing between two points

3 Drawing a circle with a given center of circle P(x,y) and a radius r

4 Drawing a Eclipse

5 Flood Fill algorithm to fill rectangle with color

6 Boundary fill algorithm to fill rectangle with color

7 Transformation of a object
Write a C-Program to draw a line segment between two
given points?

ALGORITHM:

1. Input the two endpoints of the line segment, (x1,y1) and (x2,y2).
2. Calculate the difference between the x-coordinates and y-coordinates of the endpoints as dx
and dy respectively.
3. Calculate the slope of the line as m = dy/dx.
4. Set the initial point of the line as (x1,y1).
5. Loop through the x-coordinates of the line, incrementing by one each time, and calculate the
corresponding y-coordinate using the equation y = y1 + m(x – x1).
6. Plot the pixel at the calculated (x,y) coordinate.
7. Repeat steps 5 and 6 until the endpoint (x2,y2) is reached.

Program :

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

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


int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);

int dx = x2 - x1;
int dy = y2 - y1;

int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

float xIncrement = (float) dx / steps; float


yIncrement = (float) dy / steps;

float x = x1, y = y1;

putpixel(round(x), round(y), WHITE);

for(int i = 1; i <= steps; i++) {


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

delay(5000); closegraph();
}

int main() { int x1,


y1, x2, y2;

printf("Enter x1, y1: ");


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

drawLine(x1, y1, x2, y2);

return 0; }

Output:

Enter x1, y1: 200 ,300


Enter x2, y2: 400 ,400
BRESENHAM’S ALGORITHM FOR LINE
DRAWING

Algorithm:
1. Input the two endpoints of the line and save the left endpoint in (x0, y0).
2. Plot the point (x0, y0).
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and obtain the first value for the
decision parameter as: p0=2Δy−Δxp0=2Δy−Δx
4. Perform the following test at each xₖ along the line, beginning at k = 0.
5. If pk<0pk<0, then the next point to plot is (xₖ₊₁, yₖ) and: pk+1=pk+2Δypk+1=pk+2Δy
6. Otherwise, the next point to plot is (xₖ₊₁, yₖ₊₁) and: pk+1=pk+2Δy−2Δxpk+1=pk
+2Δy−2Δx
7. Repeat step 4, (Δx – 1) times.

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

void draw_line(int x0, int y0, int x1, int y1) {


int dx = abs(x1 - x0); int dy = abs(y1 - y0);
int p0 = 2 * dy - dx; int x = x0, y = y0;

int x_inc = (x0 < x1) ? 1 : -1;


int y_inc = (y0 < y1) ? 1 : -1;

while (x != x1) {
putpixel(x, y, WHITE);
if (p0 < 0) {
p0 += 2 * dy;
} else {
p0 += 2 * (dy - dx);
y += y_inc;
}
x += x_inc;
}
putpixel(x, y, WHITE);
}

int main() { int gd =


DETECT, gm;
initgraph(&gd, &gm, NULL);
int x0, y0, x1, y1;
printf("Enter the coordinates of the starting point (x0 y0): "); scanf("%d
%d", &x0, &y0);
printf("Enter the coordinates of the ending point (x1 y1): ");
scanf("%d %d", &x1, &y1);

draw_line(x0, y0, x1, y1);

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

Output:
Enter the coordinates of the starting point (x0 y0): 100 100
Enter the coordinates of the ending point (x1 y1): 300 300
BRESENHAM’S ALGORITHM TO DRAW A
CIRCLE

ALGORITHM:
1. Start Algorithm
2. Declare p, q, x, y, r, d variables: p, q are coordinates of the center of the circle r is the radius
of the circle
3. Enter the value of r
4. Calculate d = 3 - 2r
5. Initialize x = 0 and y = r
6. Check if the whole circle is scan converted: If x >= y, Stop
7. Plot eight points by using concepts of eight-way symmetry. The center is at (p, q). Current
active pixel is (x, y):
putpixel(x+p, y+q) putpixel(y+p,
x+q) putpixel(-y+p, x+q)
putpixel(-x+p, y+q) putpixel(-
x+p, -y+q) putpixel(-y+p, -x+q)
putpixel(y+p, -x+q) putpixel(x+p,
-y+q)
8. Find the location of the next pixels to be scanned:
If d < 0, then d = d + 4x + 6, increment x = x + 1
If d ≥ 0, then d = d + 4(x - y) + 10, increment x = x + 1, decrement y = y - 1
9. Go to step 6
10. Stop Algorithm

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

void drawCircle(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=d+
4 * x + 6; } else { d
= d + 4 * (x - y) + 10;
y--;
} x++;
}
}

int main() {
int gd = DETECT, gm;
int xc, yc, radius;

initgraph(&gd, &gm, "");

printf("Enter the center of the circle (x, y): ");


scanf("%d %d", &xc, &yc); printf("Enter the
radius of the circle: "); scanf("%d", &radius);
drawCircle(xc, yc, radius); getchar();
delay(10000); closegraph(); return 0; }

OUTPUT:
Enter the center of the circle (x, y): 250 250
Enter the radius of the circle: 100
BRESENHAM’S ALGORITHM TO DRAW A
Eclipse

ALGORITHM:
1. Start the algorithm.
2. Define center (xc, yc), major axis (a), and minor axis (b) of the ellipse.
3. Calculate initial decision parameters d1 and d2.
4. Initialize x = 0 and y = b.
5. Plot the initial point (x, y) in the first quadrant and its symmetrical points in other octants.
6. Repeat until the entire ellipse is drawn:
A. Calculate the decision parameter d1 for the next position.
B. If d1 < 0, update d1 and increment x.
C. If d1 ≥ 0, update d1, decrement y, and increment x.
D. Plot the current pixel (x, y) and its symmetrical points in other octants.
7. Stop the algorithm.

Program:
#include <stdio.h>
#include <graphics.h>
void drawEllipse(int xc, int yc, int a, int b) {
int x = 0, y = b;
int d1 = (b * b) - (a * a * b) + (0.25 * a * a); int
deltaE = 2 * b * b * x;
int deltaSE = 2 * b * b * x + 2 * b * b;

while (deltaE < deltaSE) {


putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
x++;
deltaE += 2 * b * b;
if (d1 < 0) {
d1 += deltaE + b * b;
} else {
y--;
deltaSE += 2 * a * a;
d1 += deltaE + b * b - deltaSE;
}
}

int 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);
y--;
deltaSE -= 2 * a * a;
if (d2 > 0) {
d2 += a * a - deltaSE;
} else {
x++;
deltaE += 2 * b * b;
d2 += a * a - deltaSE + deltaE;
}
}
}

int main() { int gd =


DETECT, gm;
int xc, yc, a, b;

initgraph(&gd, &gm, "");


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

drawEllipse(xc, yc, a, b);


getchar(); delay(1000);
closegraph();

return 0;
}

Output:
By using the concept of flood fill algorithm, write a
C- program for filling a given rectangle object with
color

Algorithm:
Procedure floodfill(x, y, fill_color, old_color: integer)
If (getpixel(x, y) = old_color)
{
setpixel(x, y, fill_color);
floodfill(x+1, y, fill_color, old_color);
floodfill(x-1, y, fill_color, old_color);
floodfill(x, y+1, fill_color, old_color);
floodfill(x, y-1, fill_color, old_color);
}

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

void floodFill(int x, int y, int fillColor, int oldColor) {


if (getpixel(x, y) != oldColor) {
return;
}

putpixel(x, y, fillColor);

floodFill(x + 1, y, fillColor, oldColor);


floodFill(x - 1, y, fillColor, oldColor); floodFill(x,
y + 1, fillColor, oldColor); floodFill(x, y - 1,
fillColor, oldColor);
}

int main() { int gd =


DETECT, gm;
int left, top, right, bottom, color;

initgraph(&gd, &gm, "");

printf("Enter the coordinates of the rectangle (left, top, right, bottom): "); scanf("%d%d%d%d",
&left, &top, &right, &bottom);
printf("Enter the fill color (integer value): ");
scanf("%d", &color);

// Draw the rectangle


rectangle(left, top, right, bottom);

// Perform flood fill inside the rectangle


floodFill(left + 1, top + 1, color, getpixel(left + 1, top + 1));

delay(5000); // Delay to display the result

closegraph(); return
0;
}

Output:
Enter the coordinates of the rectangle (left, top, right, bottom): 100 200 300 300 Enter the fill
color (integer value): 3
By using the concept of Boundary fill algorithm,
Write a C- program for filling a given rectangle object
with color?

Algorithm:
Procedure boundaryfill(x, y, fill_color, boundary_color: integer)
If (getpixel(x, y) != boundary_color and getpixel(x, y) != fill_color)
{
setpixel(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);
}

Program:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

// Function to perform boundary fill 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);
}
}

// Function to draw a rectangle


void drawRectangle(int x1, int y1, int x2, int y2) { rectangle(x1,
y1, x2, y2);
}

int main() { int gd =


DETECT, gm;
initgraph(&gd, &gm, "");

int x1, y1, x2, y2; // Rectangle coordinates


int fill_color; // Fill color
int boundary_color = WHITE; // Boundary color

// Ask user for input


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

printf("Enter the fill color (integer value): "); scanf("%d",


&fill_color);

// Draw the rectangle


drawRectangle(x1, y1, x2, y2);

// Fill the rectangle


boundaryFill((x1 + x2) / 2, (y1 + y2) / 2, fill_color, boundary_color);
delay(5000); closegraph(); return 0;
}

Output:
Enter the coordinates of the rectangle (x1 y1 x2 y2): 100 100 300 200
Enter the fill color (integer value): 4
Write a C-program for performing the basic 2D
transformations such as translation, Scaling, Rotation,
shearing and reflection for a given 2D object?

Program:
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

typedef struct {
int x;
int y;
} Point;

void drawTriangle(Point p1, Point p2, Point p3) {


line(p1.x, p1.y, p2.x, p2.y); line(p2.x,
p2.y, p3.x, p3.y); line(p3.x, p3.y, p1.x,
p1.y);
}

void translateTriangle(Point *p1, Point *p2, Point *p3, int tx, int ty) {
p1->x += tx; p1->y += ty; p2->x += tx; p2->y += ty; p3->x +=
tx; p3->y += ty;
}

void scaleTriangle(Point *p1, Point *p2, Point *p3, float sx, float sy) {
p1->x = (int)(p1->x * sx); p1->y = (int)(p1->y * sy); p2->x =
(int)(p2->x * sx); p2->y = (int)(p2->y * sy); p3->x = (int)(p3->x *
sx); p3->y = (int)(p3->y * sy);
}

void rotateTriangle(Point *p1, Point *p2, Point *p3, float angle) {


float rad = angle * M_PI / 180.0; int temp_x, temp_y;

temp_x = p1->x; temp_y = p1->y; p1->x =


(int)(temp_x * cos(rad) - temp_y * sin(rad));
p1->y = (int)(temp_x * sin(rad) + temp_y * cos(rad));

temp_x = p2->x; temp_y = p2->y; p2->x =


(int)(temp_x * cos(rad) - temp_y * sin(rad));
p2->y = (int)(temp_x * sin(rad) + temp_y * cos(rad));
temp_x = p3->x; temp_y = p3->y; p3->x =
(int)(temp_x * cos(rad) - temp_y * sin(rad));
p3->y = (int)(temp_x * sin(rad) + temp_y * cos(rad));
}

void reflectTriangle(Point *p1, Point *p2, Point *p3, char axis) {


if (axis == 'x') { p1->y = -p1->y; p2->y = -p2->y; p3-
>y = -p3->y; } else if (axis == 'y') { p1->x = -p1->x; p2-
>x = -p2->x; p3->x = -p3->x;
}
}

int main() { int gd = DETECT, gm;


initgraph(&gd, &gm, "C:\\TC\\BGI");

Point p1 = {100, 100};


Point p2 = {200, 100};
Point p3 = {150, 200};
int original_color = getcolor();

drawTriangle(p1, p2, p3);


printf("Initial Triangle Displayed.\n");

printf("\nChoose Transformation:\n");
printf("1. Translate\n"); printf("2.
Scale\n"); printf("3. Rotate\n");
printf("4. Reflect\n"); printf("Enter
your choice (1-4): "); int choice;
scanf("%d", &choice);

switch (choice) {
case 1: {
int tx, ty;
printf("Enter translation vector (tx, ty): ");
scanf("%d %d", &tx, &ty); translateTriangle(&p1,
&p2, &p3, tx, ty);
break;
} case 2: {
float sx, sy;
printf("Enter scaling factors (sx, sy): ");
scanf("%f %f", &sx, &sy); scaleTriangle(&p1,
&p2, &p3, sx, sy);
break;
} case 3: {
float angle;
printf("Enter rotation angle (in degrees): ");
scanf("%f", &angle); rotateTriangle(&p1, &p2,
&p3, angle);
break;
} case 4:
{ char axis;
printf("Enter reflection axis (x or y): ");
scanf(" %c", &axis); reflectTriangle(&p1,
&p2, &p3, axis);
break; } default:
printf("Invalid choice.\n");
break;
}

drawTriangle(p1, p2, p3);

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

Output:
Choose Transformation:
1. Translate
2. Scale
3. Rotate
4. Reflect
Enter your choice (1-4):

Initial Object:

Translate
Enter translation vector (tx, ty): 50 50
Scaling

Enter scaling factors (sx, sy): 2 2

Rotation:
Enter rotation angle (in degrees): 15

You might also like