Line and Circle Drawing Algorithms
Line and Circle Drawing Algorithms
Module II
Graphics Output Primitives
Basic Raster Algorithms
for 2D Primitives
Description of pictures
Specified by a set of intensities for the pixel positions.
Describe it as a set of complex objects, such as trees,
furniture and walls, positioned at specific coordinate
locations within the scene.
Graphics programming packages provide functions to
describe a scene in terms of basic geometric structures
referred to as output primitives.
• Points
• Straight lines
• Circles
• Splines curves and surfaces
• Polygon color areas
• Character strings
2
• Etc.
Implementing Application programs
3
(0,0)
Points CRT
(maxx,maxy)
5
Line Drawing Algorithms
Cartesian equation:
y = mx + c y2
where y1
m – slope x1 x2
c – y-intercept
y 2 y1 y
m
x2 x1 x
6
Slope
+ve -ve
if |m| = 1 45°
45°
= 45°
if |m| 1
-45° < < 45° °
°
if |m| 1
45° < < 90° or °
x y 8
0 0 7
1 1 6
2 2 5
3 3 4
4 4
3
5 5
2
6 6
1
7 7
0
8 8 x
0 1 2 3 4 5 6 7 8
8
|m| 1
y=½x+1
m=½
y
c=1
8
x y round(y)
7
0 1 1
1 1.5 2 6
2 2 2 5
3 2.5 3 4
4 3 3 3
5 3.5 4 2
6 4 4 1
7 4.5 5 0
8 5 5 0 1 2 3 4 5 6 7 8
x
9
|m| 1
y = 3x - 2
m=3
c = -2 y
8
x y round(y)
0 -2 -2 7
1 1 1 6
2 4 4 5
3 7 7 4
4 10 10 3
5 13 13 2
6 16 16 1
7 19 19 0
8 22 22 x
0 1 2 3 4 5 6 7 8
outside
10
The Digital Differential Analyzer
(DDA) Algorithm
y
m means that for a unit (1) change in x there is
x m-change in y.
x y Do not use
i.e. y = 3x + 1 m=3 y = 3x + 1
0 1 to calculate y.
1 4 Use m
2 7
3 10
4 13
5 16
x 1 means that for a unit (1) change in y there is
y m 1/m change in x.
11
The DDA Method
Uses differential equation of the line : m
If slope |m| 1 then increment x in steps of 1 pixel
and find corresponding y-values.
If slope |m| 1 then increment y in steps of 1 pixel
and find corresponding x-values.
Desired line
(xi+1,round(yi+m))
(xi,yi) (xi+1,yi+m)
(xi,round(yi))
13
Proceeding from right-endpoint to left-endpoint
if slope m 0
if |m| 1 if |m| 1
xi+1 = xi - 1 yi+1 = yi - 1
yi+1 = yi - m xi+1 = xi - 1/m
Right
Right
Left
14
Left
if slope m < 0
if |m| 1 if |m| 1
xi+1 = xi + 1 yi+1 = yi - 1
yi+1 = yi + m xi+1 = xi - 1/m
Left
Left
Right
15
Right
Proceeding from right-endpoint to left-endpoint
if slope m 0
if |m| 1 if |m| 1
xi+1 = xi - 1 yi+1 = yi + 1
yi+1 = yi - m xi+1 = xi + 1/m
Left
Left
Right
16
Right
Example (DDA)
0 m 1
y 13 x 1 xi 1 xi 1
y y 1
i 1 i 3
y
x y round(y)
0 1 1 8
1 4/3 1 7
2 5/3 2 6
5
3 2 2
4
4 7/3 2
3
5 8/3 3
2
6 3 3
1
7 10/3 3 0
8 11/3 4 x 17
0 1 2 3 4 5 6 7 8
Example (DDA)
m 1
y 3 x 8 yi 1 yi 1
xi 1 xi ( 13 )
y
y x round(x)
8 0 0 8
7 1/3 0 7
6 2/3 1 6
5
5 1 1
4
4 4/3 1
3
3 5/3 2
2
2 2 2
1
1 7/3 2 0
0 8/3 3 x 18
0 1 2 3 4 5 6 7 8
void LineDDA(int xa, int ya, int xb, int yb)
{
int dx = xb – xa, dy = yb – ya, steps;
float xinc, yinc, x=xa, y=ya ;
if (abs(dx)>abs(dy))
steps = abs(dx);
else
steps = abs(dy);
setPixel(round(x), round(y));
20
Advantage :
It’s the faster method to find slope of the line
Disadvantage :
– Rounding off floating number is time
consuming
21
Draw a line from (2,3) to (12,8)
Draw a line from (1,6) to (11,0)
Example
Draw a line from point (2,1) to (12,6)
Draw a line from point (1,6) to (11,0)
7
0 1 2 3 4 5 6 7 8 9 10 11 12
23
Bresenham Line Algorithm
True line
ti
si
xk+1
The y coordinate on the mathematical line at xk+1 is:
y m( xk 1) b
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
Deriving The Bresenham Line
Algorithm (cont…)
2 y x k 2 x y k c
Deriving The Bresenham Line
Algorithm (cont…)
So, a decision parameter pk for the kth step along a line is
given by:
pk 1 pk 2y ( xk 1 xk ) 2x( yk 1 yk )
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
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1.
The Bresenham Line Algorithm
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
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1 pk 2y 2x
The Bresenham Line Algorithm
(cont…)
3 14 (24,13) 16
4 10 (25,14) 15
5 6 (26,15) 14
6 2 (27,16) 13
7 -2 (28,16)
12
8 14 (29,17)
11
9 10 (30,18)
10
20 21 22 23 24 25 26 27 28 29 30 31 32 36
Bresenham Example (cont…)
18 k pk (xk+1,yk+1)
17 0
16 1
15 2
14 3
13 4
5
12
6
11
7
10
8
20 21 22 23 24 25 26 27 28 29 30
9
Bresenham Line Algorithm Summary