0% found this document useful (0 votes)
3 views33 pages

Lecture Two COMPUTER GRAPHICS

The document provides an overview of computer graphics, focusing on input-output devices, scan conversion, and line drawing algorithms. It discusses the definitions of graphics primitives, the importance of algorithms for efficient graphics rendering, and details various methods like DDA and Bresenham's algorithm for line drawing. Additionally, it highlights the advantages and disadvantages of these algorithms in terms of speed and accuracy.

Uploaded by

seyiafelister
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views33 pages

Lecture Two COMPUTER GRAPHICS

The document provides an overview of computer graphics, focusing on input-output devices, scan conversion, and line drawing algorithms. It discusses the definitions of graphics primitives, the importance of algorithms for efficient graphics rendering, and details various methods like DDA and Bresenham's algorithm for line drawing. Additionally, it highlights the advantages and disadvantages of these algorithms in terms of speed and accuracy.

Uploaded by

seyiafelister
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

COMPUTER GRAPHICS

BIT 2301 A/ BAC 3206


By
Dr. Edwin Omol
Assignment
• Prepare notes on:
– Input-Output Devices
1. Input Devices
2. Trackball
3. Light Pen
4. Image Scanner
– Output Devices
1. Plotters
SCAN CONVERSION A LINE
• CONTENTS
–Definition
–A point
–A Straight Line
–DDA Algorithm
–Bresenham’s Line Algorithm
DEFINITION
• The graphics objects are continuous. The pixels
used are discrete. Each pixel can have either on or
off state.
• The circuitry of the video display device of the
computer is capable of converting binary values
(0, 1) into a pixel on and pixel off information. 0 is
represented by pixel off. 1 is represented using
pixel on. Using this ability graphics computer
represent picture having discrete dots.
DEFINITION
 Several primitives can be used to represent real world
objects, e.g.:
 Point
 It is also called a pixel, pel or dot. It is the smallest
picture unit accepted by display devices.
 a location in space, 2D or 3D
 sometimes denotes one pixel
 Line
 straight path connecting two points
 infinitesimal width, consistent density
 beginning and end on points
DEFINITION
 Vertex
 point in 3D
 Edge
 line in 3D connecting two vertices
 Polygon/Face/Facet
 arbitrary shape formed by connected vertices
 fundamental unit of 3D computer graphics
 Mesh
 set of connected polygons forming a surface (or object)
SCAN CONVERSION
 AKA Rasterization
 Convert high-level geometry description to pixel colors in the frame
buffer
 Example: given vertex x,y coordinates determine pixel colors to
draw line
 Two ways to create an image:
 Scan existing photograph
 Procedurally compute values (rendering)

Viewport
Rasterization
Transformatio
n
ADVANTAGE OF DEVELOPING ALGORITHMS
FOR SCAN CONVERSION

1. Algorithms can generate graphics objects at a


faster rate.
2. Using algorithms memory can be used efficiently.
3. Algorithms can develop a higher level of graphical
objects.
SCAN CONVERTING A POINT
• Each pixel on the graphics display does not represent a mathematical point.
Instead, it means a region which theoretically can contain an infinite number of
points. Scan-Converting a point involves illuminating the pixel that contains the
point.
• Example: Display coordinates points as shown in fig would
both be represented by pixel (2, 1). In general, a point p (x, y) is represented by
the integer part of x & the integer part of y that is pixels [(INT (x), INT (y).
SCAN CONVERTING A STRAIGHT LINE
• A straight line may be defined by two endpoints & an equation. In fig the two
endpoints are described by (x1,y1) and (x2,y2). The equation of the line is used
to determine the x, y coordinates of all the points that lie between these two
endpoints.
• Using the equation of a straight line, y = mx + b where m = & b = the y
interrupt, we can find values of y by incrementing x from x =x1, to x = x2. By
scan-converting these calculated x, y values, we represent the line as a
sequence of pixels.
PROPERTIES OF GOOD LINE DRAWING
ALGORITHM
• Line should appear Straight: We must appropriate the line by
choosing addressable points close to it. If we choose well, the
line will appear straight, if not, we shall produce crossed lines.
• Lines should terminate accurately: Unless lines are plotted
accurately, they may terminate at the wrong place.
• Lines should have constant density: Line density is proportional
to the no. of dots displayed divided by the length of the line.
• Line should be drawn rapidly: This computation should be
performed by special-purpose hardware.
• Line density should be independent of line length and
angle: This can be done by computing an approximating line-
length estimate and to use a line-generation algorithm that keeps
line density constant to within the accuracy of this estimate.
ALGORITHM FOR LINE DRAWING
1. Direct use of line equation
2. DDA (Digital Differential Analyzer)
3. Bresenham's Algorithm
DIRECT USE OF LINE EQUATION
• It is the simplest form of conversion. First of all scan
P1 and P2 points. P1 has co-ordinates (x1',y1') and (x2'
y2' ).
• Then m = (y2',y1')/( x2',x1') and b =
• If value of |m|≤1 for each integer value of x. But do
not consider
• If value of |m|>1 for each integer value of y. But do
not consider
• Example: A line with starting point as (0, 0) and ending point (6, 18) is given. Calculate value of intermediate points and
slope of line.
• Solution: P1 (0,0) P7 (6,18)
x1=0
y1=0
x2=6
y2=18

• We know equation of line is


y =m x + b
y = 3x + b..............equation (1)
• put value of x from initial point in equation (1), i.e., (0, 0) x =0, y=0
0=3x0+b
0 = b ⟹ b=0
• put b = 0 in equation (1)
y = 3x + 0
y = 3x
• Now calculate intermediate points
Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15
Let x = 6 ⟹ y = 3 x 6 ⟹ y = 18
• So points are P1 (0,0)
P2 (1,3)
P3 (2,6)
P4 (3,9)
P5 (4,12)
P6 (5,15)
Algorithm for drawing line using equation
• Step1: Start Algorithm
• Step2: Declare variables x1,x2,y1,y2,dx,dy,m,b,
• Step3: Enter values of x1,x2,y1,y2.
The (x1,y1) are co-ordinates of a starting point of the line.
The (x2,y2) are co-ordinates of a ending point of the line.
• Step4: Calculate dx = x2- x1
• Step5: Calculate dy = y2-y1
• Step6: Calculate m = dy/dx
• Step7: Calculate b = y1-m* x1
• Step8: Set (x, y) equal to starting point, i.e., lowest point and x endequal to largest value of x.
• If dx < 0
then x = x2
y = y2
xend= x1
If dx > 0
then x = x1
y = y1
xend= x2
• Step9: Check whether the complete line has been drawn if x=x end, stop
• Step10: Plot a point at current (x, y) coordinates
• Step11: Increment value of x, i.e., x = x+1
• Step12: Compute next value of y from equation y = mx + b
• Step13: Go to Step9.
Program to draw a line using LineSlope
Method
OUTPUT:
Enter Starting and End Points Enter (X1, Y1, X2,
Y2) 200 100 300 200
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.
Suppose at step i, the pixels is (xi,yi)
The line of equation for step i
yi=mxi+b......................equation 1
Next value will be
yi+1=mxi+1+b.................equation 2
m = ∆y/ ∆x
yi+1-yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4
yi+1=yi+∆y
∆y=m∆x
yi+1=yi+m∆x
∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m
Case1: When |M|<1 then (assume that x1<x2)
x= x1,y=y1 set ∆x=1
yi+1=y1+m, x=x+1
Until x = x2</x
Case2: When |M|<1 then (assume that y1<y2)
x= x1,y=y1 set ∆y=1
xi+1= , , y=y+1
ADVANTAGE:
1. It is a faster method than method of using direct
use of line equation.
2. This method does not use multiplication theorem.
3. It allows us to detect the change in the value of x
and y ,so plotting of same point twice is not
possible.
4. This method gives overflow indication when a
point is repositioned.
5. It is an easy method because each step involves
just two additions.
DISADVANTAGE
1. It involves floating point additions rounding off is
done. Accumulations of round off error cause
accumulation of error.
2. Rounding off operations and floating point
operations consumes a lot of time.
3. It is more suitable for generating line using the
software. But it is less suited for hardware
implementation.
DDA Algorithm
Step1: Start Algorithm
Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.
Step3: Enter value of x1,y1,x2,y2.
Step4: Calculate dx = x2-x1
Step5: Calculate dy = y2-y1
Step6: If ABS (dx) > ABS (dy)
Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1
Step8: Set pixel (x, y)
Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))
Step10: Repeat step 9 until x = x2
• Example: If a line is drawn from (2, 3) to (6, 15)
with use of DDA. How many points will needed to
generate such line?
• Solution: P1 (2,3) P11 (6,15)
x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m=
For calculating next value of x takes x = x +
Symmetrical DDA
• The DDA works on the principle that we simultaneously increment x
and y by small steps proportional to the first derivatives of x and y. In
this case of a straight line, the first derivatives are constant and are
proportional to ∆x and ∆y . Therefore, we could generate a line by
incrementing x and y by ϵ ∆x and ϵ ∆y, where ϵ is some small
quantity. There are two ways to generate points
1. By rounding to the nearest integer after each incremental step, after
rounding we display dots at the resultant x and y.
2. An alternative to rounding the use of arithmetic overflow: x and y are
kept in registers that have two parts, integer and fractional. The
incrementing values, which are both less than unity, are repeatedly
added to the fractional parts and whenever the results overflows, the
corresponding integer part is incremented. The integer parts of the x
and y registers are used in plotting the line. In the case of the
symmetrical DDA, we choose ε=2-n,where 2n-1≤max (|∆x|,|∆y|)<2π
• Example: If a line is drawn from (0, 0) to (10, 5) with a symmetrical DDA
• 1. How many iterations are performed?
• 2. How many different points will be generated?
• Solutions: Given: P1 (0,0) P2 (10,5)
x1=0
y1=0
x2=10
y2=5
dx = 10 - 0 = 10
dy = 5 - 0 = 0

• P1 (0,0) will be considered starting points


P3 (1,0.5) point not plotted
P4 (2, 1) point plotted
P5 (3, 1.5) point not plotted
P6 (4,2) point plotted
P7 (5,2.5) point not plotted
P8 (6,3) point plotted
P9 (7,3.5) point not plotted
P10 (8, 4) point plotted
P11 (9,4.5) point not plotted
Bresenham's Line Algorithm
• This algorithm is used for scan converting a line. It was developed by
Bresenham. It is an efficient method because it involves only integer
addition, subtractions, and multiplication operations. These operations
can be performed very rapidly so lines can be generated quickly.
• In this method, next pixel selected is that one who has the least
distance from true line.
• The method works as follows:
• Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our
may to the night, one pixel position at a time in the horizontal direction
toward P2'(x2',y2').
• Once a pixel in choose at any step
The next pixel is
• Either the one to its right (lower-bound for the line)
• One top its right and up (upper-bound for the line)
• The line is best approximated by those pixels that
fall the least distance from the path between
P1',P2'.
To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1
The actual y coordinates of the line at x = xi+1is
y=mxi+1+b

The distance from S to the actual line in y direction


s = y-yi
The distance from T to the actual line in y direction
t = (yi+1)-y
Now consider the difference between these 2 distance values
s-t
When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1

Substituting m by and introducing decision variable


di=△x (s-t)
di=△x (2 (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c
Where c= 2△y+△x (2b-1)
We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)

Special Cases
If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x
If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y
Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]
Since mx1+b-yi=0 and m = , we have
ADVANTAGE
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it
does not use multiplication and division.
4. It is faster as compared to DDA (Digital Differential
Analyzer) because it does not involve floating point
calculations like DDA Algorithm.
DISADVANTAGE
1. This algorithm is meant for basic line drawing
only Initializing is not part of Bresenham's line
algorithm. So to draw smooth lines, you should
look into a different algorithm.
BRESENHAM'S LINE ALGORITHM
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
• Example: Starting and Ending position of the line
are (1, 1) and (8, 5). Find intermediate points.
Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1
x y d=d+I1 or I2

1 1 d+I2=1+(-6)=-5

2 2 d+I1=-5+8=3

3 2 d+I2=3+(-6)=-3

4 3 d+I1=-3+8=5

5 3 d+I2=5+(-6)=-1

6 4 d+I1=-1+8=7

7 4 d+I2=7+(-6)=1

8 5
Differentiate between DDA Algorithm and
Bresenham's Line Algorithm
DDA Algorithm Bresenham's Line Algorithm

1. DDA Algorithm use floating point, i.e., 1. Bresenham's Line Algorithm use fixed
Real Arithmetic. point, i.e., Integer Arithmetic

2. DDA Algorithms uses multiplication & 2.Bresenham's Line Algorithm uses only
division its operation subtraction and addition its operation

3. DDA Algorithm is slowly than 3. Bresenham's Algorithm is faster than


Bresenham's Line Algorithm in line DDA Algorithm in line because it involves
drawing because it uses real arithmetic only addition & subtraction in its
(Floating Point operation) calculation and uses only integer
arithmetic.

4. DDA Algorithm is not accurate and 4. Bresenham's Line Algorithm is more


efficient as Bresenham's Line Algorithm. accurate and efficient at DDA Algorithm.

5.DDA Algorithm can draw circle and 5. Bresenham's Line Algorithm can draw
curves but are not accurate as circle and curves with more accurate than
Bresenham's Line Algorithm DDA Algorithm.

You might also like