Nothing Special   »   [go: up one dir, main page]

CG Lab Manual - Se - SPB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

“The Shetkari Shikshan Mandal’s”

PadmabhooshanVasantdada Patil Institute of

Technology, Bavdhan, Pune-21.

LAB MANUAL

OF

210247: Computer Graphics Lab

(2019 Course)

Department of Computer Engineering


210247: Computer Graphics Lab

Teaching Scheme Examination scheme (Marks)


Subject
Lect. Tut. Pract. Theory
TW PR OR Total
In Sem End Sem
210247
02
Computer
-- -- Hours/ -- -- 25 50 -- 75
Graphics Week
Lab
Table of Contents
Sr.
Topic
No
List of Experiments
Write C++/Java program to draw line using DDA and Bresenham‘s algorithm. Inherit pixel class and
1
Use function overloading.
2 Write C++/Java program to draw circle using Bresenham‘s algorithm. Inherit pixel class.
Write C++/Java program to draw the following pattern using any Line drawing algorithms.

Write C++ program to draw a 4X4 chessboard. Use DDA and Bresenham‘s drawing algorithm to draw
4
lines. Use Seed fill algorithm to fill black squares of the board
Write C++ program to draw a concave polygon and fill it with desired color using scan fill algorithm.
5

6 Write C++/Java program to implement Cohen-Sutherland line clipping algorithm for given window.
a) Write C++ program to draw 2-D object and perform following basic transformations, Scaling b)
7 Translation c) Rotation. Use operator overloading.

Write a program to draw Bezier curve using basic concepts of Object oriented programming.
8

Write C++ program to generate snowflake using concept of fractals using basic concepts of Object
9
oriented programming.
Write C++/Java program to simulate any one of or similar scene-
10 Clock with pendulum
Vehicle/boat locomotion
b) Write C++ program to draw 3-D cube and perform following transformations on it using OpenGL i)
11
Scaling ii) Translation iii) Rotation about one axis.
Write a C++ Program to make Tic Tac Toe game
12

Text:
1. S. Harrington, ―Computer Graphics‖, 2nd Edition, McGraw-Hill Publications, 1987,
ISBN 0 – 07 – 100472 – 6.
2. D. Rogers, ―Procedural Elements for Computer Graphics‖, 2nd Edition, Tata McGraw-Hill
Publication, 2001, ISBN 0 – 07 – 047371 – 4.
3. Donald D. Hearn, ―Computer Graphics with Open GL‖, 4th Edition,
ISBN-13: 9780136053583.

Reference:
1. J. Foley, V. Dam, S. Feiner, J. Hughes, ―Computer Graphics Principles and Practice‖,
2nd Edition, Pearson Education, 2003, ISBN 81 – 7808 – 038 – 9.
2. D. Rogers, J. Adams, ―Mathematical Elements for Computer Graphics‖, 2nd Edition,
Tata McGrawHill Publication, 2002, ISBN 0 – 07 – 048677 – 8.
3. Mario Zechner, Robert Green, ―Beginning Android 4 Games Development‖,
Apress, ISBN: 978-81- 322-0575-3.
Experiment No: 01
Title: Line drawing using DDA and Bresenham’s Algorithm.

Aim:-
Write C++/Java program to draw line using DDA and Bresenham‘s algorithm.
Inherit pixel class and Use function overloading

Objectives:-
 To learn function to Draw a line using DDA and Bresenham’s Algorithm.

Problem Statement: Write a c++ class for a line drawing using DDA and Bresenham’s
Algorithm, Inheriting the Pixel or Point

Software Requirement: Fedora, C++

Input: Read the X & Y co-ordinates From the Users.

Output: It Display the Line on the Screen.

Theory:

Write and Explain DDA Line Drawing Algorithm

DDA Line Drawing Algorithm:

Digital differential analyzer (DDA) is hardware or software used for linear interpolation of
variables over an interval between start and end point. DDAs are used for rasterization of lines,
triangles and polygons.DDA algorithm is an incremental scan conversion method. Here we
perform calculations at each step using the results from the preceding step. The characteristic of
the DDA algorithm is to take unit steps along one coordinate and compute the corresponding
values along the other coordinate.
Algorithm:
(i) Compute length = abs(x2 – x1)

(ii) if abs(y2 – y1) > length then length = (y2 – y1)


