Computer Graphics Lab
Computer Graphics Lab
Assignment 1.............................................................................................. 2
Problem Statement............................................................................... 2
Theory................................................................................................ 2
Advantages of DDA............................................................................... 3
Disadvantages of DDA........................................................................... 3
Viva Questions..................................................................................... 3
Assignment 2.............................................................................................. 4
Problem Statement.................................................................................................................. 4
Theory......................................................................................................................................4
Viva Questions.......................................................................................... 4
Assignment 3............................................................................................................................... 5
Problem Statement.................................................................................................................. 5
Theory......................................................................................................................................5
2D Translation....................................................................................................................5
2D Scaling..........................................................................................................................6
2D Rotation in Computer Graphics.................................................................................... 8
Assignment 1
Problem Statement
Write a program to draw line using DDA Line Drawing Algorithm
Theory
DDA algorithm is the simple line generation algorithm which is explained step by
step here.
Step 1 − Get the input of two end points (X0,Y0) and (X1,Y1).
dx = X1 - X0
dy = Y1 - Y0
Step 3 − Based on the calculated difference in step-2, you need to identify the
number of steps to put pixel. If dx > dy, then you need more steps in x coordinate;
otherwise in y coordinate.
Advantages of DDA
● DDA algorithm is faster method for calculating pixels as compared to direct
use of line equation y= mx + b
● Easy to understand and implement.
Disadvantages of DDA
1. Because round off errors are introduced and causes the calculated pixel
position to drift away from the true line path.
2. Because of floating point operations the algorithm is time consuming.
Viva Questions
1. What is slope?
2. What is intercept?
3. What is slope-intercept form of line equation
4. What is gentle slope?
5. What is steep slope?
6. Explain DDA algorithm.
Assignment 2
Problem Statement
Write a program to draw 4X4 Chessboard
Theory
1. rectangle(left, top, right, bottom): A function from graphics.h header file
which is used to draw a rectangle. Coordinates of the left top and right
bottom corners are required to draw the rectangle. left specifies the
X-coordinate of the top left corner, top specifies the Y-coordinate of the top
left corner, right specifies the X-coordinate of the right bottom corner, bottom
specifies the Y-coordinate of the right bottom corner.
2. delay(): This function is present in library “dos.h” is used for holding the
program output for a small period of time since processing is very fast so use
it to see the result.
3. setcolor(): A function from graphics.h header file which sets the color of the
pointer (cursor). There are some predefined colors in computer graphics.
Here n is the color number.
4. setfillstyle(): A function from graphics.h header file which sets the current fill
pattern and fill color.
5. floodfill(): A function from graphics.h header file which is used to fill an
enclosed area.
Viva Questions
1. Compare flood fill and boundary fill algorithm
2. What is setcolor function in graphics
3. Explain different ways by which a rectangle can be drawn
Assignment 3
Problem Statement
Write a program to perform following basic transformation
1. Translation
2. Rotation
3. Scaling
Theory
2D Translation
X’ = X + tx
Y’ = Y + ty
(tx, ty) represents the shift or the translation vector. The equations can be
expressed using column vectors for efficient representation and
computation.
P’ = P + T
2D Scaling
To perform 2D scaling, we utilize scaling factors: sx for the x-axis and sy for
the y-axis. These factors determine how much each coordinate should be
scaled along its respective axis.
If the scaling factor (SX and SY) is greater than 1, the object is enlarged and
moves away from the origin. A scaling factor of 1 leaves the object
unchanged, while a scaling factor less than 1 shrinks the object and moves it
closer to the origin.
The equations for scaling are X’ = X * SX and Y’ = Y * SY, where X and Y are
the original coordinates of a point, and X’ and Y’ are the scaled coordinates
after the transformation.
P’ = P . S
The scaling process is depicted using the scaling matrix S in the given
figure:
2D Rotation in Computer Graphics
P′ = P . R
The above matrix can be used in the case of a rotation angle which is
positive, however for a rotation angle which is negative, the matrix is shown
below: