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

Computer Graphics Lab

Uploaded by

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

Computer Graphics Lab

Uploaded by

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

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).

Step 2 − Calculate the difference between two end points.

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.

if (absolute(dx) > absolute(dy))


Steps = absolute(dx);
else
Steps = absolute(dy);

Step 4 − Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;


Yincrement = dy / (float) steps;

Step 5 − Put the pixel by successfully incrementing x and y coordinates accordingly


and complete the drawing of the line.
for(int v=0; v < Steps; v++)
{
x = x + Xincrement;
y = y + Yincrement;
putpixel(Round(x), Round(y));
}

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

2D Translation is a transformation technique that changes the position of


each point in an object or a coordinate system by a specified distance in the
x and y axes.

Applying 2D translation, we can say:

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=[X]/[Y] p’ = [X′]/[Y′] T = [tx]/[ty]

This can also be written as:

P’ = P + T

2D Scaling

2D Scaling in Computer Graphics involves resizing objects or coordinate


systems in a 2D plane. It allows us to change the size of each point in the
object or coordinate system by applying scaling factors in the x and y
directions.

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.

These equations can also be represented in matrix form as:


OR

P’ = P . S

The scaling process is depicted using the scaling matrix S in the given
figure:
2D Rotation in Computer Graphics

2D rotation is a fundamental concept that involves changing the orientation


of an object or a coordinate system in a 2D plane. It enables us to rotate
graphical elements around a specified point or axis by a certain angle.
To perform a 2D rotation, we need to consider two key components: the
rotation angle and the rotation center. The rotation angle, denoted as θ (in
radians), represents the amount of rotation to be applied. The rotation
center, represented as (cx, cy), defines the point around which the rotation
will occur.

The original coordinates of points P, A, and B can be represented using


standard trigonometric functions.

Similarly, the points P’, A’, and B’ can be represented as follows:


Upon substituting equations (1) and (2) into equations (3) and (4)
correspondingly, we obtain the following expressions:

In matrix form, the above equation can be represented as follows:

P′ = P . R

Where R is the rotation matrix, the equation can be represented in matrix


form as follows:

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:

You might also like