(iii) xinc = (x2 – x1) / length
yinc = (y2 – y1) / length
(iv) x = x1 + 0.5
y = y1 + 0.5
(v) i = 1
(vi) Plot(trunk (x), trunk (y));
(vii) x = x + xinc
y = y + yinc
(viii) i = i + 1
(ix) if i ≤ length then go to step (vi)
(x) Stop

Advantages of DDA Algorithm:

1. It is the simplest algorithm and it does not require special skills for implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation y= mx+b. It
eliminates the multiplication in the equation by making use of raster Characteristics, so that
appropriate increments are applied in the x or y direction to Find the pixel positions along the
line path.
Disadvantages of DDA Algorithm:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. So the end point accuracy is poor.

Bresenham's line algorithm


Bresenham's line algorithm is an algorithm that determines which points in an n-dimensional
raster should be plotted in order to form a close approximation to a straight line between two
given points. It is commonly used to draw lines on a computer screen, as it uses only integer
addition, subtraction and bit shifting, all of which are very cheap operations in standard computer
architectures. It is one of the earliest algorithms developed in the field of computer graphics. A
minor extension to the original algorithm also deals with drawing circles.
Algorithm:
1. Input the two line endpoints and store the left endpoint in (xo,yo)
2. Load (xO, yO) into the frame buffer; that is, plot the first point.
3. Calculate constants ∆x, ∆y, 2∆y, and 2∆y - 2∆x, and obtain the starting value for the
decision parameter as
po= 2∆y - ∆x
4. At each xk along the line, starting at k = 0, perform the following test:
If Pk< 0, the next point to plot is (xk+1, yk) and
Pk+1=P k+2∆y
Otherwise, the next point to plot is (xk+ 1, yk+ 1) and
pk+1= pk+ 2∆y - 2∆x
5. Repeat step 4 ∆x times.

Conclusion:-

Outcome of practical:
OP Marks Outcome of practical
OP1 5 Line generation using DDA algorithm

OP2 5 Line generation using Bresenham’s algorithm


Experiment No: 02
Title:Write C++ program to draw circle using Bresenham‘s algorithm.

Aim:-
Write C++/Java program to draw circle using Bresenham‘s algorithm. Inherit pixel
class.

Objectives:
1. To understand the mathematical foundation (Derivation) for Circle algorithm.
2. To Draw a Circle pixel by pixel on to the device Context or computer Screen with
the help of Algorithm
Problem Statement:
Write C++/Java program to draw circle using Bresenham‘s algorithm. Inherit pixel class.

Software Required: Linux Operating Systems, GCC.


Input: Centre of the Circle Coordinates and Radius of the Circle.
Output: Circle.
Theory:
Circles have the property of being highly symmetrical, which is handy when it comes to drawing
them on a display screen.

We know that there are 360 degrees in a circle. First we see that a circle is symmetrical about the
x axis, so only the first 180 degrees need to be calculated. Next, we see that it's also symmetrical
about the y axis, so now we only need to calculate the first 90 degrees. Finally, we see that the
circle is also symmetrical about the 45 degree diagonal axis, so we only need to calculate the first
45 degrees.

putpixel(centerx + x, center y + y)

putpixel(centerx + x, center y - y)

putpixel(centerx - x, center y + y)

putpixel(centerx - x, center y - y)

putpixel(centerx + y, center y + x)
putpixel(centerx + y, center y - x)

putpixel(centerx - y, center y + x)

putpixel(centerx - y, center y - x)

Now, consider a very small continuous arc of the circle interpolated below, passing by the
discrete pixels as shown.

As can be easily intercepted, the continuous arc of the circle cannot be plotted on a raster display
device, but has to be approximated by choosing the pixels to be highlighted. At any point (x,y),
we have two choices – to choose the pixel on east of it, i.e. N(x+1,y) or the south-east pixel
S(x+1,y-1). To choose the pixel, we determine the errors involved with both N & S which are
f(N) and f(S) respectively and whichever gives the lesser error, we choose that pixel. Let d i =
f(N) + f(S), where d can be called as "decision parameter", so that If d i <=0, then, N(x+1,y) is to
be chosen as next pixel i.e. x i+1 = x i +1 and y i+1 = y i, and
If d i >0, then, S(x+1,y-1) is to be chosen as next pixel i.e. x i+1 = x i +1 and y i+1 = y i -1.
Mathematical Foundation:

We know that for a circle,

x2 + y2 = r2

r= radius of the circle which is input to the algorithm.


Error can be represented as
f(N) = (xi + 1) 2 + y i2 - r2 , ------------------ (1)
f(S) = (xi + 1) 2 + (yi - 1)2 - r ------------- (2)
As
di = f(N) + f(S),

di = 2(xi +1) 2 + y i2 + (yi -1) 2 – 2r2 ------------- (3)

Calculate Next Decision Parameter..

di+1 = 2(xi +2) 2 + yi+12 + (yi+1 -1)2 – 2r2 --- (4)

From (4) –(3) we get

di+1 – di = 2((xi +2) 2 -(x i +1) 2 ) + (y i+12 – y i2 ) + ((y i+1 -1) 2 + (y i -1) 2 )
d i+1 = d i + 2((x i +2+x i +1)(x i +2-x i -1)) + ((y i+1 +y i )(y i+1 -y i )) + ((y i+1 -1+y i -1)(y
i+1 -1-y i +1))
d i+1 = d i + 2(2x i +3) + ((y i+1 +y i )(y i+1 -y i )) + ((y i+1 -1+y i -1)(y i+1 -1-y i +1)
Now, if (d i <=0),
x i+1 =x i +1 and y i+1 =y i
so that d i+1 = d i + 2(2x i + 3) + ((y i+1 +y i )( y i -y i )) + ((y i -1+y i -1)(y i -1-y i +1))
d i+1 = d i + 2(2x i + 3) + ((y i+1 +y i )(0)) + ((y i -1+y i -1)(0))
d i+1 = d i + 4x i + 6

Else

d i+1 = d i + 2(2x i +3) + ((y i -1+y i )(y i -1-y i )) + ((y i -2+y i -1)(y i -2-y i +1))

d i+1 = d i + 4x i +6 + ((2y i -1)(-1)) + ((2y i -3)(-1))

d i+1 = d i + 4x i +6 - 2y i - 2y i + 1 + 3
d i+1 = d i + 4(x i - y i ) + 10

To know d i+1, we have to know d i first. The initial value of d i can be obtained by replacing
x=0 and y=r in (3). Thus, we get,
d o = 2 + r 2 + (r - 1) 2 -2r 2
d o = 2 + r 2 + r 2 + 1 -2r – 2r 2
d o = 3 – 2r
Using the highlighted formulas, we can easily plot a circle on a raster graphics display device.

Pseudo code:
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (d < 0)
d = d + 4 * x + 6;
else
{
y--;
d = d + 4 * (x - y) + 10;
}
}
}

Conclusion:

Outcome of practical:
OP Marks Outcome of practical
OP1 5 Circle drawing using Bresenham’s algorithm
OP2 5 Circle drawing using midpoint function
Experiment No: 03
Write C++/Java program to draw the following pattern using any Line drawing
algorithms.

Aim :- Write C++/Java program to draw the following pattern using any Line
drawing algorithms.

Objectives:-
 To learn function to Draw a line using DDA and Bresenham’s Algorithm.

Problem Statement: Write a c++ class for a line drawing using DDA and Bresenham’s
Algorithm, Inheriting the Pixel or Point

Software Requirement: Fedora, C++

Input: Read the X & Y co-ordinates From the Users.

Output: It Display the Pattern.

Theory:

Write and Explain DDA Line Drawing Algorithm

DDA Line Drawing Algorithm:


Digital differential analyzer (DDA) is hardware or software used for linear interpolation of
variables over an interval between start and end point. DDAs are used for rasterization of lines,
triangles and polygons.DDA algorithm is an incremental scan conversion method. Here we
perform calculations at each step using the results from the preceding step. The characteristic of
the DDA algorithm is to take unit steps along one coordinate and compute the corresponding
values along the other coordinate.

Algorithm:
(i) Compute length = abs(x2 – x1)

(ii) if abs(y2 – y1) > length then length = (y2 – y1)


(iii) xinc = (x2 – x1) / length
yinc = (y2 – y1) / length
(iv) x = x1 + 0.5
y = y1 + 0.5
(v) i = 1
(vi) Plot(trunk (x), trunk (y));
(vii) x = x + xinc
y = y + yinc
(viii) i = i + 1
(ix) if i ≤ length then go to step (vi)
(x) Stop

Advantages of DDA Algorithm:

1. It is the simplest algorithm and it does not require special skills for implementation.
2. It is a faster method for calculating pixel positions than the direct use of equation y= mx+b. It
eliminates the multiplication in the equation by making use of raster Characteristics, so that
appropriate increments are applied in the x or y direction to Find the pixel positions along the
line path.
Disadvantages of DDA Algorithm:
1. Floating point arithmetic in DDA algorithm is still time-consuming.
2. The algorithm is orientation dependent. So the end point accuracy is poor.

