SlideShare a Scribd company logo
1 of 51
Computer Graphics (CSE-457)
Lecture-1
1
Muhammad Ibrahim Khan, PhD
Professor, Dept. of CSE
Chittagong University of Engineering & Technology (CUET)
2
Chittagong University of Engineering & Technology (CUET)
Pixel
In digital image processing, a pixel, or pel or picture element is a
physical point in an image.
It is the smallest addressable element in a display device; so it is t
he smallest controllable element of a picture represented on the sc
reen.
The address of a pixel corresponds to its physical coordinates.
Each pixel is a sample of an original image; more samples typicall
y provide more accurate representations of the original. So picture
quality is directly proportional to the picture resolution.
3
Chittagong University of Engineering & Technology (CUET)
Pixel…
4
Chittagong University of Engineering & Technology (CUET)
Scan Conversion
Rasterisation (or rasterization) is the task of taking an
image described in a vector graphics format (shapes) and
converting it into a raster image (pixels or dots) for outp
ut on a video display or printer, or for storage in a bitma
p file format.
This is also known as scan conversion.
5
Chittagong University of Engineering & Technology (CUET)
Scan Conversion of a Point
• A point (x, y) within an image area, scan
converted to a pixel at location (x’, y’).
• x’ = Floor(x) and y’ = Floor(y).
• All points satisfying
and are mapped to
pixel (x’, y’).
• Point P1(1.7, 0.8) is represented by pixel
(1, 0) and points
are both represented by pixel (2, 1).
1




 x
x
x
1




 y
