Documente Academic
Documente Profesional
Documente Cultură
A
Lab Manual
For
Prepared by
List of Assignment
Sr Assignment Name
No.
1. Write a C++ class for a Line drawing method using DDA and Bresenham’s
Algorithms. Inherit the pixel class and use function overloading.
2. Write a C++ program to draw a circle using bresenham’s algorithm .Inherit pixel
class.
3. Write C++ program to draw 2-D object and perform following basic
transformation-Scaling, Translation, Rotation.
4. Write C++ program to draw inscribed and circumscribed circles in the triangle as
shown as on example.
5. Write C++ program to draw the following pattern using any line drawing
algorithm.
6. Write C++ program for line drawing using DDA or Bresenhams algorithm with
patterns such as solid, dotted, dashed, dash dot and thick.
7. Write C++ program to implement reflection of 2-D object about X axis, Y axis
and about X=Y axis..
9. Write C++ program to implement painter’s algorithm for hidden surface removal.
10. Write C++ program to implement translation, shear, rotation and scaling
transformations on equilateral triangle and rhombus
11. Write C++/Java program to draw 3-D cube and perform following
transformations on it using OpenGL. a) Scaling b) Translation c) Rotation about
one axis
Vehicle/boat locomotion Water drop falling into the water and generated
waves after impact
Assignment No:- 1
Aim :- Write a C++ class for a Line drawing method using DDA and Bresenham’s Algorithms.
Theory:-
What is inheritance?
Types of inheritance.
The vector generation algorithms which step along the line to determine the pixel which should
be turned on are sometimes called “Digital Differential Analyzer”.
The above differential equation can be used to obtain a rasterized straight line. For any given X interval ∆
X along a line, we can compute the corresponding y interval ∆Y from euation (1) as
Y2-Y1 ----------------------(2)
∆Y = ∆X
X2-X1
X2-X1
∆X = ∆Y
Y2-Y1
Once the intervals are known the values for next X and next Y on the straight line can be obtained as
follows:
Put the values of ∆X and ∆Y in the equations (4) and (5) , then these equation represent a recursion for
successive values of X and Y along the required line. Such a way of rasterizing a line is called “ Digital
Differential Analyzer (DDA).”
For simple DDA either ∆X or ∆Y , whichever is larger, is chosen as one raster unit i.e,
∆X = 1
Else
∆Y = 1.
Algorithm:
STEP1: Read the line end points (x1,y1) and (x2,y2)
STEP 5: X= X1+0.5*sign(∆X)
sign function makes the algorithm work in all quadrant. It
Y=Y1+0.5*sign(∆Y) returns -1,0,1 depending on whether its argument is <0,
=0, >0 respectively.
STEP 6: i=1
while (i ≤ length)
{
Plot(Integer(x), Integer(y))
X=X+∆X;
Y=Y+∆Y;
i=i+1
}
STEP7: STOP
Flowchart:
Bresenham’s line algorithm uses only integer addition and subtraction and multiplication by 2,
and we know that the computer can perform the operations of integer addition and subtraction very
rapidly. The computer is also time-effiecient when performing integer multiplication by powers of 2.
Therefore , it is an efficient method for scan converting straight lines.
The basic principle of bresenham’s line algorithm is to select the optimum raster locations to
represent a straight line. To accomplish this the algorithm always increments either x or y by one unit
depending upon the slope of line . To increment i the other variable is determined by examining the
distance between the actual line location and the nearest pixel. This distance is called “Decision variable
or the error”.
Algorithm:-
1. Read the line end points (x1,y1) and (x2,y2) such that they are not equal.
2. ∆x = |x2-x1| and ∆y = |y2-y1|
3. Initialize starting point
x = x1
y = y1
4. e = 2* ∆y-∆x [initialize value of decision variable or error to compensate for nonzero intercepts]
5. i=1 [initialize counter]
6. plot(x,y)
7. while(e>=0)
{
y = y+1;
e = e-2 * ∆x
}
x=x+1;
e =e + 2*∆y
8. i =i+1
9. if(i<=∆x) then go to step 6
10. stop
Conclusion:-
Thus We’ve learnt and understood the concept of inheritance and overloading.
Assignment No:- 2
Aim:-Write a C++ program to draw a circle using bresenham’s algorithm .Inherit pixel class.
Objectives:-Learn and understand the concept of circle drawing by inheriting line class.
Theory:-
Algorithm:-
1. Read the radius r of the circle
2. d = 5/4-r
3. x = 0, y = r ( initialize starting point)
4. do
{
putpixel(x,y,10);
if(d<0) then
{
d=d + 2x +1
}
else
{
d=d + 2(x-y) +1
y= y-1
}
x = x+1
}while(x<=y)
5. stop
The remaining part of circle can be drawn by reflecting point about y-axis , x-axis and about origin as
shown in fig.
Therefore, by adding seven more plot commands after the plot command in step 4 of the algorithm,
the circle can be plotted . the remaining seven plot commands are:
Plot(y,x)
Plot(y,-x)
Plot(x,-y)
Plot(-x,-y)
Plot(-y,-x)
Plot(-y,x) and
Plot(-x,y)
FAQ:
1. What is inheritance?
2. What is the advantage of inheritance?
3. What is meant by single inheritance.?
4. What is multiple inheritances?
5. What is hierarchical inheritance?
6. What is multilevel inheritance?
7. What is hybrid inheritance?
8. What is the need of Bresenham’s Circle drawing algorithm?
9. What are the differences between a C++ struct and C++ class?
Conclusion:-
Thus we’ve learnt and understood the concept of circle drawing by inheriting line class.
Assignment No: A3
Aim : Write C++ program to draw 2-D object and perform following basic transformation-Scaling,
Translation, Rotation.
Theory:
Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors
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’).
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 (SX, SY), and the
produced coordinates are (X’, Y’). This can be mathematically represented as shown below −
The scaling factor SX, SY scales the object in X and Y direction respectively.
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.
Conclusion-
Assignment No: A4
Aim : Write C++ program to draw inscribed and circumscribed circles in the triangle as shown as on
example:
Objective: Learn and understand the concept of line drawing and circle drawing
Theory:
The vector generation algorithms which step along the line to determine the pixel which should
be turned on are sometimes called “Digital Differential Analyzer”.
Algorithm:
STEP1: Read the line end points (x1,y1) and (x2,y2)
STEP 5: X= X1+0.5*sign(∆X)
sign function makes the algorithm work in all quadrant. It
Y=Y1+0.5*sign(∆Y) returns -1,0,1 depending on whether its argument is <0,
=0, >0 respectively.
STEP 6: i=1
while (i ≤ length)
{
Plot(Integer(x), Integer(y))
X=X+∆X;
Y=Y+∆Y;
i=i+1
}
STEP7: STOP
Therefore, by adding seven more plot commands after the plot command in step 4 of the algorithm,
the circle can be plotted . the remaining seven plot commands are:
Plot(y,x)
Plot(y,-x)
Plot(x,-y)
Plot(-x,-y)
Plot(-y,-x)
Plot(-y,x) and
Plot(-x,y)
Conclusion:-
Thus we’ve learnt and understood the concept of line drwing and circle drawing by inheriting line class.
Assignment No: A5
Aim : Write C++ program to draw the following pattern using any line drawing algorithm
The vector generation algorithms which step along the line to determine the pixel which should
be turned on are sometimes called “Digital Differential Analyzer”.
Algorithm:
STEP1: Read the line end points (x1,y1) and (x2,y2)
STEP 5: X= X1+0.5*sign(∆X)
sign function makes the algorithm work in all quadrant. It
Y=Y1+0.5*sign(∆Y) returns -1,0,1 depending on whether its argument is <0,
=0, >0 respectively.
STEP 6: i=1
while (i ≤ length)
{
Plot(Integer(x), Integer(y))
X=X+∆X;
Y=Y+∆Y;
i=i+1
}
STEP7: STOP
Conclusion:- Thus we’ve learnt and understood the concept of line drwing and circle drawing by
inheriting line class.
Assignment No:- B6
Aim:- Write Write C++ program for line drawing using DDA or Bresenhams algorithm with patterns
such as solid, dotted, dashed, dash dot and thick.
Theory:
Basic attributes of a straight line segment are its type, its width, and its color. In some graphics packages,
lines can also be displayed using selected pen or brush options. In the following sections, we consider
how line drawing routines can be modified to accommodate various attribute specifications.
Line Type
Possible selections for the line-type attribute include solid lines, dashed lines, and dotted lines. We
modify a line drawing algorithm to generate such lines by setting the length and spacing of displayed
solid sections along the line path. A dashed line could be displayed by generating an inter dash spacing
that is equal to the length of the solid sections. Both the length of the dashes and the inter dash spacing are
often specified as user options. A dotted line can be displayed by generating very short dashes with the
spacing equal to or greater than the dash size. Similar methods are used to produce other line-type
variations. Line Attributes To set line type attributes in a PHICS application program, a user invokes the
function
setLinetype (lt)
where parameter 1t is assigned a positive integer value of 1,2,3, or 4 to generate lines that are,
respectively, solid, dashed, dotted, or dash-dotted.
Line Width
Implementation of line- width options depends on the capabilities of the output device. A heavy line on a
monitor could be displayed as adjacent parallel lines, while a pen plotter might require pen changes. As
with other PHIGS attributes, a line-width command is used to set the current line-width value in the
attribute list. This value is then used by line-drawing algorithms to control the thickness of lines
generated with subsequent output primitive commands
SetLinewidthScaleFactor(lw)
Line-width parameter lw is assigned a positive number to indicate the relative width of the line to be
displayed. A value of 1 specifies a standard width line. On a pen plotter, for instance, a user could set lw
to a value of 0.5 to plot a line whose width is half that of the standard line. Values greater than 1 produce
lines thicker than the standard.
We can draw lines with thickness greater than one pixel. To produce a thick line segment, we can draw
two lines in parallel along the thick-line edges. As we step along the line finding successive edge pixels,
we must turn ON all the pixels which lie between the boundaries.
To draw a line between (xa,ya) and (xb,yb) with thickness w, we would have a top boundary between the
points (xa,ya+wy) and (xb,yb+wy) and a lower boundary between(xa,ya+wy) and (xb,yb-wy) where wy is
given by,
( w 1) [( xb xa ) 2 ( yb ya )]1/ 2
wy
2 | xb xa |
Conclusion:
Thus, we have successfully studied to implement a program in C/C++ to draw a line with line style
(Thick, Thin, Dotted).
Assignment No:- B7
Aim:- Write C++ program to implement reflection of 2-D object about X axis, Y axis and about X=Y
axis
Theory:- Reflection
Reflection is the mirror image of original object. In other words, we can say that it is a rotation
operation with 180°. In reflection transformation, the size of the object does not change.The
following figures show reflections with respect to X and Y axes, and about the origin
respectively.
Assignment No: B8
Aim : Write program to implement Cohen Sutherland Hodgeman algorithm to clip any polygon. Provide
the vertices of the polygon to be clipped and pattern of clipping interactively.
Theory:
The Sutherland - Hodgman algorithm performs a clipping of a polygon against each window
edge in turn. It accepts an ordered sequence of verices v1, v2, v3, ..., vn and puts out a set of
vertices defining the clipped polygon.
The following figures show how this algorithm works at each edge, clipping the polygon.
As the algorithm goes around the edges of the window, clipping the polygon, it encounters four
types of edges. All four edge types are illustrated by the polygon in the following figure. For
each edge type, zero, one, or two vertices are added to the output list of vertices that define the
clipped polygon.
1. Edges that are totally inside the clip window. - add the second inside vertex point
2. Edges that are leaving the clip window. - add the intersection point as a vertex
3. Edges that are entirely outside the clip window. - add nothing to the vertex output list
4. Edges that are entering the clip window. - save the intersection and inside points as vertices
Assume that we're clipping a polgon's edge with vertices at (x1,y1) and (x2,y2) against a clip
window with vertices at (xmin, ymin) and (xmax,ymax).
The location (IX, IY) of the intersection of the edge with the left side of the window is:
i. IX = xmin
ii. IY = slope*(xmin-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The location of the intersection of the edge with the right side of the window is:
i. IX = xmax
ii. IY = slope*(xmax-x1) + y1, where the slope = (y2-y1)/(x2-x1)
The intersection of the polygon's edge with the top side of the window is:
Finally, the intersection of the edge with the bottom side of the window is:
Conclusion-
Thus we’ve understood polygon clipping algorithms and implemented them in laboratory
Assignment No: B9
Aim:- Write C++ program to implement painter’s algorithm for hidden surface removal..
Theory:-
Depth sorting method uses both image space and object-space operations. The depth-sorting
method performs two basic functions −
The scan conversion of the polygon surfaces is performed in image space. This method for
solving the hidden-surface problem is often referred to as the painter's algorithm. The
following figure shows the effect of depth sorting −
The algorithm begins by sorting by depth. For example, the initial “depth” estimate of a polygon
may be taken to be the closest z value of any vertex of the polygon.
Let us take the polygon P at the end of the list. Consider all polygons Q whose z-extents overlap
P’s. Before drawing P, we make the following tests. If any of the following tests is positive, then
we can assume P can be drawn before Q.
If all the tests fail, then we split either P or Q using the plane of the other. The new cut polygons
are inserting into the depth order and the process continues. Theoretically, this partitioning could
generate O(n2) individual polygons, but in practice, the number of polygons is much smaller.
Conclusion-
Thus we’ve understood painter’s algorithm for hidden surface removal and implemented them in
laboratory
Aim : Write C++ program to implement translation, shear, rotation and scaling transformations on
equilateral triangle and rhombus
Theory:
Translation
A translation moves an object to a different position on the screen. You can translate a point in
2D by adding translation coordinate (tx, ty) to the original coordinate (X, Y) to get the new
coordinate (X’, Y’).
X’ = X + tx
Y’ = Y + ty
The pair (tx, ty) is called the translation vector or shift vector. The above equations can also be
represented using the column vectors
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’).
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 (SX, SY), and the
produced coordinates are (X’, Y’). This can be mathematically represented as shown below −
The scaling factor SX, SY scales the object in X and Y direction respectively.
P’ = P . S
Where S is the scaling matrix. The scaling process is shown in the following figure.
If we provide values less than 1 to the scaling factor S, then we can reduce the size of the object.
If we provide values greater than 1, then we can increase the size of the object.
Shear
A transformation that slants the shape of an object is called the shear transformation. There are
two shear transformations X-Shear and Y-Shear. One shifts X coordinates values and other
shifts Y coordinate values. However; in both the cases only one coordinate changes its
coordinates and other preserves its values. Shearing is also termed as Skewing.
X-Shear
The X-Shear preserves the Y coordinate and changes are made to X coordinates, which causes
the vertical lines to tilt right or left as shown in below figure.
X' = X + Shx . Y
Y’ = Y
Y-Shear
The Y-Shear preserves the X coordinates and changes the Y coordinates which causes the
horizontal lines to transform into lines which slopes up or down as shown in the following
figure.
Y’ = Y + Shy . X
X’ = X
Conclusion-
Aim : Write C++/Java program to draw 3-D cube and perform following transformations on it using
OpenGL. a) Scaling b) Translation c) Rotation about one axis
Theory:
Rotation
3D rotation is not same as 2D rotation. In 3D rotation, we have to specify the angle of rotation
along with the axis of rotation. We can perform 3D rotation about X, Y, and Z axes. They are
represented in the matrix form as below –
Scaling
You can change the size of an object using scaling transformation. 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. The
following figure shows the effect of 3D scaling −
In 3D scaling operation, three coordinates are used. Let us assume that the original coordinates
are (X, Y, Z), scaling factors are (SX,SY,Sz)
respectively, and the produced coordinates are (X’, Y’, Z’). This can be mathematically
represented as shown below −
Conclusion-
Aim : Write C++/Java program to simulate any one of or similar scene-Clock with pendulum
Theory:
Outtextxy Function :
Circle Function
Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius
of the circle. The code given below draws a circle.
Line Function:
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2)
are end points of the line.The code given below draws a line.
Declaration :- void line(int x1, int y1, int x2, int y2);
Cleardevice Function:
Usleep Function
#include <unistd.h>
int usleep(useconds_t useconds);
The usleep() function shall cause the calling thread to be suspended from execution until either
the number of realtime microseconds specified by the argument useconds has elapsed or a signal
is delivered to the calling thread and its action is to invoke a signal-catching function or to
terminate the process.
Conclusion-