Conclusion:-
Experiment No: 04
Title: Write C++ program to draw a 4X4 chessboard. Use DDA and
Bresenham‘s drawing algorithm to draw lines. Use Seed fill algorithm to
fill black squares of the board

Aim: Write C++ program to draw a 4X4 chessboard. Use DDA and Bresenham‘s drawing
algorithm to draw lines. Use Seed fill algorithm to fill black squares of the board

Software required: Linux Operating system, GCC


Input: Polygon (Chessboard)
Output: Filled polygon with color.

THEORY:

DDA Line Drawing Algorithm:

Digital differential analyzer (DDA) is hardware or software used for linear interpolation of
variables over an interval between start and end point. DDAs are used for rasterization of lines,
triangles and polygons.DDA algorithm is an incremental scan conversion method. Here we
perform calculations at each step using the results from the preceding step. The characteristic of
the DDA algorithm is to take unit steps along one coordinate and compute the corresponding
values along the other coordinate.

Boundary Fill Algorithm:


The boundary fill algorithm works as its name. This algorithm picks a point inside an object and
starts to fill until it hits the boundary of the object. The color of the boundary and the color that
we fill should be different for this algorithm to work.
In this algorithm, we assume that color of the boundary is same for the entire object. The
boundary fill algorithm can be implemented by 4-connected pixels or 8-connected pixels.
4-Connected Polygon:
In this technique 4-connected pixels are used as shown in the figure. We are putting the pixels
above, below, to the right, and to the left side of the current pixels and this process will continue
until we find a boundary with different color.

Algorithm
Step 1 − Initialize the value of seed point seedx,seedyseedx,seedy, fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color, then repeat the steps 4 and 5
till the boundary pixels reached.

If getpixel(x, y) = dcol then repeat step 4 and 5

Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighborhood points.
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
Step 6 – Exit
There is a problem with this technique. Consider the case as shown below where we tried to fill
the entire region. Here, the image is filled only partially. In such cases, 4-connected pixels
technique cannot be used.

8-Connected Polygon:
In this technique 8-connected pixels are used as shown in the figure. We are putting pixels
above, below, right and left side of the current pixels as we were doing in 4-connected
technique.
In addition to this, we are also putting pixels in diagonals so that entire area of the current pixel
is covered. This process will continue until we find a boundary with different color.

Algorithm
Step 1 − Initialize the value of seed point seedx,seedyseedx,seedy, fcolor and dcol.
Step 2 − Define the boundary values of the polygon.
Step 3 − Check if the current seed point is of default color then repeat the steps 4 and 5
till the boundary pixels reached
If getpixel(x,y) = dcol then repeat step 4 and 5
Step 4 − Change the default color with the fill color at the seed point.
setPixel(seedx, seedy, fcol)
Step 5 − Recursively follow the procedure with four neighbourhood points
FloodFill (seedx – 1, seedy, fcol, dcol)
FloodFill (seedx + 1, seedy, fcol, dcol)
FloodFill (seedx, seedy - 1, fcol, dcol)
FloodFill (seedx, seedy + 1, fcol, dcol)
FloodFill (seedx – 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy + 1, fcol, dcol)
FloodFill (seedx + 1, seedy - 1, fcol, dcol)
FloodFill (seedx – 1, seedy - 1, fcol, dcol)
Step 6 – Exit
Conclusion:
Experiment No: 05
Title: Write C++/Java program to draw a concave polygon and fill it with
desired pattern using scan line algorithm.

Aim: Write C++/Java program to draw a concave polygon and fill it with desired pattern using
scan line algorithm.
Title: Draw a concave polygon and fill it with desired pattern using scan line algorithm.
Objectives: To learn function to Draw and fill convex polygon using Scan line Algorithm.
Problem Statement: Write C++/Java program to draw a concave polygon and fill it with desired
pattern using scan line algorithm.
Software Requirement: Fedora, GCC
Input: Enter the co-ordinates

Output: It Display the concave polygon filled with desired color on the Screen.

Theory:
A concave polygon will always have at least one reflex interior angle—that is, an angle with a
measure that is between 180 degrees and 360 degrees exclusive.
Some lines containing interior points of a concave polygon intersect its boundary at more than
two points. Some diagonals of a concave polygon lie partly or wholly outside the polygon. Some
sidelines of a concave polygon fail to divide the plane into two half-planes one of which entirely
contains the polygon. None of these three statements holds for a convex polygon.
As with any simple polygon, the sum of the internal angles of a concave polygon is π (n − 2)
radians, equivalently 180°×(n − 2), where n is the number of sides.
i.e., ∠ABC, ∠BCD, ∠CDE, ∠DEF, ∠EFA and ∠FAB. Among the six interior angles, ∠CDE is
greater than 180°.
Properties of Concave Polygon:
A concave polygon has the following properties -
 It has at least one interior angle greater than 180° and less than 360. We can say that
a concave polygon has at least one angle a reflex angle.
 It has at least one vertex pointing inwards which gives it a concave shape.
 At least one pair of sides joining a vertex goes outside the vertex.
 There can be one more than one diagonals which lie outside the boundary of the
polygon.
 A line segment which is drawn crossing the concave polygon, intersects its boundary
more than two times. This is demonstrated by the following figure.

Scan Line Algorithm

This algorithm works by intersecting scanline with polygon edges and fills the polygon
between pairs of intersections. The following steps depict how this algorithm works.

Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax. Name
each intersection point of the polygon. As per the figure shown above, they are named as
p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1),
(p1, p2), and (p2, p3).
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the
alternate pairs.

Pseudo code : ALGORITHM:


 The scan conversion algorithm works as follows
o Intersect each scanline with all edges
o Sort intersections in x
o Calculate parity of intersections to determine in/out
o Fill the “in” pixels
 Special cases to be handled:
o Horizontal edges should be excluded
o Vertices's lying on scan-lines handled by shortening of edges,
 Coherence between scan-lines tells us that
1. Edges that intersect scan-line y are likely to intersect y +
2. X changes predictably from scanline y to y + 1 (Incremental Calculation
Possible)

 The slope of the edge is constant from one scan line to the next:
a. Let m denote the slope of the
edge. b.
 Each successive x is computed by a

Each successive x is computed by adding the inverse of the slope and rounding to the
nearest integer.

Conclusion:

Outcome of practical:
OP Marks Outcome of practical
OP1 10 Concave Polygon filling using scan line algorithm
Experiment No: 06
Title: To implement Cohen-Sutherland line clipping algorithm for
given window.

Title: Write C++/Java program to implement Cohen-Sutherland line clipping algorithm for given
window.
Objectives: To learn Cohen – Sutherland Line Clipping Algorithm.

Problem Statement: Write C++/Java program to implement Cohen-Sutherland line clipping


algorithm for given window.

Software Requirement: Ubuntu, GCC

Input: Read the X & Y co-ordinates and rectangle co-ordinates from User.

Output: It displays clipped line in the rectangle.


Theory:
Cohen-Sutherland Line Clipping The Cohen-Sutherland line clipping algorithm quickly detects
and dispenses with two common and trivial cases. To clip a line, we need to consider only its
endpoints. If both endpoints of a line lie inside the window, the entire line lies inside the window.
It is trivially accepted and needs no clipping. On the other hand, if both endpoints of a line lie
entirely to one side of the window, the line must lie entirely outside of the window. It is trivially
rejected and needs to be neither clipped nor displayed.

The sequence
for reading the codes' bits is LRBT (Left, Right, Bottom, Top).
Algorithm:

1. Given a line segment with endpoint P1=(x1,y1) and P2=(x2,y2)


2. Compute the 4-bit codes for each endpoint.
If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies completely
inside the window: pass the endpoints to the draw routine.

If both codes have a 1 in the same bit position (bitwise AND of the codes is not
0000), the line lies outside the window. It can be trivially rejected.
3. If a line cannot be trivially accepted or rejected, at least one of the two endpoints
must lie outside the window and the line segment crosses a window edge. This line
must be clipped at the window edge before being passed to the drawing routine.
4. Examine one of the endpoints, say P1=(x1,y1). Read P1 's 4-bit code in order: Left-to-
Right, Bottom-to-Top.
5. When a set bit (1) is found, compute the intersection I of the corresponding window
edge with the line from P1 to P2. Replace P1 with I and repeat the algorithm.

Before Clipping

1. Consider the line segment AD. Point A has an outcode of 0000 and point D has an outcode of
1001. The logical AND of these outcodes is zero; therefore, the line cannot be trivally rejected.
Also, the logical OR of the outcodes is not zero; therefore, the line cannot be trivally accepted.
The algorithm then chooses D as the outside point (its outcode contains 1's). By our testing order,
we first use the top edge to clip AD at B. The algorithm then recomputes B's outcode as 0000.
With the next iteration of the algorithm, AB is tested and is trivially accepted and displayed.

2. Consider the line segment EI Point E has an outcode of 0100, while point I's outcode is 1010.
The results of the trivial tests show that the line can neither be trivally rejected or accepted. Point
E is determined to be an outside point, so the algorithm clips the line against the bottom edge of
the window. Now line EI has been clipped to be line FI. Line FI is tested and cannot be trivially
accepted or rejected. Point F has an outcode of 0000, so the algorithm chooses point I as an
outside point since its outcode is1010. The line FI is clipped against the window's top edge,
yielding a new line FH. Line FH cannot be trivally accepted or rejected. Since H's outcode is
0010, the next iteration of the algorthm clips against the window's right edge, yielding line FG.
The next iteration of the algorithm tests FG, and it is trivially accepted and display.

After Clipping
After clipping the segments AD and EI, the result is that only the line segment AB and FG
can be seen in the window.

Conclusion:

Outcome of practical:
OP Marks Outcome of practical
OP1 10 Clipping of line using Cohen-Sutherland algorithm
Experiment No: 07
Title: To draw 2-D object and perform basic
transformations

Aim:
Write C++/Java program to draw 2-D object and perform following basic transformations,
a) Scaling
b) Translation
c) Rotation
Use operator overloading.

Software Required: Linux Operating Systems, C++


Input: Polygon
Output: Transformation of 2D object.
Theory:
Transformation means changing some graphics into something else by applying rules. We can
have various types of transformations such as translation, scaling up or down, rotation, shearing,
etc. When a transformation takes place on a 2D plane, it is called 2D transformation.
Transformations play an important role in computer graphics to reposition the graphics on the
screen and change their size or orientation.

Homogenous Coordinates
To perform a sequence of transformation such as translation followed by rotation and scaling, we
need to follow a sequential process −
 Translate the coordinates,
 Rotate the translated coordinates, and then
 Scale the rotated coordinates to complete the composite transformation.
 Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (t x , t y ) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).

From the above


figure, you can write that −

X’ = X + t x

Y’ = Y + t y

The pair (tx , ty ) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors.
P=[X][Y]
p' = [X′][Y′]T = [tx][ty]

We can write it as −
P’ = P + T
Rotation

In rotation, we rotate the object at particular angle θ (theta) from its origin. From the following
figure, we can see that the point P(X, Y) is located at angle φ from the horizontal X coordinate
with distance r from the origin.
Let us suppose you want to rotate it at the angle θ. After rotating it to a new location, you will get
a new point P’ (X’, Y’).

Using standard trigonometric the original coordinate of point P(X, Y) can be represented as

X=rcosφ......(1)
Y=rsinφ......(2)
Same way we can represent the point P’ (X’, Y’) as −
x′=rcos(φ+θ)=rcosφcosθ−rsinφsinθ.......(3)
y′=rsin(φ+θ)=rcosφsinθ+rsinφcosθ.......(4)
Substituting equation (1) & (2) in (3) & (4) respectively, we will get
x′=xcosθ−ysinθ
y′=xsinθ+ycosθ

Representing the above equation in matrix form,


[X′Y′]=[X′Y′][cosθ−sinθsinθcosθ]OR
P’ = P . R
Where R is the rotation matrix
R=[cosθ−sinθsinθcosθ]
The rotation angle can be positive and negative.
For positive rotation angle, we can use the above rotation matrix. However, for negative angle
rotation, the matrix will change as shown below −

R=[cos(−θ)−sin(−θ)sin(−θ)cos(−θ)]
=[cosθsinθ−sinθcosθ](∵ cos(−θ)=cosθandsin(−θ)=−sinθ)

Scaling:
To change the size of an object, scaling transformation is used. In the scaling process, you either
expand or compress the dimensions of the object. Scaling can be achieved by multiplying the
original coordinates of the object with the scaling factor to get the desired result.
Let us assume that the original coordinates are (X, Y), the scaling factors are (S X , S Y ),and the
produced coordinates are (X’, Y’). This can be mathematically represented as shown below −

X' = X . S X and Y' = Y . S Y


The scaling factor S X , S Y scales the object in X and Y direction respectively. The above
equations can also be represented in matrix form as below −

(X′Y′)=(XY)[Sx00Sy]
OR
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.

Conclusion:
Experiment No: 8
Title: Write a program to draw Bezier curve using basic concepts of Object
oriented programming.

Aim: Write a program to draw Bezier curve using basic concepts of Object oriented
programming.

Objectives: To learn function to draw Bezier curve using basic concepts of Object oriented
programming.

Problem Statement: Write C++/Java program to generate Bezier curve using basic concepts of
Object oriented programming..

Software Requirement: Fedora, GCC


Input: Enter the co-ordinates.

Output: Display Bezier curve

Theory:

Bezier curves are parametric curves which are pretty much customizable and smooth. They are
well suited for many applications. They were named after Pierre Bézier, a French mathematician
and engineer who developed this method of computer drawing in the late 1960s while working
for the car manufacturer Renault. People say that at the same time the same development took
place during the research of Ford. There is still a confusion about who found it first.

Because of my imaging background, my article will mainly focus on interpolation and curve
fitting. In interpolation, what one would simply like to do is to find unknown points using known
values. This way, a discrete case can be represented with a more continuous structure, and we
can have a well defined curve for missing points. The curve is initialized with certain data points,
and it tries to generate new ones that are approximating (or interpolating) the old values.
Constructive Bezier Curve Algorithm

Consider the n+1 points P0,…,Pn and connect the points into a polyline we will denote hereafter
as the control polygon.

Given points Pi, i = 0,...,n, our goal is to determine a curve g (t), for all values t Î [0,1]. The idea
is demonstrated below:

Basic Algorithm

The objective here is to find points in the middle of two nearby points and iterate this until we
have no more iterations. The new values of points will give us the curve. The famous Bezier
equation is the exact formulation of this idea. Here is the algorithm:

Step 1: Select a value t Î [0,1]. This value remains constant for the rest of the steps.
Step 2: Set Pi[0] (t) = Pi, for i = 0,...,n.

Step 3: For j= 0,...,n, set for i = j,...,n.

Step 4: g (t) = Pn[n] (t)

Special & General Cases

Now, I will give formulas for common, special cases that can be helpful in certain applications.
The code of the article does not demonstrate any of them, but it uses the generalized formula. So,
let me start with the generalized formula:

For the sake of simplicity and convention used in this article and code, it is better to represent
this formula as:

What this equation tells us is nothing but the formulation of the above algorithm (the mid-point
iterations). It is very important in the sense that a whole algorithm could be summarized into a
formula and a straightforward implementation would yield correct results. Here, n denotes the
number of points and P denotes the points themselves. The factorial coefficients of the points are
simply called the Bernstein basis functions, because of the name of the founder.

Here are the special cases:

Linear Bezier:

Quadratic Bezier:

Cubic Bezier:
Conclusion:
Experiment No: 09
Title: To generate snowflake using concept of fractals.

Aim: Write C++/Java program to generate snowflake using concept of fractals.


Objectives: To learn function to generate snowflake using concept of fractals.
Problem Statement: Write C++/Java program to generate snowflake using concept of fractals.

Software Requirement: Fedora, GCC


Input: Enter the co-ordinates.

Output: Display Snowflake

Theory:

Koch Snowflake:

The Koch snowflake (also known as the Koch curve, Koch star, or Koch island) is a
mathematical curve and one of the earliest fractal curves to have been described.

The progression for the area of the snowflake converges to 8/5 times the area of the original
triangle, while the progression for the snowflake’s perimeter diverges to infinity.
Consequently, the snowflake has a finite area bounded by an infinitely long line.

The Koch snowflake can be constructed by starting with an equilateral triangle, then recursively
altering each line segment as follows:
1. Divide the line segment into three segments of equal length.
2. Draw an equilateral triangle that has the middle segment from step 1 as its base and
points outward. Remove the line segment that is the base of the triangle from step 2.

3. After one iteration of this process, the resulting shape is the outline of a hexagram.
The Koch snowflake is the limit approached as the above steps are followed over and
over again. The Koch curve originally described by Helge von Koch is constructed with
only one of the three sides of the original triangle. In other words, three Koch curves
make a Koch snowflake.
Algorithm:
int x = x3 + (x4-x3)*cos(angle)+(y4-y3)*sin(angle);
int y = y3 – (x4-x3)*sin(angle)+(y4-y3)*cos(angle);
Conditions:
if(it > 0)
{
koch(x1, y1, x3, y3, it-1);
koch(x3, y3, x, y, it-1);
koch(x, y, x4, y4, it-1);
koch(x4, y4, x2, y2, it-1);
}
else
{
line(x1, y1, x3, y3);
line(x3, y3, x, y);
line(x, y, x4, y4);
line(x4, y4, x2, y2);
}

Conclusion:

Outcome of practical:
OP Marks Outcome of practical
OP1 10 Generate snowflake using concept of fractals
Experiment No: 10
Title: To simulate any one of or similar scene- Vehicle /boat locomotion

Aim: Write C++/Java program to simulate any one of or similar scene- Vehicle /boat locomotion

Objectives: To simulate the scene

Software Required: Linux Operating Systems, C++

Input: - (x,y) coordinate of rectangle, circle, minimum and maximum value of x-coordinates

Output: Moving object from initial position to last.

Theory:

Vehicle
It is a mobile machine that transports people or cargo. Typical vehicles include wagons, bicycles,
motor vehicles (motorcycles, cars, trucks, buses), railed vehicles (trains, trams), watercraft
(ships, boats), aircraft and spacecraft.
Land vehicles are classified broadly by what is used to apply steering and drive forces against the
ground: wheeled, tracked, railed or skied. ISO 3833-1977 is the standard, also internationally
used in legislation, for road vehicles types, terms and definitions.

Locomotion:
Movement from one place to another and the ability to locomote, to get from one place to the
next. The locomotive system permits locomotion and consists of bones that are the framework of
the skeleton, joints that hold the bones together and make movement possible, and muscles that
contract and relax and make for movement. In this program, we will first draw a car and color it.
In every iteration of for loop we keep on increasing the x coordinates of every point of car to
make it look like this car is moving from left to right. We will use below mentioned graphics
functions in this program.

Function Description
initgraph It initializes the graphics system by loading the passed graphics
driver then changing the system into graphics mode.
getmaxx It returns the maximum X coordinate in current graphics mode and
driver.
getmaxy It returns the maximum X coordinate in current graphics mode and
driver.
setcolor It changes the current drawing colour. Default colour is white. Each
color is assigned a number, like BLACK is 0 and RED is 4. Here we
are using colour constants defined inside graphics.h header file.
setfillstyle It sets the current fill pattern and fill color.
circle It draws a circle with radius r and centre at (x, y).
line It draws a straight line between two points on screen.
arc It draws a circular arc from start angle till end angle.
floodfill It is used to fill a closed area with current fill pattern and fill color. It
takes any point inside closed area and color of the boundary as
input.
cleardevice It clears the screen, and sets current position to (0, 0).
delay It is used to suspend execution of a program for a M milliseconds.
closegraph It unloads the graphics drivers and sets the screen back to text
mode.

Conclusion:

Outcome of practical:
OP Marks Outcome of practical
OP1 5 Simulation of clock pendulum
OP2 5 Simulation of Vehicle locomotion
Experiment No: 11
Title: Write C++ program to draw 3-D cube and perform following
transformations on it using OpenGL i) Scaling ii) Translation iii) Rotation
about one axis.

Aim: To apply the basic transformations such as translation, Scaling, Rotation for a given 3D
object.
Description: We have to perform transformations on 3D objects. Here we perform
transformations on a line segment.
The transformations are:
Translation
Scaling
Rotation
Translation: Translation is defined as moving the object from one position to another position
along straight line path.
We can move the objects based on translation distances along x and y axis. tx denotes translation
distance along x-axis and ty denotes translation distance along y axis.
Translation Distance: It is nothing but by how much units we should shift the object from one
location to another along x, y-axis.
Consider (x,y) are old coordinates of a point. Then the new coordinates of that same point (x’,y’)
can be obtained as follows:
X’=x+tx
Y’=y+ty
Z’=z+tz
We denote translation transformation as P.
2. Scaling: scaling refers to changing the size of the object either by increasing or decreasing.
We will increase or decrease the size of the object based on scaling factors along x and y-axis.
If (x, y) are old coordinates of object, then new coordinates of object after applying scaling
transformation are obtained as:
x’=x*sx
y’=y*sy.
Z’=z*sz.
sx ,sy and sz are scaling factors along x-axis, y-axis and z-axis. we express the above equations
in matrix form as:
3. Rotation: A rotation repositions all points in an object along a circular path in the plane
centered at the
pivot point. We rotate an object by an angle theta.
The Transformation matrices for above 3D transformations are given below :

Conclusion:

You might also like