Computer Graphics Lecture-1
Computer Graphics Lecture-1
Lecture-1
1
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 the smallest controllable element of a picture represented
on the screen.
• The address of a pixel corresponds to its physical coordinates.
• Each pixel is a sample of an original image; more samples
typically provide more accurate representations of the
original. So picture quality is directly proportional to the
picture resolution.
Pixel…
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 output on a video
display or printer, or for storage in a bitmap
file format.
• This is also known as scan conversion.
Scan Conversion of a Point
? Which intermediate
pixels to turn on?
Line Drawing Algorithm…
Slope-intercept line equation
y = mx + b
Given two end points (x0,y0), (x1, y1), how to compute
m and b?
dy y1 y 0
m b y 0 m * x0
dx x1 x0
(x1,y1)
dy
(x0,y0)
dx
Line Drawing Algorithm…
Numerical example of finding slope m:
(Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
By Ay 96 41 55
m 0.5392
Bx Ax 125 23 102
Digital Differential Analyzer (DDA):
Line Drawing Algorithm
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
(x1,y1)
dy
(x0,y0)
dx
DDA Line Drawing Algorithm (Case
y y m
a: m < 1)
k 1 k x = x0 y = y0
Illuminate pixel (x, round(y))
(x1,y1)
x = x0 + 1 y = y0 + 1 * m
x=x+1 y=y+1*m
Until x == x1
(x0, y0)
DDA Line Drawing Algorithm (Case
b: m > 1)
1 x = x0 y = y0
x k 1 xk (x1,y1)
m Illuminate pixel (round(x), y)
y = y0 + 1 x = x0 + 1 * 1/m
y=y+1 x = x + 1 /m
(x0,y0) Until y == y1
DDA Line Drawing Algorithm
compute m; Pseudocode
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
frame buffer
DDA Example (Case a: m < 1)
Suppose we want to t x y R(x) R(y)
pixel (12,8). 2 4 4 4 4
3 5 4.5 5 5
What are the values of
4 6 5 6 5
the variables x and y at
5 7 5.5 7 6
each timestep? 6 8 6 8 6
What are the pixels 7 9 6.5 9 7
colored, according to 8 10 7 10 7
the DDA algorithm? 9 11 7.5 11 8
10 12 8 12 8
DDA Algorithm Drawbacks
• DDA is the simplest line drawing algorithm
– Not very efficient
– Floating point operations and rounding operations
are expensive.
The Bresenham Line Algorithm
• The Bresenham algorithm is another
incremental scan conversion algorithm
• The big advantage of this algorithm is that it
uses only integer calculations: integer
addition, subtraction and multiplication by 2,
which can be accomplished by a simple
arithmetic shift operation.
The Big Idea
Move across the x axis in unit intervals and at each
step choose between two different y coordinates
For example, from
5 position (2, 3) we have to
(xk+1, yk+1) choose between (3, 3) and
4 (3, 4)
(xk, yk)
We would like the point
3
(xk+1, yk) that is closer to the
2
original line
2 3 4 5
Deriving The Bresenham Line
Algorithm
At sample position xk+1 yk+1
dupper
the vertical separations y
from the dlower
yk
mathematical line are
labelled dupper and xk+1
d lower y yk
m( xk 1) b yk
and:
d upper ( yk 1) y
yk 1 m( xk 1) b
We can use these to make a simple decision about which
pixel is closer to the mathematical line
Deriving The Bresenham Line
Algorithm…
This simple decision is based on the difference between the
two pixel positions:
d lower d upper 2 m ( xk 1) 2 y k 2b 1
Let’s substitute m with ∆y/∆x where ∆x and
∆y are the differences between the end-points:
y
x ( d lower d upper ) x ( 2 ( xk 1) 2 y k 2b 1)
x
2 y xk 2 x y k 2 y x ( 2b 1)
2 y xk 2 x y k c
Deriving The Bresenham Line
Algorithm…
So, a decision parameter pk for the kth step along a
line is given by:
pk x(d lower d upper )
2y xk 2x yk c
The sign of the decision parameter pk is the same as
that of dlower – dupper
pk 1 pk 2y ( xk 1 xk ) 2x( yk 1 yk )
Deriving The Bresenham Line
Algorithm…
But, xk+1 is the same as xk+1 so:
pk 1 pk 2y 2x( yk 1 yk )
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:
p0 2y x
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:
p0 2y x
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: pk 1 pk 2y
The Bresenham Line Algorithm…
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2y 2x
5. Repeat step 4 (Δx – 1) times
• 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)
Example (cont.)
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)
Example (cont.)
Bresenham’s Line Algorithm (cont.)
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);
}}
Bresenham Line Algorithm Summary
y0 202 0 2 20
y1 202 12 20
y2 202 2 2 20
(-y, x) (y, x)
R
2
(-y, -x) (y, -x)
3. If pk < 0,
plot (xk + 1, yk ) and pk+1 = pk + 2xk + 1 + 1,
Otherwise,
43
Mid-point Circle Algorithm - Steps
5. Move each calculated pixel position (x, y) onto the circular path
centered on (xc, yc) and plot the coordinate values: x = x + xc ,
y = y + yc
44
Mid-point Circle Algorithm - Steps
45
Mid-point circle algorithm (Example)
Solution:
• p0 =1 – r = – 9
• Plot the initial point (x0, y0 ) = (0, 10),
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
circle path are calculated using the midpoint method as:
Mid-point Circle Algorithm – Example (2)
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)