0% found this document useful (0 votes)
56 views

Computer Graphics Lecture-1

The document summarizes key concepts related to computer graphics and digital image processing: 1) It defines a pixel as the smallest controllable element in a digital image, with its address corresponding to its physical coordinates. More pixels provide a more accurate representation of the original image. 2) Rasterization converts vector graphics into raster images by mapping graphical shapes to pixels for output or storage. 3) Line drawing algorithms like DDA and Bresenham's algorithm are used to determine which pixels along a line segment should be turned on when rasterizing a line. They incrementally step through integer pixel coordinates rather than using floating point values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Computer Graphics Lecture-1

The document summarizes key concepts related to computer graphics and digital image processing: 1) It defines a pixel as the smallest controllable element in a digital image, with its address corresponding to its physical coordinates. More pixels provide a more accurate representation of the original image. 2) Rasterization converts vector graphics into raster images by mapping graphical shapes to pixels for output or storage. 3) Line drawing algorithms like DDA and Bresenham's algorithm are used to determine which pixels along a line segment should be turned on when rasterizing a line. They incrementally step through integer pixel coordinates rather than using floating point values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Computer Graphics

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

• 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 x  x  x  1
and y   y  y  1 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).


P2 (2.2,1.3) and P3 (2.8,1.9)
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) P1 and P2
• Points P3 both are now
represented by pixel (2, 1) and by pixel
(3, 2).
Line drawing algorithm
• Need algorithm to figure out which intermediate pixels
are 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
Line Drawing Algorithm

Line: (3,2) -> (9,6)

? 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

Illuminate pixel (x, round(y))

x=x+1 y=y+1*m

Illuminate pixel (x, round(y))

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

Illuminate pixel (round(x), y)

y=y+1 x = x + 1 /m

Illuminate pixel (round(x), y)

(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)

draw a line starting at 0 2 3 2 3

pixel (2,3) and ending at 1 3 3.5 3 4

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

dlowery coordinate on the mathematical line at x +1


The k
is:
y  m( xk  1)  b
Deriving The Bresenham Line
Algorithm…
So, dupper and dlower are given as follows:

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 )
 2y  xk  2x  yk  c
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
Deriving The Bresenham Line
Algorithm…
Remember coordinate changes occur along the x axis
in unit steps so we can do everything with integer
calculations
At step k+1 the decision parameter is given as:
pk 1  2y  xk 1  2x  yk 1  c
Subtracting pk from this we get:

pk 1  pk  2y ( xk 1  xk )  2x( yk 1  yk )
Deriving The Bresenham Line
Algorithm…
But, xk+1 is the same as xk+1 so:
pk 1  pk  2y  2x( 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  2y  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  2y  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  2y
The Bresenham Line Algorithm…
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1  pk  2y  2x
5. Repeat step 4 (Δx – 1) times

• The algorithm and derivation above assumes slopes are less


than 1. for other slopes we need to adjust the algorithm
slightly
Bresenham’s Line Algorithm ( Example)
• using Bresenham’s Line-Drawing Algorithm, Digitize the line with
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)
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)
Example (cont.)
Bresenham’s Line Algorithm (cont.)

• Notice that bresenham’s algorithm works on lines with slope


in range 0 < m < 1.

• We draw from left to right.

• To draw lines with slope > 1, interchange the roles of x and y


directions.
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);
}}
Bresenham Line Algorithm Summary

• The Bresenham line algorithm has the


following advantages:
– An fast incremental algorithm
– Uses only integer calculations
• Comparing this to the DDA algorithm, DDA has
the following problems:
– Accumulation of round-off errors can make the
pixelated line drift away from what was intended
– The rounding operations and floating point
arithmetic involved are time consuming
A Simple Circle Drawing Algorithm
• The equation for a circle is:
x y r
2 2 2

• where r is the radius of the circle


• So, we can write a simple circle drawing
algorithm by solving the equation for y at unit
x intervals using:
y   r 2  x2
A Simple Circle Drawing Algorithm (cont…)

y0  202  0 2  20

y1  202  12  20

y2  202  2 2  20

y19  202  192  6

y20  202  202  0


A Simple Circle Drawing Algorithm (cont…)

• However, unsurprisingly this is not a brilliant


solution!
• Firstly, the resulting circle has large gaps
where the slope 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
Eight-Way Symmetry
• The first thing we can notice to make our circle
drawing algorithm more efficient is that circles
centred at (0, 0) have eight-way symmetry
(-x, y) (x, y)

(-y, x) (y, x)

R
2
(-y, -x) (y, -x)

(-x, -y) (x, -y)


Mid-Point Circle Algorithm
• Similarly to the case with lines, there is an
incremental algorithm for drawing circles – the
mid-point circle algorithm
• In the mid-point circle algorithm we use eight-
way symmetry so only ever calculate the
points for the top right eighth of a circle, and
then use symmetry to get the rest of the
points
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?
Mid-Point Circle Algorithm (cont…)
• Let’s re-jig the equation of the circle slightly to give us:
f circ ( x, y )  x 2  y 2  r 2
• The equation evaluates as follows:

0, if ( x, y ) is inside the circle boundary



fcirc(x, y) 0, if ( x, y ) is on the circle boundary
0, if ( x, y ) is outside the circle boundary

• By evaluating this function at the midpoint between
the candidate pixels we can make our decision
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:
pk  f circ ( xk  1, yk  1 )
2
 ( xk  1) 2  ( yk  1 ) 2  r 2
2
• 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
Mid-Point Circle Algorithm (cont…)
• To ensure things are as efficient as possible we can
do all of our calculations incrementally
• First consider:
 
pk 1  f circ xk 1  1, yk 1  1
2
  2
 [( xk  1)  1]  yk 1  1  r 2
2
2
• or:
pk 1  pk  2( xk  1)  ( yk 1  yk )  ( yk 1  yk )  1
2 2

• where yk+1 is either yk or yk-1 depending on the


sign of pk
Mid-Point Circle Algorithm (cont…)
• The first decision variable is given as:
p0  f circ (1, r  1 )
2
 1  (r  1 ) 2  r 2
2
 5 r
4
• Then if pk < 0 then the next decision variable is
given as: p  p  2x 1
k 1 k k 1

• If pk > 0 then the decision variable is:


p k 1  p k  2 x k 1  1  2 y k  1
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.

43
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
centered 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 )

44
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)

45
Mid-point circle algorithm (Example)

• Given a circle radius r = 10, demonstrate the midpoint circle


algorithm by determining positions along the circle octant in
the first quadrant 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:
Mid-point circle algorithm (Example)
K Pk )xk+1, yk+1( xk+1 2 yk+1 2
0 9– )10 ,1( 2 20
1 6– )10 ,2( 4 20
2 1– )10 ,3( 6 20
3 6 )9 ,4( 8 18
4 3– )9 ,5( 10 18
5 8 )6,8( 12 16
6 5 )7,7( 14 14
Mid-point circle algorithm (Example)
Mid-point Circle Algorithm – Example (2)

• Given a circle radius r = 15, demonstrate the midpoint circle


algorithm by determining positions along the circle octant in
the first quadrant 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
circle path are calculated using the midpoint method as:
Mid-point Circle Algorithm – Example (2)

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)

K Pk )xk+1, yk+1( xk+1 2 yk+1 2


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

You might also like