You are on page 1of 38

COMPUTER GRAPHICS

Dr. Soumi Ghosh


Module 1& 2
• Module I: Introduction to Graphics and Graphics Hardware System
• Application of computer graphics, Video Display Devices, Raster Scan
Display, Random Scan Display, Input Devices, Graphic Software and
graphics standards, Numerical based on Raster and Random scan display,
Frame buffer, Display processor.
• Module II: Output Primitives and Clipping operations
• Algorithms for drawing 2D Primitives lines (DDA and Bresenham‘s line
algorithm), circles (Bresenham‘s and midpoint circle algorithm), ellipses
(midpoint ellipse algorithm), Antialiasing (Supersampling method and
Area sampling method), Line clipping (cohen-sutherland algorithm), Curve
clipping algorithm, and polygon clipping with Sutherland Hodgeman
algorithm, Area fill algorithms for various graphics primitives: Scanline fill
algorithm, boundary fill algorithm, flood fill algorithm, Polygon
representation, various method of Polygon Inside test: Even-Odd method,
winding number method, Character generation techniques.
Line Drawing Algorithms
Line Drawing Algorithm
• DDA Line drawing algorithm
• Breshenham’s Line drawing algorithm (Also
called Antialiasing Algorithm)
• These Line drawing algorithms are used for
scan conversion of graphic object i.e.
Line.
Line Drawing Algorithm
• Scan conversion is a process to represent
graphics objects as a collection of
pixels.
• Various algorithm and mathematical
computations are available for this purpose.
• Rasterization-Process of determining which
pixels give the best approximation to a desired
line on the screen.
A line connects two points. It is a basic element in graphics. To draw a
line, you need two points between which you can draw a line. In the
following three algorithms, we refer the one point of line as X0,Y0 and the
second point of line as X1,Y1.

 But in the case of computer graphics we can not directly join any two
coordinate points, for that we should calculate intermediate point’s
coordinate and put a pixel for each intermediate point, of the desired
color with help of functions like putpixel(x, y, K) , where (x,y) is our co-
ordinate and K denotes some color.

DDA Algorithm

Digital Differential Analyzer 


DDA algorithm is the simple line generation algorithm which is explained
step by step here.
EQUATION OF LINE
y =mx + c where m is slope and c is intercept
Slope between X0,Y0 and  X1,Y1
m=(Y1-Y0)/(X1-X0)
or, m=dy/dx

Similarly, points are xk, yk and xk+1, yk+1


then slope???

m= (yk+1 –yk)/(xk+1 –xk)

DDA algorithm interpolates values in interval by computing for each


xi the equations xi = xi−1 + 1, yi = yi−1 + m,
where m is the slope of the line.
 This slope can be expressed in DDA as follows:
m= yend-ystart/xend-xstart
Lines with slope magnitude |m| <1, ∆y = m ∆x

∆x> ∆y P2

P1

Lines with slope magnitude |m| >1, ∆x = ∆y /m

∆y> ∆x
P2
Digital Differential Algorithm(DDA)
• Scan
Steps
P1 ( xa,ya) and P2 (xb,yb) are the two end points.

2.Horizontal and vertical differences between the


endpoint positions are computed & assigned to
two parameters namely dx and dy.

3.The difference with greater magnitude


determines the ‘value’ of increments to be done.
That means the number of times sampling has to
be done. This value is assigned to a parameter
called ‘steps’.
4. Starting from the 1st pixel ,we determine the
offset needed at each step to generate the
next pixel position along the line path. Call
the offset value as xnicrementand ynicrement.
The starting points are xa and ya.
Assign x =xa and y=ya
x= x+ xincr & y= y+ yincr

5. Loop through the process steps times, till


the last point xb, yb is reached.

P1 P2
(xa,ya) (xb,yb)
DDA ALGORITHM
Step 1 − Get the input of two end points (X0,Y0)(X0,Y0) and (X1,Y1)(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;
DDA ALGORITHM
Step 5 − Put the pixel by successfully incrementing x and y coordinates
accordingly
// Put pixel for each step
X = X0; Y = Y0;
and complete the drawing of the line.
for(int i=0; i<= Steps; i++)
{ x = x + Xincrement;
y = y + Yincrement;
putpixel(Round(x), Round(y), RED);
}
DDA Pseudo-code
// assume that slope is gentle
DDA(float x0, float x1, float y0, float y1) {
float x, y;
float xinc, yinc;
int numsteps;

numsteps = Round(x1) – Round(x0);


xinc = (x1 – x0) / numsteps;
yinc = (y1 – y0) / numsteps;
x = x0;
y = y0;
putpixel(Round(x),Round(y));

for (int i=0; i<numsteps; i++) {


x += xinc;
y += yinc;
putpixel(Round(x),Round(y));
}

}
DDA Example
• Suppose we want to draw a
line starting at pixel (2,3) and
numsteps = 12 – 2 = 10
ending at pixel (12,8).
xinc = 10/10 = 1.0 X1-X0= 10
// assume that slope is gentle M<=1 yinc = 5/10 = 0.5 Y1- y0= 5
DDA(float x0, float x1, float y0, float y1)
{ Iteration x y Round(x) Round(y)
float x, y; 0 2 3 2 3
float xinc, yinc;
int numsteps; 1 3 3.5 3 4
2 4 4 4 4
numsteps = Round(x1) – Round(x0);
xinc = (x1 – x0) / numsteps; 3 5 4.5 5 5
yinc = (y1 – y0) / numsteps; 4 6 5 6 5
x = x0;
y = y0; 5 7 5.5 7 6
putpixel(Round(x),Round(y) 6 8 6 8 6
);
7 9 6.5 9 7
for (int i=0; i<numsteps; i++) { 8 10 7 10 7
x += xinc;
y += yinc; 9 11 7.5 11 8
putpixel(Round(x),Round(y)); 10 12 8 12 8
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (15,4) and
numsteps =
ending at pixel (20,17).
xinc = X1-X0=
// assume that slope is gentle M>1 yinc = Y1- y0=
DDA(float x0, float x1, float y0, float y1)
{ Iteration x Round(x Round(y)
float x, y; )
y 0
float xinc, yinc;
int numsteps; 1
2
numsteps = Round(y1) – Round(y0);
xinc = (x1 – x0) / numsteps; 3
yinc = (y1 – y0) / numsteps; 4
x = x0;
y = y0; 5
putpixel(Round(x),Round(y) 6
);
7
for (int i=0; i<numsteps; i++) { 8
x += xinc;
y += yinc; 9
putpixel(Round(x),Round(y)); 10
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (6,3) and
numsteps = ?
ending at pixel (8,10).
xinc = ? X1-X0=
// assume that slope is gentle M>1 yinc = ? Y1-
DDA(float x0, float x1, float y0, float y1) y0=
{ Iteration x Round(y)
float x, y; Round(x
y 0
float xinc, yinc; )
int numsteps; 1
2
numsteps = Round(y1) – Round(y0);
xinc = (x1 – x0) / numsteps; 3
yinc = (y1 – y0) / numsteps; 4
x = x0;
y = y0; 5
putpixel(Round(x),Round(y) 6
);
7
for (int i=0; i<numsteps; i++) { 8
x += xinc;
y += yinc; 9
putpixel(Round(x),Round(y)); 10
}}
DDA Example
• Suppose we want to draw a
line starting at pixel (2,3) and
numsteps = ?
ending at pixel (5,12).
xinc = ? X1-X0=
yinc = ? Y1-
y0=
Iteration x y Round(x) Round(y)
0
1
2
3
4
5
6
7
8
9
10
DDA Algorithm (continued)

Y_inc

X_inc

This DDA algorithm suffers from two reasons.


 Floating point calculations and rounding operations are expensive and time
consuming.
• Due to limited precision and Rounding operations calculated points will not
be displayed at its original point.
Bresenham’s Algorithm

• Uses only integer calculations

• Requires less time to generate line due to


elimination of floating point calculations.
Bresenham’s Algorithm m<=1
1. Input the two line endpoints and store left endpoint as (x0,y0)
2. Pre-calculate the values dx, dy
3. Color pixel (x0,y0)
4. Let p0 = 2dy –dx, Here P0 is decision parameter which tells us which next
pixel will glow.
5. At each xk along the line, starting with k=0:

If pk<0, then the next point to plot is (xk + 1,yk),


and pk+1 = pk + 2dy

Otherwise, the next point to plot is (xk + 1, yk + 1),


and pk+1 = pk + 2dy – 2dx

6. Repeat Step-5 Until we does not reach up to final point.

Remember! The algorithm and derivation above assumes slopes are less
than or equal to 1. for other slopes we need to adjust the algorithm slightly.
Bresenham’s Algorithm Example
Where m<=1
• Suppose we want to draw a line starting at dx = 12 – 2 = 10 2dy = 10
pixel (2,3) and ending at pixel (12,8). dy = 8 – 3 = 5 2dy – 2dx = -10
p0 = 2dy – dx = 0
t p P(x) P(y)
Algorithm 0 0 2 3
1. Input the two line endpoints and 1 -10 3 4
store left endpoint as (x0,y0)
2. Pre-calculate the values dx, dy, 2 0 4 4
2dy and 2dy -dx 3 -10 5 5
3. Color pixel (x0,y0)
4 0 6 5
4. Let p0 = 2dy –dx
5. At each xk along the line, 5 -10 7 6
starting with k=0: 6 0 8 6
If pk<0, then the next point to plot is (xk + 1,yk), 7 -10 9 7
and pk+1 = pk + 2dy (Down pixel will be selected)
8 0 10 7
Otherwise, the next point to plot is (xk + 1, yk + 1),
and pk+1 = pk + 2dy – 2dx (Upper pixel will be 9 -10 11 8
selected) 10 0 12 8
6. Repeat Step-4 dx times
Anti-aliasing and
filtering
techniques
Anti-aliasing
• Anti-aliasing is a method of fooling the eye
that a jagged edge is really smooth.
• Due to low resolution aliasing effect will
occur, which can be removed by increasing
the screen resolution.

Jagged edges due to aliasing


Circle after applying antialiasing
Some more Examples
Reducing Aliasing
• By increasing Resolution

The aliasing effect can be minimized


by increasing resolution of the raster display.
Disadvantage of improving resolution

• More memory requirement (Size of frame buffer will become Large)


• More scan conversion time
Anti-aliasing Methods
• Super-sampling method or post filtering

• Area sampling or Pre filtering


Super-sampling Method
(Cont….)
• In this method every individual pixel is
subdivided in to sub-pixel.
• In this method we count the number of
pixel which are overlapped by the object.
• The intensity value of a pixel is the
average of the intensity values of all the
sampled sub-pixels with in that pixel.
• In this method every pixel on the screen
have different intensity.
Super-sampling for a line object
having Non-Zero width.
Super-sampling Method (Cont….)

• Pixel at upper right corner is assigned 7/9 because


seven of its nine-sub pixels are inside the object
area.

• Suppose the color of object is RED(1,0,0), and the


background color is light yellow (.5,.5,.5).

• At what intensity the pixel will glow?


(1 X 7/9 +.5 X 2/9, 0X7/9+ 0.5 X 2/9,
0X7/9+.5X2/9) R G B
Blending of background color and object color will occur
only in area of pixel where object overlaps.

Question-

1.What will be the intensity of center pixel?


Answer- (1 X 1+.5X0, 0X1+.5X0, 0X1+.5X0)
2.What will be the intensity of lower right side pixel?
Answer- (1X1/9 + .5X8/9, 0X1/9+.5X8/9, 0X1/9+.5X8/9)
Intensity Variation on pixels after Super sampling method
Write Formula for Blending of Colors for following Conditions
1. Object Background is (.5,.5,.5)
2.Object Color is (1,1,0)
Ans:
Write Formula for
Blending of Colors for
following Conditions
3. Object Background is
Area Sampling Method
• This figure shows how line with a non-Zero width have
different intensity value at each pixel on the screen
• In this Area sampling method intensity value
is determined according to area covered by the object.

90% 65%
15% 50%
Thank you!!

You might also like