ADMAS UNIVERSITY
DEPARTMENT OF COMPUTER
SCIENCE
COMPUTER GRAPHICS
By: Getaneh T.
E-Mail: [email protected]
1
CHAPTER 3:
GEOMETRY AND DRAWING ALGORITHMS
2
DRAWING DIFFERENT OBJECTS
Geometric Representation of objects: is a representation
or creating of an objects in two dimensional space.
Different techniques used to draw an object
1. Scan Converting Lines
2. Scan Converting Circles
3. Scan Converting Ellipse
4. Scan Converting Polygons
5. Scan Converting Texts
3
…
The graphics.h header file
Will demonstrate in lab session….....
4
SCAN CONVERTING LINES
Line Drawing
First problem statement: Draw a line on a screen between two
points
Problem Statement
Given two points P and Q in the XY plane, both with integer
coordinates, determine which pixels on a screen should be
drawn in order to best approximate a unit-width line segment
starting at P and ending at Q
5
WHAT IS SCAN CONVERSION?
Final step of rasterization (the process of taking geometric
shapes and converting them into an array of pixels stored in the
framebuffer to be displayed) – converting from “random scan”
specification to a raster scan where we specify which pixels are
drawn based on a stored array of primitives.
Takes triangles (or higher-order primitives) and maps them to
pixels on screen
6
SCAN-CONVERTING A LINE: FINDING THE NEXT
PIXEL
Special cases:
Horizontal Line:
Draw pixel P and increment x coordinate value by 1 to get next
pixel.
Vertical Line:
Draw pixel P and increment y coordinate value by 1 to get next
pixel.
Diagonal Line:
Draw pixel P and increment both x and y coordinate by 1 to get next
pixel.
7
VERTICAL DISTANCE
Why can we use vertical distance as a
measure of which point (pixel center)
is closer?
… because vertical distance is
proportional to actual distance
Similar triangles show that true
distances to line (in blue) are directly
proportional to vertical distances to
line (in black) for each point
Therefore, point with smaller vertical
distance to line is closest to line
8
STRATEGY 1 – INCREMENTAL
ALGORITHM
9
STRATEGY 1 – INCREMENTAL ALGORITHM
10
STRATEGY 2 – MIDPOINT LINE
ALGORITHM
11
STRATEGY 2 – MIDPOINT LINE
ALGORITHM
NE pixel
Previous pixel Q
Midpoint M
E pixel
12
STRATEGY 2- MIDPOINT LINE
ALGORITHM
NE pixel
E pixel
For line shown, algorithm chooses
NE as next pixel.
Now, need to find a way to
calculate on which side of line
midpoint lies
13
GENERAL LINE EQUATION IN IMPLICIT
FORM
14
DECISION VARIABLE
15
INCREMENTING DECISION VARIABLE IF
E WAS CHOSEN:
16
IF NE WAS CHOSEN:
17
SUMMARY
18
EXAMPLE CODE
19
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
Created by Jack Bresenham worked for 27 years at
IBM before entering academia.
20
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
(xk+1, yk+1)
position (2, 3) we
4
have to choose
(xk, yk) between (3, 3) and
3 (3, 4)
(xk+1, yk)
2
We would like the
point that is closer to
21
2 3 4 5
the original line
DERIVING THE BRESENHAM LINE
ALGORITHM
At sample position xk+1 the
yk+1
vertical separations from the dupper
mathematical line are labelled y
dlower
dupper and dlower yk
xk+1
The y coordinate on the mathematical line at
xk+1 is:
y m( xk 1) b 22
DERIVING THE BRESENHAM LINE
ALGORITHM (CONT…)
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
23
DERIVING THE BRESENHAM LINE
ALGORITHM (CONT…)
This simple decision is based on the difference between the two pixel
positions:
d lower d upper 2m( xk 1) 2 yk 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 yk 2b 1)
x
2y xk 2x yk 2y x(2b 1)
24
2y xk 2x yk c
DERIVING THE BRESENHAM LINE
ALGORITHM (CONT…)
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
If pk is negative, then we choose the lower pixel,
otherwise we choose the upper pixel
25
DERIVING THE BRESENHAM LINE
ALGORITHM (CONT…)
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:
p 2y x
Subtracting pkkfrom
1 k 1
this we get:
2x yk 1 c
pk 1 pk 2y ( xk 1 xk ) 2x( yk 1 yk )
26
DERIVING THE BRESENHAM LINE
ALGORITHM (CONT…)
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
27
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
28
(xk+1, yk) and:
pk 1 pk 2y
THE BRESENHAM LINE ALGORITHM
(CONT…)
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2y 2x
5. Repeat step 4 (Δx – 1) times
.
29
BRESENHAM EXAMPLE
Let’s have a go at this
Let’s plot the line from (20, 10) to (30, 18)
First off calculate all of the constants:
Δx: 10
Δy: 8
2Δy: 16
2Δy - 2Δx: -4
Calculate the initial decision parameter p0:
p0 = 2Δy – Δx = 6
30
BRESENHAM EXAMPLE (CONT…)
18 k pk (xk+1,yk+1)
17 0
16 1
15 2
3
14
4
13
5
12
6
11
7
10 31
8
20 21 22 23 24 25 26 27 28 29 30 9
BRESENHAM EXERCISE
Go through the steps of the Bresenham line drawing
algorithm for a line going from (21,12) to (29,16)
32
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
33
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
34
A SIMPLE CIRCLE DRAWING
ALGORITHM (CONT…)
y0 20 2 0 2 20
y1 20 2 12 20
y2 20 2 2 2 20
y19 20 2 19 2 6
y20 20 2 20 2 0
35
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
36
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)
(-y, -x) (y, -x)
37
(-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
38
MID-POINT CIRCLE ALGORITHM
(CONT…)
Assume that we have
just plotted point (xk, yk) (xk, yk) (xk+1, yk)
The next point is a
choice between (xk+1, yk)
(xk+1, yk-1)
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?
39
MID-POINT CIRCLE ALGORITHM
(CONT…)
Let’s see 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
f circ ( 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
40
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
2 1
( xk 1) ( yk ) r 2 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 closer
41
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
or:
[( xk 1) 1] yk 1 1
2
2 r
2
2
pk 1 pk 2( xk 1) ( yk21 yk2 ) ( yk 1 yk ) 1
where yk+1 is either yk or yk-1 depending on the sign of
pk 42
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:
43
pk 1 pk 2 xk 1 1 2 yk 1
THE MID-POINT CIRCLE ALGORITHM
MID-POINT CIRCLE ALGORITHM
Input radius r and circle centre (xc, yc), then set the
coordinates for the first point on the circumference of a
circle centred on the origin as:
( x0 , y0 ) (0, r )
Calculate the initial value of the decision parameter as:
p0 5 r
4
Starting with k = 0 at each position xk, perform the
following test. If pk < 0, the next point along the circle
centred on (0, 0) is (xk+1, yk) and:
44
pk 1 pk 2 xk 1 1
THE MID-POINT CIRCLE ALGORITHM
(CONT…)
Otherwise the next point along the circle is (xk+1, yk-1) and:
pk 1 pk 2 xk 1 1 2 yk 1
4. Determine symmetry points in the other seven octants
5. Move each calculated pixel position (x, y) onto the circular
path centred at (xc, yc) to plot the coordinate values:
6. Repeat steps 3xto5xuntil
xcx >= yy y yc
45
MID-POINT CIRCLE ALGORITHM
EXAMPLE
To see the mid-point circle algorithm in action lets use it to
draw a circle centred at (0,0) with radius 10
46
MID-POINT CIRCLE ALGORITHM
EXAMPLE (CONT…)
10 pk
k (xk+1,yk+1) 2xk+1 2yk+1
9
8 0
7 1
6
5 2
4 3
3
2 4
1 5
0
47
6
0 1 2 3 4 5 6 7 8 9 10
MID-POINT CIRCLE ALGORITHM
EXERCISE
Use the mid-point circle algorithm to draw the circle
centred at (0,0) with radius 15
48
MID-POINT CIRCLE ALGORITHM
SUMMARY
The key insights in the mid-point circle algorithm are:
Eight-way symmetry can hugely reduce the work in drawing
a circle
Moving in unit steps along the x axis at each point along the
circle’s edge we need to choose between two possible y
coordinates
49