y
y
(2.8,1.9)
and
)
3
.
1
,
2
.
2
( 3
2 P
P
6
Chittagong University of Engineering & Technology (CUET)
Scan Conversion of a Point…
• Another approach is to align the integer values
in the co-ordinate system for (x, y) with the
pixel co-ordinates.
• Here x’ = Floor(x + 0.5) and y’ = Floor(y + 0.5)
• Points both are now represented
by pixel (2, 1) and by pixel (3, 2).
2
1 and P
P
3
P
7
Chittagong University of Engineering & Technology (CUET)
Line drawing algorithm
Need algorithm to figure out which intermediate pixels a
re on line path
Pixel (x, y) values constrained to integer values
Actual computed intermediate line values may be floats
Rounding may be required. Computed point
(10.48, 20.51) rounded to (10, 21)
Rounded pixel value is off actual line path (jaggy!!)
Sloped lines end up having jaggies
Vertical, horizontal lines, no jaggies
8
Chittagong University of Engineering & Technology (CUET)
Line Drawing Algorithm
Line: (3,2) -> (9,6)
? Which intermediate
pixels to turn on?
9
Chittagong University of Engineering & Technology (CUET)
Line Drawing Algorithm…
Slope-intercept line equation
y = mx + b
Given two end points (x0,y0), (x1, y1), how to compute
m and b?
(x0,y0)
(x1,y1)
dx
dy
0
1
0
1
x
x
y
y
dx
dy
m



 0
*
0 x
m
y
b 

10
Chittagong University of Engineering & Technology (CUET)
Line Drawing Algorithm…
Numerical example of finding slope m:
(Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
5392
.
0
102
55
23
125
41
96








Ax
Bx
Ay
By
m
11
Chittagong University of Engineering & Technology (CUET)
Digital Differential Analyzer (DDA): Line Drawing
Algorithm
(x0,y0)
(x1,y1)
dx
dy
Walk through the line, starting at (x0,y0)
Constrain x, y increments to values in [0,1] range
Case a: x is incrementing faster (m < 1)
Step in x=1 increments, compute and round y
Case b: y is incrementing faster (m > 1)
Step in y=1 increments, compute and round x
12
Chittagong University of Engineering & Technology (CUET)
DDA Line Drawing Algorithm (Case a: m < 1)
(x0, y0)
x = x0 + 1 y = y0 + 1 * m
Illuminate pixel (x, round(y))
x = x + 1 y = y + 1 * m
Illuminate pixel (x, round(y))
…
Until x == x1
(x1,y1)
x = x0 y = y0
Illuminate pixel (x, round(y))
m
y
y k
k 

1
13
Chittagong University of Engineering & Technology (CUET)
DDA Line Drawing Algorithm (Case b: m > 1)
y = y0 + 1 x = x0 + 1 * 1/m
Illuminate pixel (round(x), y)
y = y + 1 x = x + 1 /m
Illuminate pixel (round(x), y)
…
Until y == y1
x = x0 y = y0
Illuminate pixel (round(x), y)
(x1,y1)
(x0,y0)
m
x
x k
k
1
1 


14
Chittagong University of Engineering & Technology (CUET)
DDA Line Drawing Algorithm Pseudocode
compute m;
if m < 1:
{
float y = y0; // initial value
for(int x = x0;x <= x1; x++, y += m)
setPixel(x, round(y));
}
else // m > 1
{
float x = x0; // initial value
for(int y = y0;y <= y1; y++, x += 1/m)
setPixel(round(x), y);
}
Note: setPixel(x, y) writes current color into pixel in column x and row y in fram
e buffer
15
Chittagong University of Engineering & Technology (CUET)
DDA Example (Case a: m < 1)
Suppose we want to dra
w a line starting at pixel
(2,3) and ending at pixel
(12,8).
What are the values of t
he variables x and y at e
ach timestep?
What are the pixels colo
red, according to the D
DA algorithm?
t x y R(x) R(y)
0 2 3 2 3
1 3 3.5 3 4
2 4 4 4 4
3 5 4.5 5 5
4 6 5 6 5
5 7 5.5 7 6
6 8 6 8 6
7 9 6.5 9 7
8 10 7 10 7
9 11 7.5 11 8
10 12 8 12 8
16
Chittagong University of Engineering & Technology (CUET)
DDA Algorithm Drawbacks
DDA is the simplest line drawing algorithm
Not very efficient
Floating point operations and rounding operations are expensiv
e.
17
Chittagong University of Engineering & Technology (CUET)
The Bresenham Line Algorithm
The Bresenham algorithm is another incremental scan co
nversion algorithm
The big advantage of this algorithm is that it uses only in
teger calculations: integer addition, subtraction and multi
plication by 2, which can be accomplished by a simple a
rithmetic shift operation.
18
Chittagong University of Engineering & Technology (CUET)
The Big Idea
Move across the x axis in unit intervals and at each st
ep choose between two different y coordinates
2 3 4 5
2
4
3
5
For example, from
position (2, 3) we have to
choose between (3, 3) and
(3, 4)
We would like the point
that is closer to the
original line
(xk, yk)
(xk+1, yk)
(xk+1, yk+1)
19
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm
The y coordinate on the mathematical line at xk+1
is:
At sample position xk+1
the vertical separations
from the mathematical line
are labelled dupper and
dlower
b
x
m
y k 

 )
1
(
y
yk
yk+1
xk+1
dlower
dupper
20
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm…
So, dupper and dlower are given as follows:
and:
We can use these to make a simple decision about which
pixel is closer to the mathematical line
y
y
d k
upper 

 )
1
(
b
x
m
y k
k 



 )
1
(
1
k
lower y
y
d 

k
k y
b
x
m 


 )
1
(
21
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm…
This simple decision is based on the difference between the
two pixel positions:
Let’s substitute m with ∆y/∆x where ∆x and
∆y are the differences between the end-points:
1
2
2
)
1
(
2 




 b
y
x
m
d
d k
k
upper
lower
)
1
2
2
)
1
(
2
(
)
( 








 b
y
x
x
y
x
d
d
x k
k
upper
lower
)
1
2
(
2
2
2 









 b
x
y
y
x
x
y k
k
c
y
x
x
y k
k 





 2
2
22
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm…
So, a decision parameter pk for the kth step along a li
ne is given by:
The sign of the decision parameter pk is the same as
that of dlower – dupper
If pk is negative, then we choose the lower pixel,
otherwise we choose the upper pixel
c
y
x
x
y
d
d
x
p
k
k
upper
lower
k










2
2
)
(
23
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm…
Remember coordinate changes occur along the x axis
in unit steps so we can do everything with integer cal
culations
At step k+1 the decision parameter is given as:
Subtracting pk from this we get:
c
y
x
x
y
p k
k
k 





 

 1
1
1 2
2
)
(
2
)
(
2 1
1
1 k
k
k
k
k
k y
y
x
x
x
y
p
p 





 


24
Chittagong University of Engineering & Technology (CUET)
Deriving The Bresenham Line Algorithm…
But, xk+1 is the same as xk+1 so:
where yk+1 - yk is either 0 or 1 depending on the sign
of pk
The first decision parameter p0 is evaluated at
(x0, y0) is given as:
)
(
2
2 1
1 k
k
k
k y
y
x
y
p
p 




 

x
y
p 


 2
0
25
Chittagong University of Engineering & Technology (CUET)
The Bresenham Line Algorithm…
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1. Input the two line end-points, storing the left end-point in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get
the first value for the decision parameter as:
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:
x
y
p 


 2
0
y
p
p k
k 


 2
1
26
Chittagong University of Engineering & Technology (CUET)
The Bresenham Line Algorithm…
Otherwise, the next point to plot is (xk+1, yk+1) and:
5. Repeat step 4 (Δx – 1) times
x
y
p
p k
k 




 2
2
1
• The algorithm and derivation above assumes slopes are less
than 1. for other slopes we need to adjust the algorithm
slightly
27
Chittagong University of Engineering & Technology (CUET)
Bresenham’s Line Algorithm ( Example)
using Bresenham’s Line-Drawing Algorithm, Digitize the line wit
h endpoints (20,10) and (30,18).
y = 18 – 10 = 8,
x = 30 – 20 = 10
m = y / x = 0.8
2*y = 16
2*y – 2* x = -4
plot the first point (x0, y0) = (20, 10)
p0 = 2 * y – x = 2 * 8 – 10 = 6 , so the next point is (21, 11)
28
Chittagong University of Engineering & Technology (CUET)
Example (cont.)
K Pk (xk +1, yk +1) K Pk (xk +1, yk +1)
0 6 (21,11) 5 6 (26,15)
1 2 (22,12) 6 2 (27,16)
2 -2 (23,12) 7 -2 (28,16)
3 14 (24,13) 8 14 (29,17)
4 10 (25,14) 9 10 (30,18)
29
Chittagong University of Engineering & Technology (CUET)
Example (cont.)
30
Chittagong University of Engineering & Technology (CUET)
Bresenham’s Line Algorithm (cont.)
Notice that bresenham’s algorithm works on lines with slope in ra
nge 0 < m < 1.
We draw from left to right.
To draw lines with slope > 1, interchange the roles of x and y dire
ctions.
31
Chittagong University of Engineering & Technology (CUET)
Code (0 < slope < 1)
Bresenham ( int xA, yA, xB, yB) {
int d, dx, dy, xi, yi;
int incE, incNE;
dx = xB – xA;
dy = yB – yA;
incE = dy << 1; // Q
incNE = incE – dx << 1; // Q + R
d = incE – dx; // initial d = Q + R/2
xi = xA; yi = yA;
writePixel(xi, yi);
while(xi < xB) {
xi++;
if(d < 0) // choose E
d += incE;
else { // choose NE
d += incNE;
yi++;
}
writePixel(xi, yi);
}}
32
Chittagong University of Engineering & Technology (CUET)
Bresenham Line Algorithm Summary
The Bresenham line algorithm has the following advanta
ges:
An fast incremental algorithm
Uses only integer calculations
Comparing this to the DDA algorithm, DDA has the follo
wing problems:
Accumulation of round-off errors can make the pixel at end line
drift away from what was intended
The rounding operations and floating point arithmetic involved
are time consuming
33
Chittagong University of Engineering & Technology (CUET)
A Simple Circle Drawing Algorithm
The equation for a circle is:
where r is the radius of the circle
So, we can write a simple circle drawing algorithm by so
lving the equation for y at unit x intervals using:
2
2
2
r
y
x 

2
2
x
r
y 


34
Chittagong University of Engineering & Technology (CUET)
A Simple Circle Drawing Algorithm (cont…)
20
0
20 2
2
0 


y
20
1
20 2
2
1 


y
20
2
20 2
2
2 


y
6
19
20 2
2
19 


y
0
20
20 2
2
20 


y
35
Chittagong University of Engineering & Technology (CUET)
A Simple Circle Drawing Algorithm (cont…)
However, unsurprisingly this is not a brilliant solution!
Firstly, the resulting circle has large gaps where the slop
e approaches the vertical
Secondly, the calculations are not very efficient
The square (multiply) operations
The square root operation – try really hard to avoid these!
We need a more efficient, more accurate solution
36
Chittagong University of Engineering & Technology (CUET)
Eight-Way Symmetry
The first thing we can notice to make our circle drawing al
gorithm more efficient is that circles centred at (0, 0) have
eight-way symmetry
(x, y)
(y, x)
(y, -x)
(x, -y)
(-x, -y)
(-y, -x)
(-y, x)
(-x, y)
2
R
37
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm
Similarly to the case with lines, there is an incremental alg
orithm for drawing circles – the mid-point circle algorithm
In the mid-point circle algorithm we use eight-way symmet
ry so only ever calculate the points for the top right eighth
of a circle, and then use symmetry to get the rest of the poi
nts
38
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm (cont…)
Assume that we have
just plotted point (xk, yk)
The next point is a
choice between (xk+1, yk)
and (xk+1, yk-1)
We would like to choose
the point that is nearest to
the actual circle
So how do we make this choice?
(0, r)
39
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm (cont…)
Let’s re-jig the equation of the circle slightly to give us:
The equation evaluates as follows:
By evaluating this function at the midpoint between the
candidate pixels we can make our decision
2
2
2
)
,
( r
y
x
y
x
fcirc 










,
0
,
0
,
0
)
,
( y
x
fcirc
boundary
circle
the
inside
is
)
,
(
if y
x
boundary
circle
on the
is
)
,
(
if y
x
boundary
circle
the
outside
is
)
,
(
if y
x
40
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm (cont…)
Assuming we have just plotted the pixel at (xk,yk) so we
need to choose between (xk+1,yk) and (xk+1,yk-1)
Our decision variable can be defined as:
If pk < 0 the midpoint is inside the circle and and the pixel
at yk is closer to the circle
Otherwise the midpoint is outside and yk-1 is closer
2
2
2
)
2
1
(
)
1
(
)
2
1
,
1
(
r
y
x
y
x
f
p
k
k
k
k
circ
k








41
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm (cont…)
To ensure things are as efficient as possible we can do al
l of our calculations incrementally
First consider:
or:
where yk+1 is either yk or yk-1 depending on the sign of
pk
 
  2
2
1
2
1
1
1
2
1
]
1
)
1
[(
2
1
,
1
r
y
x
y
x
f
p
k
k
k
k
circ
k













1
)
(
)
(
)
1
(
2 1
2
2
1
1 






 

 k
k
k
k
k
k
k y
y
y
y
x
p
p
42
Chittagong University of Engineering & Technology (CUET)
Mid-Point Circle Algorithm (cont…)
The first decision variable is given as:
Then if pk < 0 then the next decision variable is given as
:
If pk > 0 then the decision variable is:
r
r
r
r
f
p circ








4
5
)
2
1
(
1
)
2
1
,
1
(
2
2
0
1
2 1
1 

 
 k
k
k x
p
p
1
2
1
2 1
1 



 
 k
k
k
k y
x
p
p
43
Chittagong University of Engineering & Technology (CUET)
Mid-point Circle Algorithm - Steps
1. Input radius r and circle center (xc, yc ). set the first point (x0 , y0 ) = (0, r ).
2. Calculate the initial value of the decision parameter as p0 = 1 – r.
(p0 = 5 /4 – r ≅ 1 – r )
3. If pk < 0,
plot (xk + 1, yk ) and pk+1 = pk + 2xk + 1 + 1,
Otherwise,
plot (xk+ 1, yk – 1 ) and pk+1 = pk + 2xk+1 + 1 – 2yk+1,
where 2xk + 1 = 2xk + 2 and 2yk + 1 = 2yk – 2.
44
Chittagong University of Engineering & Technology (CUET)
Mid-point Circle Algorithm - Steps
4. Determine symmetry points on the other seven octants.
5. Move each calculated pixel position (x, y) onto the circular path c
entered on (xc, yc) and plot the coordinate values: x = x + xc ,
y = y + yc
6. Repeat steps 3 though 5 until x  y.
7. For all points, add the center point (xc, yc )
45
Chittagong University of Engineering & Technology (CUET)
Mid-point Circle Algorithm - Steps
Now we drew a part from circle, to draw a complete circle, we must
plot the other points.
We have (xc + x , yc + y), the other points are:
(xc - x , yc + y)
(xc + x , yc - y)
(xc - x , yc - y)
(xc + y , yc + x)
(xc - y , yc + x)
(xc + y , yc - x)
(xc - y , yc - x)
46
Chittagong University of Engineering & Technology (CUET)
Mid-point circle algorithm (Example)
Given a circle radius r = 10, demonstrate the midpoint circle algorit
hm by determining positions along the circle octant in the first quad
rant from x = 0 to x = y.
Solution:
p0 =1 – r = – 9
Plot the initial point (x0, y0 ) = (0, 10),
2x0 = 0 and 2y0 =20.
Successive decision parameter values and positions along the circle
path are calculated using the midpoint method as appear in the next
table:
47
Chittagong University of Engineering & Technology (CUET)
Mid-point circle algorithm (Example)
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1
0 – 9 (1, 10) 2 20
1 – 6 (2, 10) 4 20
2 – 1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 – 3 (5, 9) 10 18
5 8 (6,8) 12 16
6 5 (7,7) 14 14
48
Chittagong University of Engineering & Technology (CUET)
Mid-point circle algorithm (Example)
49
Chittagong University of Engineering & Technology (CUET)
Mid-point Circle Algorithm – Example (2)
Given a circle radius r = 15, demonstrate the midpoint circle algor
ithm by determining positions along the circle octant in the first q
uadrant from x = 0 to x = y.
Solution:
p0 = 1 – r = – 14
plot the initial point (x0 , y0) = (0, 15),
2x0 = 0 and 2y0 = 30.
Successive decision parameter values and positions along the circl
e path are calculated using the midpoint method as:
50
Chittagong University of Engineering & Technology (CUET)
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1
0 – 14 (1, 15) 2 30
1 – 11 (2, 15) 4 30
2 – 6 (3, 15) 6 30
3 1 (4, 14) 8 28
4 – 18 (5, 14) 10 28
Mid-point Circle Algorithm – Example (2)
51
Chittagong University of Engineering & Technology (CUET)
Mid-point Circle Algorithm – Example (2)
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1
5 – 7 (6,14) 12 28
6 6 (7,13) 14 26
7 – 5 (8,13) 16 26
8 12 (9,12) 18 24
9 7 (10,11 ) 20 22
10 6 (11,10) 22 20

More Related Content

Similar to lecture 1.pptx

Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniyaTutorialsDuniya.com
 
Lecture _Line Scan Conversion.ppt
Lecture _Line Scan Conversion.pptLecture _Line Scan Conversion.ppt
Lecture _Line Scan Conversion.pptGaganvirKaur
 
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1aravindangc
 
Bresenham circlesandpolygons
Bresenham circlesandpolygonsBresenham circlesandpolygons
Bresenham circlesandpolygonsaa11bb11
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons dericationKumar
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsAmol Gaikwad
 
Lines and curves algorithms
Lines and curves algorithmsLines and curves algorithms
Lines and curves algorithmsMohammad Sadiq
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfRajJain516913
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualAnkit Kumar
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsKetan Jani
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsThirunavukarasu Mani
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptxAhmadAbba6
 
Notes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfNotes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfPranavRawat14
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2SanthiNivas
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer GraphicsKamal Acharya
 
Output Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptxOutput Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptxNaveenaKarthik3
 

Similar to lecture 1.pptx (20)

Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniya
 
Lecture _Line Scan Conversion.ppt
Lecture _Line Scan Conversion.pptLecture _Line Scan Conversion.ppt
Lecture _Line Scan Conversion.ppt
 
Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
 
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
 
Bresenham circlesandpolygons
Bresenham circlesandpolygonsBresenham circlesandpolygons
Bresenham circlesandpolygons
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
 
Lines and curves algorithms
Lines and curves algorithmsLines and curves algorithms
Lines and curves algorithms
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptx
 
Notes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdfNotes_456_Lines_Drawing2_4 (1).pdf
Notes_456_Lines_Drawing2_4 (1).pdf
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Computer Graphics Unit 2
Computer Graphics Unit 2Computer Graphics Unit 2
Computer Graphics Unit 2
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Output primitives in Computer Graphics
Output primitives in Computer GraphicsOutput primitives in Computer Graphics
Output primitives in Computer Graphics
 
Output Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptxOutput Primitive and Brenshamas Line.pptx
Output Primitive and Brenshamas Line.pptx
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 

lecture 1.pptx

  • 1. Computer Graphics (CSE-457) Lecture-1 1 Muhammad Ibrahim Khan, PhD Professor, Dept. of CSE Chittagong University of Engineering & Technology (CUET)
  • 2. 2 Chittagong University of Engineering & Technology (CUET) Pixel In digital image processing, a pixel, or pel or picture element is a physical point in an image. It is the smallest addressable element in a display device; so it is t he smallest controllable element of a picture represented on the sc reen. The address of a pixel corresponds to its physical coordinates. Each pixel is a sample of an original image; more samples typicall y provide more accurate representations of the original. So picture quality is directly proportional to the picture resolution.
  • 3. 3 Chittagong University of Engineering & Technology (CUET) Pixel…
  • 4. 4 Chittagong University of Engineering & Technology (CUET) Scan Conversion Rasterisation (or rasterization) is the task of taking an image described in a vector graphics format (shapes) and converting it into a raster image (pixels or dots) for outp ut on a video display or printer, or for storage in a bitma p file format. This is also known as scan conversion.
  • 5. 5 Chittagong University of Engineering & Technology (CUET) Scan Conversion of a Point • A point (x, y) within an image area, scan converted to a pixel at location (x’, y’). • x’ = Floor(x) and y’ = Floor(y). • All points satisfying and are mapped to pixel (x’, y’). • Point P1(1.7, 0.8) is represented by pixel (1, 0) and points are both represented by pixel (2, 1). 1      x x x 1      y y y (2.8,1.9) and ) 3 . 1 , 2 . 2 ( 3 2 P P
  • 6. 6 Chittagong University of Engineering & Technology (CUET) Scan Conversion of a Point… • Another approach is to align the integer values in the co-ordinate system for (x, y) with the pixel co-ordinates. • Here x’ = Floor(x + 0.5) and y’ = Floor(y + 0.5) • Points both are now represented by pixel (2, 1) and by pixel (3, 2). 2 1 and P P 3 P
  • 7. 7 Chittagong University of Engineering & Technology (CUET) Line drawing algorithm Need algorithm to figure out which intermediate pixels a re on line path Pixel (x, y) values constrained to integer values Actual computed intermediate line values may be floats Rounding may be required. Computed point (10.48, 20.51) rounded to (10, 21) Rounded pixel value is off actual line path (jaggy!!) Sloped lines end up having jaggies Vertical, horizontal lines, no jaggies
  • 8. 8 Chittagong University of Engineering & Technology (CUET) Line Drawing Algorithm Line: (3,2) -> (9,6) ? Which intermediate pixels to turn on?
  • 9. 9 Chittagong University of Engineering & Technology (CUET) Line Drawing Algorithm… Slope-intercept line equation y = mx + b Given two end points (x0,y0), (x1, y1), how to compute m and b? (x0,y0) (x1,y1) dx dy 0 1 0 1 x x y y dx dy m     0 * 0 x m y b  
  • 10. 10 Chittagong University of Engineering & Technology (CUET) Line Drawing Algorithm… Numerical example of finding slope m: (Ax, Ay) = (23, 41), (Bx, By) = (125, 96) 5392 . 0 102 55 23 125 41 96         Ax Bx Ay By m
  • 11. 11 Chittagong University of Engineering & Technology (CUET) Digital Differential Analyzer (DDA): Line Drawing Algorithm (x0,y0) (x1,y1) dx dy Walk through the line, starting at (x0,y0) Constrain x, y increments to values in [0,1] range Case a: x is incrementing faster (m < 1) Step in x=1 increments, compute and round y Case b: y is incrementing faster (m > 1) Step in y=1 increments, compute and round x
  • 12. 12 Chittagong University of Engineering & Technology (CUET) DDA Line Drawing Algorithm (Case a: m < 1) (x0, y0) x = x0 + 1 y = y0 + 1 * m Illuminate pixel (x, round(y)) x = x + 1 y = y + 1 * m Illuminate pixel (x, round(y)) … Until x == x1 (x1,y1) x = x0 y = y0 Illuminate pixel (x, round(y)) m y y k k   1
  • 13. 13 Chittagong University of Engineering & Technology (CUET) DDA Line Drawing Algorithm (Case b: m > 1) y = y0 + 1 x = x0 + 1 * 1/m Illuminate pixel (round(x), y) y = y + 1 x = x + 1 /m Illuminate pixel (round(x), y) … Until y == y1 x = x0 y = y0 Illuminate pixel (round(x), y) (x1,y1) (x0,y0) m x x k k 1 1   
  • 14. 14 Chittagong University of Engineering & Technology (CUET) DDA Line Drawing Algorithm Pseudocode compute m; if m < 1: { float y = y0; // initial value for(int x = x0;x <= x1; x++, y += m) setPixel(x, round(y)); } else // m > 1 { float x = x0; // initial value for(int y = y0;y <= y1; y++, x += 1/m) setPixel(round(x), y); } Note: setPixel(x, y) writes current color into pixel in column x and row y in fram e buffer
  • 15. 15 Chittagong University of Engineering & Technology (CUET) DDA Example (Case a: m < 1) Suppose we want to dra w a line starting at pixel (2,3) and ending at pixel (12,8). What are the values of t he variables x and y at e ach timestep? What are the pixels colo red, according to the D DA algorithm? t x y R(x) R(y) 0 2 3 2 3 1 3 3.5 3 4 2 4 4 4 4 3 5 4.5 5 5 4 6 5 6 5 5 7 5.5 7 6 6 8 6 8 6 7 9 6.5 9 7 8 10 7 10 7 9 11 7.5 11 8 10 12 8 12 8
  • 16. 16 Chittagong University of Engineering & Technology (CUET) DDA Algorithm Drawbacks DDA is the simplest line drawing algorithm Not very efficient Floating point operations and rounding operations are expensiv e.
  • 17. 17 Chittagong University of Engineering & Technology (CUET) The Bresenham Line Algorithm The Bresenham algorithm is another incremental scan co nversion algorithm The big advantage of this algorithm is that it uses only in teger calculations: integer addition, subtraction and multi plication by 2, which can be accomplished by a simple a rithmetic shift operation.
  • 18. 18 Chittagong University of Engineering & Technology (CUET) The Big Idea Move across the x axis in unit intervals and at each st ep choose between two different y coordinates 2 3 4 5 2 4 3 5 For example, from position (2, 3) we have to choose between (3, 3) and (3, 4) We would like the point that is closer to the original line (xk, yk) (xk+1, yk) (xk+1, yk+1)
  • 19. 19 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm The y coordinate on the mathematical line at xk+1 is: At sample position xk+1 the vertical separations from the mathematical line are labelled dupper and dlower b x m y k    ) 1 ( y yk yk+1 xk+1 dlower dupper
  • 20. 20 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm… So, dupper and dlower are given as follows: and: We can use these to make a simple decision about which pixel is closer to the mathematical line y y d k upper    ) 1 ( b x m y k k      ) 1 ( 1 k lower y y d   k k y b x m     ) 1 (
  • 21. 21 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm… This simple decision is based on the difference between the two pixel positions: Let’s substitute m with ∆y/∆x where ∆x and ∆y are the differences between the end-points: 1 2 2 ) 1 ( 2       b y x m d d k k upper lower ) 1 2 2 ) 1 ( 2 ( ) (           b y x x y x d d x k k upper lower ) 1 2 ( 2 2 2            b x y y x x y k k c y x x y k k        2 2
  • 22. 22 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm… So, a decision parameter pk for the kth step along a li ne is given by: The sign of the decision parameter pk is the same as that of dlower – dupper If pk is negative, then we choose the lower pixel, otherwise we choose the upper pixel c y x x y d d x p k k upper lower k           2 2 ) (
  • 23. 23 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm… Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer cal culations At step k+1 the decision parameter is given as: Subtracting pk from this we get: c y x x y p k k k           1 1 1 2 2 ) ( 2 ) ( 2 1 1 1 k k k k k k y y x x x y p p          
  • 24. 24 Chittagong University of Engineering & Technology (CUET) Deriving The Bresenham Line Algorithm… But, xk+1 is the same as xk+1 so: where yk+1 - yk is either 0 or 1 depending on the sign of pk The first decision parameter p0 is evaluated at (x0, y0) is given as: ) ( 2 2 1 1 k k k k y y x y p p         x y p     2 0
  • 25. 25 Chittagong University of Engineering & Technology (CUET) The Bresenham Line Algorithm… BRESENHAM’S LINE DRAWING ALGORITHM (for |m| < 1.0) 1. Input the two line end-points, storing the left end-point in (x0, y0) 2. Plot the point (x0, y0) 3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as: 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: x y p     2 0 y p p k k     2 1
  • 26. 26 Chittagong University of Engineering & Technology (CUET) The Bresenham Line Algorithm… Otherwise, the next point to plot is (xk+1, yk+1) and: 5. Repeat step 4 (Δx – 1) times x y p p k k       2 2 1 • The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly
  • 27. 27 Chittagong University of Engineering & Technology (CUET) Bresenham’s Line Algorithm ( Example) using Bresenham’s Line-Drawing Algorithm, Digitize the line wit h endpoints (20,10) and (30,18). y = 18 – 10 = 8, x = 30 – 20 = 10 m = y / x = 0.8 2*y = 16 2*y – 2* x = -4 plot the first point (x0, y0) = (20, 10) p0 = 2 * y – x = 2 * 8 – 10 = 6 , so the next point is (21, 11)
  • 28. 28 Chittagong University of Engineering & Technology (CUET) Example (cont.) K Pk (xk +1, yk +1) K Pk (xk +1, yk +1) 0 6 (21,11) 5 6 (26,15) 1 2 (22,12) 6 2 (27,16) 2 -2 (23,12) 7 -2 (28,16) 3 14 (24,13) 8 14 (29,17) 4 10 (25,14) 9 10 (30,18)
  • 29. 29 Chittagong University of Engineering & Technology (CUET) Example (cont.)
  • 30. 30 Chittagong University of Engineering & Technology (CUET) Bresenham’s Line Algorithm (cont.) Notice that bresenham’s algorithm works on lines with slope in ra nge 0 < m < 1. We draw from left to right. To draw lines with slope > 1, interchange the roles of x and y dire ctions.
  • 31. 31 Chittagong University of Engineering & Technology (CUET) Code (0 < slope < 1) Bresenham ( int xA, yA, xB, yB) { int d, dx, dy, xi, yi; int incE, incNE; dx = xB – xA; dy = yB – yA; incE = dy << 1; // Q incNE = incE – dx << 1; // Q + R d = incE – dx; // initial d = Q + R/2 xi = xA; yi = yA; writePixel(xi, yi); while(xi < xB) { xi++; if(d < 0) // choose E d += incE; else { // choose NE d += incNE; yi++; } writePixel(xi, yi); }}
  • 32. 32 Chittagong University of Engineering & Technology (CUET) Bresenham Line Algorithm Summary The Bresenham line algorithm has the following advanta ges: An fast incremental algorithm Uses only integer calculations Comparing this to the DDA algorithm, DDA has the follo wing problems: Accumulation of round-off errors can make the pixel at end line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming
  • 33. 33 Chittagong University of Engineering & Technology (CUET) A Simple Circle Drawing Algorithm The equation for a circle is: where r is the radius of the circle So, we can write a simple circle drawing algorithm by so lving the equation for y at unit x intervals using: 2 2 2 r y x   2 2 x r y   
  • 34. 34 Chittagong University of Engineering & Technology (CUET) A Simple Circle Drawing Algorithm (cont…) 20 0 20 2 2 0    y 20 1 20 2 2 1    y 20 2 20 2 2 2    y 6 19 20 2 2 19    y 0 20 20 2 2 20    y
  • 35. 35 Chittagong University of Engineering & Technology (CUET) A Simple Circle Drawing Algorithm (cont…) However, unsurprisingly this is not a brilliant solution! Firstly, the resulting circle has large gaps where the slop e approaches the vertical Secondly, the calculations are not very efficient The square (multiply) operations The square root operation – try really hard to avoid these! We need a more efficient, more accurate solution
  • 36. 36 Chittagong University of Engineering & Technology (CUET) Eight-Way Symmetry The first thing we can notice to make our circle drawing al gorithm more efficient is that circles centred at (0, 0) have eight-way symmetry (x, y) (y, x) (y, -x) (x, -y) (-x, -y) (-y, -x) (-y, x) (-x, y) 2 R
  • 37. 37 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm Similarly to the case with lines, there is an incremental alg orithm for drawing circles – the mid-point circle algorithm In the mid-point circle algorithm we use eight-way symmet ry so only ever calculate the points for the top right eighth of a circle, and then use symmetry to get the rest of the poi nts
  • 38. 38 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm (cont…) Assume that we have just plotted point (xk, yk) The next point is a choice between (xk+1, yk) and (xk+1, yk-1) We would like to choose the point that is nearest to the actual circle So how do we make this choice? (0, r)
  • 39. 39 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm (cont…) Let’s re-jig the equation of the circle slightly to give us: The equation evaluates as follows: By evaluating this function at the midpoint between the candidate pixels we can make our decision 2 2 2 ) , ( r y x y x fcirc            , 0 , 0 , 0 ) , ( y x fcirc boundary circle the inside is ) , ( if y x boundary circle on the is ) , ( if y x boundary circle the outside is ) , ( if y x
  • 40. 40 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm (cont…) Assuming we have just plotted the pixel at (xk,yk) so we need to choose between (xk+1,yk) and (xk+1,yk-1) Our decision variable can be defined as: If pk < 0 the midpoint is inside the circle and and the pixel at yk is closer to the circle Otherwise the midpoint is outside and yk-1 is closer 2 2 2 ) 2 1 ( ) 1 ( ) 2 1 , 1 ( r y x y x f p k k k k circ k        
  • 41. 41 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm (cont…) To ensure things are as efficient as possible we can do al l of our calculations incrementally First consider: or: where yk+1 is either yk or yk-1 depending on the sign of pk     2 2 1 2 1 1 1 2 1 ] 1 ) 1 [( 2 1 , 1 r y x y x f p k k k k circ k              1 ) ( ) ( ) 1 ( 2 1 2 2 1 1            k k k k k k k y y y y x p p
  • 42. 42 Chittagong University of Engineering & Technology (CUET) Mid-Point Circle Algorithm (cont…) The first decision variable is given as: Then if pk < 0 then the next decision variable is given as : If pk > 0 then the decision variable is: r r r r f p circ         4 5 ) 2 1 ( 1 ) 2 1 , 1 ( 2 2 0 1 2 1 1      k k k x p p 1 2 1 2 1 1        k k k k y x p p
  • 43. 43 Chittagong University of Engineering & Technology (CUET) Mid-point Circle Algorithm - Steps 1. Input radius r and circle center (xc, yc ). set the first point (x0 , y0 ) = (0, r ). 2. Calculate the initial value of the decision parameter as p0 = 1 – r. (p0 = 5 /4 – r ≅ 1 – r ) 3. If pk < 0, plot (xk + 1, yk ) and pk+1 = pk + 2xk + 1 + 1, Otherwise, plot (xk+ 1, yk – 1 ) and pk+1 = pk + 2xk+1 + 1 – 2yk+1, where 2xk + 1 = 2xk + 2 and 2yk + 1 = 2yk – 2.
  • 44. 44 Chittagong University of Engineering & Technology (CUET) Mid-point Circle Algorithm - Steps 4. Determine symmetry points on the other seven octants. 5. Move each calculated pixel position (x, y) onto the circular path c entered on (xc, yc) and plot the coordinate values: x = x + xc , y = y + yc 6. Repeat steps 3 though 5 until x  y. 7. For all points, add the center point (xc, yc )
  • 45. 45 Chittagong University of Engineering & Technology (CUET) Mid-point Circle Algorithm - Steps Now we drew a part from circle, to draw a complete circle, we must plot the other points. We have (xc + x , yc + y), the other points are: (xc - x , yc + y) (xc + x , yc - y) (xc - x , yc - y) (xc + y , yc + x) (xc - y , yc + x) (xc + y , yc - x) (xc - y , yc - x)
  • 46. 46 Chittagong University of Engineering & Technology (CUET) Mid-point circle algorithm (Example) Given a circle radius r = 10, demonstrate the midpoint circle algorit hm by determining positions along the circle octant in the first quad rant from x = 0 to x = y. Solution: p0 =1 – r = – 9 Plot the initial point (x0, y0 ) = (0, 10), 2x0 = 0 and 2y0 =20. Successive decision parameter values and positions along the circle path are calculated using the midpoint method as appear in the next table:
  • 47. 47 Chittagong University of Engineering & Technology (CUET) Mid-point circle algorithm (Example) K Pk (xk+1, yk+1) 2 xk+1 2 yk+1 0 – 9 (1, 10) 2 20 1 – 6 (2, 10) 4 20 2 – 1 (3, 10) 6 20 3 6 (4, 9) 8 18 4 – 3 (5, 9) 10 18 5 8 (6,8) 12 16 6 5 (7,7) 14 14
  • 48. 48 Chittagong University of Engineering & Technology (CUET) Mid-point circle algorithm (Example)
  • 49. 49 Chittagong University of Engineering & Technology (CUET) Mid-point Circle Algorithm – Example (2) Given a circle radius r = 15, demonstrate the midpoint circle algor ithm by determining positions along the circle octant in the first q uadrant from x = 0 to x = y. Solution: p0 = 1 – r = – 14 plot the initial point (x0 , y0) = (0, 15), 2x0 = 0 and 2y0 = 30. Successive decision parameter values and positions along the circl e path are calculated using the midpoint method as:
  • 50. 50 Chittagong University of Engineering & Technology (CUET) K Pk (xk+1, yk+1) 2 xk+1 2 yk+1 0 – 14 (1, 15) 2 30 1 – 11 (2, 15) 4 30 2 – 6 (3, 15) 6 30 3 1 (4, 14) 8 28 4 – 18 (5, 14) 10 28 Mid-point Circle Algorithm – Example (2)
  • 51. 51 Chittagong University of Engineering & Technology (CUET) Mid-point Circle Algorithm – Example (2) K Pk (xk+1, yk+1) 2 xk+1 2 yk+1 5 – 7 (6,14) 12 28 6 6 (7,13) 14 26 7 – 5 (8,13) 16 26 8 12 (9,12) 18 24 9 7 (10,11 ) 20 22 10 6 (11,10) 22 20