Computer Graphics and Animation
Computer Graphics and Animation
Banepa-6, Kavre
LAB Report on Computer Graphics and Animation
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
4 Drawing a Eclipse
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>
int dx = x2 - x1;
int dy = y2 - y1;
delay(5000); closegraph();
}
return 0; }
Output:
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>
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);
}
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>
int main() {
int gd = DETECT, gm;
int xc, yc, radius;
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;
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>
putpixel(x, y, fillColor);
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);
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>
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 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);
}
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;
}
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
Rotation:
Enter rotation angle (in degrees): 15