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

Computer Graphics (CSE-435) : Lecture-1

This document provides an overview of pixel concepts and line drawing algorithms in computer graphics. It discusses how pixels are the smallest addressable elements in an image and how rasterization converts vector graphics to pixels. It then covers the Digital Differential Analyzer (DDA) and Bresenham's line drawing algorithms. The DDA walks through a line incrementing x and y coordinates by rounding fractional values. Bresenham's algorithm uses only integer calculations to determine which pixel is closer to the mathematical line at each step along the line path.
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)
64 views

Computer Graphics (CSE-435) : Lecture-1

This document provides an overview of pixel concepts and line drawing algorithms in computer graphics. It discusses how pixels are the smallest addressable elements in an image and how rasterization converts vector graphics to pixels. It then covers the Digital Differential Analyzer (DDA) and Bresenham's line drawing algorithms. The DDA walks through a line incrementing x and y coordinates by rounding fractional values. Bresenham's algorithm uses only integer calculations to determine which pixel is closer to the mathematical line at each step along the line path.
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/ 51

Computer Graphics (CSE-435)

Lecture-1

Habibur Rahaman

Lecturer, Dept. of CSE


Chittagong Independent University (CIU)
1
Pixel
• In digital image processing, a pixel, 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 P2 (2.2,1.3) and P3 (2.8,1

are both represented by pixel (2, 1).


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 P1 and P2both are now
represented by pixel (2, 1) and P 3 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)
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 b  y 0  m * x0
m 
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) x = x0 y = y0
k 1 k
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(y)

draw a line starting at 0 2 3 3

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

pixel (12,8). 2 4 4 4
3 5 4.5 5
What are the values of
4 6 5 5
the variables x and y at
5 7 5.5 6
each timestep? 6 8 6 6
What are the pixels 7 9 6.5 7
colored, according to 8 10 7 7
the DDA algorithm? 9 11 7.5 8
10 12 8 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
dlower
The y coordinate on the mathematical line at xk+1
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
pk 1  pk  2y
(xk+1, yk) and:
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
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.)
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:
x2  y 2  r 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 x 2 2
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 eight of a circle, and then use
symmetry to get the rest of the points
Mid-Point Circle Algorithm (cont…)
(x(xk,k+1,
• Assume that we have (0, r) yyk)k)
just plotted point (xk, yk) (xk+1, yk-
1)
• 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…)
(xk, yk) (xk+1, yk)
(0, r)

(xk+1, yk-1)
Mid-Point Circle Algorithm (cont…)
• Let’s re-jig the equation of the circle slightly to
give us: f circ ( x, y )  x  y  r
2 2 2

The equation evaluates as follows:


0, if ( x, y ) is outside the circle boundary

fcirc(x, y) 0, if ( x, y ) is on the circle boundary
0,
 if ( x, y ) is inside 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


 [( xk  1)  1]  yk 1  1
2
2 2
 r2

• or: pk 1  pk  2( xk  1)  ( yk21  yk2 )  ( yk 1  yk )  1

• 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:
pk 1  pk  2 xk 1  1

• If pk > 0 then the decision variable is:


pk 1  pk  2 xk 1  1  2 yk  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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy