COMPUTER GRAPHICS AND MULTIMEDIA Unit 1
COMPUTER GRAPHICS AND MULTIMEDIA Unit 1
MULTIMEDIA
Shubhangi Krishna
INTRODUCTION
• Computer graphics is a field of computer
science, which deals with creation,
representation and management of images on
the computer screen.
• Computer graphics deals with the
technological and theoretical aspects of
computerized image synthesis.
• An image created by a computer can illustrate
a simple scene as well as complex scenes.
• Computer graphics covers many important
elements in the field of computer science, such
as the processing of images, interaction with
images, representation of images, and display of
images.
• Due to these important elements, today,
computer graphics have become an important
aspect of our daily lives.
• Computer graphics is used in almost all the fields
today, such as games, weather reports, or all
kinds of medical investigation and surgical
procedures.
IMAGE REPRESENTATION
• In computer graphics, a graphic object is
represented as a collection of discrete picture
elements, called pixels.
• Hence, a pixel is smallest addressable screen
element.
• It is the smallest piece of display screen which
can be controlled.
• This control is achieved by setting the intensity
and color of pixel, which compose the screen.
• Each pixel represent a region, which
theoretically can contain infinite no of points.
• But in general a point is represented by the
integer part of x coordinates, and y
coordinates.
• For example, P1 are represented by (4, 3),
irrespective of their exact values.
• There are special procedure which determine,
which pixel will provide best approximation to
the desired picture or graphic objects.
• The process of determining the appropriate
pixels for representing picture or graphic
object is called rasterization.
• The process of representing continuous
picture or graphic object as a collection of
discrete pixels is called scan conversion.
COMPUTER GRAPHICS VS COMPUTER VISION
y (x2,y2)
y=mx+c
y
x
(x1,y1) x
PROPERTIES OF LINE ALGORITHM
• Line should appear straight
• The lines must be generated parallel or at 45°
to the x and y-axes.
• Lines should terminate accurately.
• Line density is proportional to the no. of dots
displayed divided by the length of the line.
• To maintain constant density, dots should be
equally spaced.
• Line density should be independent of line
length and angle.
• Line should be drawn rapidly.
• There are three algorithms for line drawing:
– Direct use of line algorithm
– DDA
– Bresenham’s Algorithm
DIRECT USE OF LINE ALGORITHM
• Declare variables x1,x2,y1,y2,dx,dy,c,m
• Enter values of x1,x2,y1,y2.
• Calculate dx = x2- x1
• Calculate dy = y2-y1
• Calculate m = dy/dx
• Calculate c = y1-(m* x1)
• Set (x, y) equal to starting point
• xend=largest value of x
• If dx < 0
then x = x2
y = y2
xend= x1
If dx > 0
then x = x1
y = y1
xend= x2
• Check whether the complete line has been drawn
if x=xend
• Plot a point at current (x, y) coordinates
• Increment value of x, i.e., x = x+1
• Compute next value of y from equation y = mx + c
DDA ALGORITHM
• DDA stands for Digital Differential Analyzer.
• It is an incremental method of scan conversion
of line.
• In this method calculation is performed at each
step but by using results of previous steps.
• Plotting of same point twice is not possible as it
detects change in value.
• Faster method than direct line algorithm.
DDA ALGORITHM
• Take starting point as (x1,y1) and ends at (x2,y2)
• Calculate Δx and Δy
– Δx = x2-x1
– Δy = y2-y1
– m = Δy/Δx
• Set the initial point of the line as (x1,y1).
• 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).
• Plot the pixel at the calculated (x,y)
coordinate.
• Repeat previous steps (last two) until the
endpoint (x2,y2) is reached.
STEPS TO CALCULATE
1. Slope(m)= (y2-y1)/(x2-x1)
2. Find Δx and Δy.
3. Δx = Δy/m
DECISION PARAMETER
4. Δy= m Δx.
5. Case1: if | Δx|>=| Δy|
1. Assign Δx=1
2. x(i+1)=xi + 1
3. y(i+1)=yi+ m
6. Case2: if | Δx|<| Δy|
1. Assign Δy=1
2. x(i+1)=xi + 1/m
3. y(i+1)=yi+ 1
BRESENHAM’S ALGORITHM
• It is an efficient method because it involves
only integer addition, subtraction and
multiplication.
• These operations can be performed very
rapidly so lines can be generated quickly.
• It resolves the disadvantage of getting floating
values of x and y like in DDA.
• In this method, next pixel selected, is that one
which has least distance from true line
• The basic principle of Bresenham’s line
algorithm is to select the optimum raster
location to represent a straight line.
• To accomplish this, the algorithm always
increment either x or y by one unit, depending
on the slope of line.
• The increment in the other variable is
determined by examining the distance
between the actual line location and nearest
pixel.
• This distance is called decision variable.
BRESENHAM’S 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.]
5. i=1 [Initialize counter]
6. plot (x,y)
7. while(e>=0)
{
y=y+1;
x = x +1;
e= e-2*Δx + 2* Δy
}
8. else
9. x=x+1; e= e+ 2*Δy
10. i=i+1
11. if(i<= Δx) then go to step 6
12. STOP
STEPS TO CALCULATE
1. Slope m= (y2-y1)/(x2-x1)
2. Decision parametric p= 2Δy – Δx(for m<1)
3. Decision parametric p=2Δx- Δy(for m>=1)
4. If m<1
– p<0 - p>=0
– x(i+1)= xi+1 - x(i+1)=xi+1
– y(i+1)=yi - y(i+1)=yi+1
– p(i+1)=pi +2Δy - p(i+1)=pi+2Δy-2Δx
5. If m>=1
– p<0 - p>=0
– xi+1=xi - xi+1=xi+1
– yi+1=yi+1 - yi+1=yi+1
– pi+1=pi+2Δx - pi+1=pi+2Δx-2Δy
6. Value of Δx and Δy is always the initial calculate
one. It is not calculated every single time.
SCAN CONVERSION OF A CIRCLE
• Circle is an eight-way symmetric figure.
• The shape of circle is the same in all quadrants.
• In each quadrant, there are two octants.
• If the calculation of the point of one octant is done,
then the other seven points can be calculated
easily by using the concept of eight-way symmetry.
• The seven octants are calculated using property of
reflection.
• The reflection is accomplished by reversing x, y co-
ordinates.
1 2
8 3
7 4
6 5
QUADRANT 1
QUADRANT 4
(X,Y)
(-X,Y)
OCTANT 8 OCTANT 1
(-X,Y) (X,Y)
OCTANT 2
OCTANT 7
(Y,X)
(-Y,X)
OCTANT 6 OCTANT 3
(-Y,-X) (Y,-X)
OCTANT 5 OCTANT 4
(-X,-Y) (X,-Y)
QUADRANT 3 QUADRANT 2
(-X,-Y) (X,-Y)
MID-POINT CIRCLE ALGORITHM
• Coordinates of the points is calculated using
Pythagorean theorem:
– (x’)2+(y’)2=r2
• Decision parameter:
– 5/4-r
– Since 5/4 is almost equal to 1, for numerical
purposes take p= 1-r
1. Assume coordinates as (x, y) and radius=r
2. Find p=1-r and take starting points as x=0
and y=r
3. If p>=0
xnew= x+1
ynew= y-1
p= pold+2xnew-2ynew+1
4. If p<0
xnew=x+1
ynew=y
p= 2*xnew+1+pold
5. If the given centre point (X0, Y0) is not (0, 0),
then do the following and plot the point-
Xplot = Xnew + X0
Yplot = Ynew + Y0
6. Keep repeating Step-03 and Step-04 until
Xplot >= Yplot.
SCAN CONVERSION OF AN ELLIPSE
• Ellipse is defined as the geometric figure which is the set of
all points on a plane whose distance from two fixed points
remains a constant.
• It consists of two axes: major and minor axes where the
major axis is the longest diameter and minor axis is the
shortest diameter.
• The ellipse has four-way symmetry property which means
that only the quadrants are symmetric while the octants
are not.
• If the points for one quadrant is calculated then the points
for the remaining three can be calculated using the former
points.
• Equation of an ellipse:
• r y 2 x 2 + r x2 y 2 - r x2 r y 2 = 0
– where,
– ry : semi-minor axis
– rx : semi-major axis
• Slope of ellipse:
• m =dx / dy = - (2 ry2 x /2 rx2 y ) = -1
• In the ellipse each quadrant is divided into two
regions: R1 and R2 respectively.
• The region R1 has the value of slope as m<-1
while R2 has the value of slope as m > -1.
MID-POINT ELLIPSE ALGORITHM
• Step 1: Start
• Step 2: Declare rx , ry , x , y , m , dx , dy , P , P2.
• Step 3: Initialize initial point of region1 as
• x=0 , y = ry
• Step 4: Calculate P= ry2 + rx2 / 4 - ry rx2
• dx = 2 ry2 x
• dy = 2 rx2 y
• Step 5: Update values of dx and dy after each
iteration.
• Step 6: Repeat steps while (dx < dy):
Plot (x,y)
if(P < 0)
Update x = x+1 ;
P += ry2 [2x + 3 ]
Else
Update x = x + 1
y= y - 1
• Step 7: When dx ≥ dy, plot region 2:
• Step 8: Calculate P2 = ry2 ( x+1 / 2)2 + rx2 (y -1)2- rx2ry2
• Step 9: Repeat till (y > 0)
If (P2 > 0)
Update y = y-1 (x will remain same)
P2 =P2 -2 y rx2 + rx2
else
x = x+1
y = y-1
P2=P2+ 2 ry2 [2x] -2 y rx2 + rx2
• Step 10: End