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

Line and Circle Drawing Algorithms

This document discusses computer graphics output primitives and algorithms for drawing basic 2D shapes like points, lines, and polygons. It describes how graphics are specified as intensities for pixel positions and how programming packages provide functions to describe scenes using geometric primitives. The document focuses on algorithms for drawing lines, including the digital differential analyzer (DDA) algorithm which incrementally calculates pixel positions for rasterized line drawing. It provides an example of using the DDA algorithm and pseudocode for its implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Line and Circle Drawing Algorithms

This document discusses computer graphics output primitives and algorithms for drawing basic 2D shapes like points, lines, and polygons. It describes how graphics are specified as intensities for pixel positions and how programming packages provide functions to describe scenes using geometric primitives. The document focuses on algorithms for drawing lines, including the digital differential analyzer (DDA) algorithm which incrementally calculates pixel positions for rasterized line drawing. It provides an example of using the DDA algorithm and pseudocode for its implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 57

Computer Graphics

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

Description of objects in terms of primitives and


attributes and converts them to the pixels on the
screen.
Primitives – what is to be generated
Attributes – how primitives are to be generated

3
(0,0)

Points CRT

(maxx,maxy)

The electron beam is turned on to illuminate the phosphor


at the selected location (x, y) where
0 ≤ x ≤ maxx
0 ≤ y ≤ maxy

 setpixel(x, y, intensity) – loads an intensity value into the


frame-buffer at (x, y).
 getpixel(x, y) – retrieves the current frame-buffer
intensity setting at position (x, y).
4
Lines
Analog devises, such as a random-scan display or a
vector plotter, display a straight line smoothly from one
endpoint to another. Linearly varying horizontal and
vertical deflection voltages are generated that are
proportional to the required changes in the x and y
directions to produce the smooth line.

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 °

-90° <  < -45° °


7
|m| = 1
y=x
m=1
c=0
y

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.




  
  
  
  

step through in x step through in y 12


The DDA Method

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

xinc = dx / (float) steps


yinc = dy/ (float) steps

setPixel(round(x), round(y));

for (int i=0; i<steps; i++)


{
x += xinc;
y += yinc;
setPixel(round(x), round(y));
} 19
}
 The accumulation of round off error in successive
additions of the floating point increment, can cause the
calculated pixel positions to drift away from the true line
path





  
  
  
   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

For a given value of x


one pixel lies at distance ti above the line, and
one pixel lies at distance si below the line 24
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

In this algorithm, we have to decide which


pixel position is closest to the scan line, by Jack Bresenham worked
using a decision parameter for 27 years at IBM
before entering
academia. Bresenham
developed his famous
algorithms at IBM in the
early 1960s
The Big Idea
 Move across the x axis in unit intervals and at each step choose
between two different y coordinates
For example, from position (2,
3) we have to choose between
(3, 3) and (3, 4)
We would like the point that is
closer to the original line

Below are some assumptions


to keep algorithm simple.
• We draw line from left to
(xk+1, yk) right.
x1 < x2 and y1< y2
•Slope of the line is between 0
2 3 4 5 and 1, so draw a line from
Deriving The Bresenham Line
Algorithm
At sample position xk+1 yk+1
dupper
the vertical separations y
from the mathematical line dlower
are labelled dupper and dlower yk

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

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
 2y  xk  2x  yk  2y  x(2b  1)

 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  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


if pk is positive, we choose the upper pixel
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:

Subtracting pk from this we get:


pk 1  2y  xk 1  2x  yk 1  c

pk 1  pk  2y ( xk 1  xk )  2x( yk 1  yk )
Deriving The Bresenham Line
Algorithm (cont…)
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
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  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
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1  pk  2y  2x
The Bresenham Line Algorithm
(cont…)

 The algorithm and derivation above assumes slopes are


less than 1. for other slopes we need to adjust the algorithm
slightly.
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
Example – Draw a line from (20,10) to (30,18)
(30,18)
dx = 10
dy = 8
initial decision d0 = 2dy – dx = 6
Also 2dy = 16, 2(dy – dx) = -4
(20,10)
i di (xi+1,yi+1)
19
0 6 (21,11)
18
1 2 (22,12)
2 -2 (23,12) 17

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

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
CIRCLES
Mod point circle Algorithm
 Mid point b/w 2 pixels
Example

You might also like