0% found this document useful (0 votes)
15 views33 pages

My Line and Circle

The document discusses scan conversion, a process that represents continuous graphical objects as discrete pixels, focusing on line and circle drawing algorithms. It details the Digital Differential Analyzer (DDA) and Bresenham's algorithm for line drawing, highlighting their methods, advantages, and drawbacks. Additionally, it covers circle drawing techniques, including explicit and parametric representations, and emphasizes the need for optimization in graphical computations.

Uploaded by

Anurup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views33 pages

My Line and Circle

The document discusses scan conversion, a process that represents continuous graphical objects as discrete pixels, focusing on line and circle drawing algorithms. It details the Digital Differential Analyzer (DDA) and Bresenham's algorithm for line drawing, highlighting their methods, advantages, and drawbacks. Additionally, it covers circle drawing techniques, including explicit and parametric representations, and emphasizes the need for optimization in graphical computations.

Uploaded by

Anurup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Computer Graphic

Scan Conversion
Line and Circle
Scan Conversion

The process of representing a continuous graphical objects as a


collection of discrete pixels by identifying their locations and
setting them On is called scan conversion
 The process of determining which pixels will provide the best
approximation to the desired line is properly known as
rasterization.

 Combined with the process of rendering the picture in scan line


order it is known as scan conversion.
Points and Line
 Points:
o A point in two dimensional space is given as an
ordered pair (x, y)
o In three dimensions a point is given as an
ordered triple (x, y, z)
 Lines: A line is defined using a start point
and an end-point
o In 2d: (xstart, ystart) to (xend, yend)
o In 3d: (xstart, ystart , zstart) to (xend, yend , zend)
y

(2, 7) (6, 7)

The line from


(2, 7) to (7, 3)
(2, 3) (7, 3)

(7, 1)

x
Simple Straight Line Equation

 The slope-intercept
Equation of a line is:


y m x  b
where:
y y1  y0
m 
x x1  x0
b  y0  m x0
 The equation of the line gives us the corresponding y point for every
x point
Example: Let’s draw a portion of the line given by the equation:
3 4
y  x
5 5
3 4
y (2)  2  2
5 5
3 4 3 y
y (3)  3  2 5
5 5 5
3 4 1
y (4)  4  3
5 5 5
3 4 4 2
y (5)  5  3
5 5 5
3 4 2 2 3 4 5 6 7
x
y (6)  6  4
5 5 5
3 4
y (7)  7  5
5 5
Digital Differential Analyser ( DDA) for a line
Let endpoints coordinates are (x1,y1 ) and (x2,y2) respectively , so
we have y y2  y1
m 
x x2  x1
1
y mx or x  y
m
A very simple procedure for scan converting a straight line is given as
follows:

1. Plot the first pixel at (x,y)= (x1,y1 )


2. increment the x- and y-values by dx and dy respectively to get the
next pixel . We consider either dx or dy to be unity and compute the
other . Plot pixel at (x+dx,y+dy)
if dx=1 the yi 1  yi  m
1
Else dy=1 xi 1  xi 
m
3. Continue the above steps until the final (x2,y2) is reached
DDA Algorithm for a line (x1,y1,x2,y2)
Begin
dx=x2-x1, dy=y2-y1;
if |dx|>|dy| then
steps=|dx|
else steps=|dy|
Xinc=dx/steps
Yinc=dy/steps
x=x1; y=y1;
setpixel(x1,y1);
for k=1 to steps do
begin x=x+Xinc; y=y+Yinc;
setpixel (round(x),round(y));
end
end /*of DDA/
Example1: Trace the DDA algorithm for drawing a line segment from (2,3)
to (8,7)
solution : dx=8-2=6 dy=7-3=4

|dx|>|dy| then steps=|dx|=6


Xinc=dx/steps=1
Yinc=dy/steps =0.67
now starting with the point (2,3) ,generate the next points in sequence to
reach the point (8,7). the computational steps are shown in table below
Xold Yold Xnew Ynew Round(x) Round(y)

2 3 3 3.67 3 4

3 3.67 4 4.34 4 4

4 4.34 5 5.01 5 5

5 5.01 6 5.68 6 6

6 5.68 7 6.35 7 6

7 6.35 8 7.02 8 7
Example1: Trace the DDA algorithm for drawing a line segment from (2,3)
to (8,10)
solution : dx=8-2=6 dy=10-3=7
|dy|>|dx| then steps=|dy|=7
Xinc=dx/steps=0.86
Yinc=dy/steps =1
now starting with the point (2,3) ,generate the next points in sequence to
reach the point (8,10). the computational steps are shown in table below

Xold Yold Xnew Ynew Round(x) Round(y)

2 3 2.86 4 3 4

2.86 4 3.72 5 4 5

3.72 5 4.58 6 5 6

4.58 6 5.44 7 5 7

5.44 7 6.3 8 6 8

6.3 8 7.16 9 7 9

7.16 9 8.02 10 8 10
Line Drawing Algorithm Drawbacks

DDA is the simplest line drawing algorithm but


 Not very efficient
 Round operation is expensive
Optimized algorithms typically used.
Integer DDA
E.g.Bresenham’s algorithm
Bresenham’s algorithm

1. Uses only integer operations and does not use


multiplication or division.
2. Algorithm always increments by one unit in
either X or Y depending on the slope of the line.
3. The increment in the other variable, either zero
or one, is determined by examining the distance
(error) between the actual line location and the
nearest grid locations.
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)
4
and (3, 4)
 We would like the point (xk, yk)
3
that is closer to the
(xk+1, yk)
original line
2

2 3 4 5
Deriving The Bresenham Line Algorithm

 At sample position xk+1 the yk+1


vertical separations from the dupper
y
mathematical line are labelled dlower
yk
dupper and dlower
xk+1
 The y coordinate on the mathematical line at xk+1 is:

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

5. Repeat step 4 (Δx ) times


Bresenham Algorithm
Besgin:

dx  x2  x1 ;
dy  y 2  y1 ;
x  x1 ; y  y1 ; x f  x2 ; y f  y 2
dy
slope 
dx
If(slope>1)
Begin
temp=dx;dx=dy;dy=temp
temp=x;x=y;y=temp
temp=xf;xf=yf;yf=temp
End
P=2dy-dx
Setpixel(x,y)
For k=1 to dx do
Begin
If(p<0) then
Begin
If (x<xf) then x=x+1;
Else x=x-1; p=p+2dy
End
Else
Begin if(y<yf) then y=y+1
else y=y-1
If (x<xf) then x=x+1;
else x=x-1; p=p+2(dy-dx);
End
If (slope>1) then setpixel(y,x)
Else setpixel(x,y)
Example 3: Trace the Bresenham’s algorithm for
drawing the line segment from (2,3) to (8,7)
P0=2dy-dx=2*4-6=2; dx=6; dy=4
Pk<0pk+1=pk+2dy;xk+1=xk+1;yk+1=yk
Pk>=0pk+1=pk+2dy-2dx; xk+1=xk+1; yk+1=yk+1

k xk yk pk Pk+1 Xk+1 Yk+1


0 2 3 2 -2 3 4
1 3 4 -2 6 4 4
2 4 4 6 2 5 5
3 5 5 2 -2 6 6
4 6 6 -2 6 7 6
5 7 6 6 2 8 7
6 8 7 2 -2 9 8
Drawing Circles
1. Using Explicit Representation
2. Using Parametric Representation
3. Midpoint Circle Drawing Algorithm
1-Drawing Circles Using Explicit Representation
(x  xc) 2  (y  yc)2 r 2
y yc  r 2  (x  xc) 2
 Very simple
 Considerable computation at each step.

 The spacing between plotted pixel


positions is not uniform.
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
2- Drawing Circles Using Parametric Representation

r
θ
x xc  rcosθ yc
y yc  rsinθ
xc

Still considerable computation at each step.


Optimisation and speed-up
 Symmetry of a
circle can be (-x,y) (x,y)
used

 Calculations of (-y,x) (y,x)


point coordinates
only for a first (-y,-x) (y,-x)
one-eighth of a
circle
(-x,-y) (x,-y)
Mid-Point Circle Algorithm (Bresenham)
 Assume that we have
just plotted point (xk, yk)
(xk, yk) (xk+1, yk)
 The next point is a
choice between(xk+1, yk)
and (xk+1, yk-1) (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?
 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



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
 Assuming we have just plotted the pixel at (x ,y ) so we
k k
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 p < 0 the midpoint is inside the circle and the pixel at
k
yk is closer to the circle
 Otherwise the midpoint is outside and yk-1 is closer
1 2 1 2 2
let pk  f ( xk  1, yk  ) ( xk  1)  ( yk  )  r
2 2
1 2 1 2 2
pk 1  f ( xk 1  1, yk 1  ) ( xk  2)  ( yk 1  )  r
2 2
where xk 1  xk  1 (0, r)

 yk if pk  0
yk 1 
 yk  1 otherwise
 pk  2 xk  3 if pk  0
pk 1 
 p k  2( x k  y k )  5 otherwise
1 2 1 2 2 5
p0  f ( x0  1, y0  ) (0  1)  (r  )  r   r
2 2 4
since ( x0 , y0 ) (0, r )
MID-POINT CIRCLE ALGORITHM
1. 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 )
2. Calculate the initial value of the decision parameter as:
p0  5  r 1  r
4
3. 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:
pk 1  pk  2 xk  3
Otherwise the next point along the circle is (xk+1, yk-1) and:

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

x  x  xc y  y  yc
6. Repeat steps 3 to 5 until x >= y
Midpoint Circle Algorithm. Example

r=10,
p0=1-r=-9, k pk (xk+1,yk+1) 2xk+1 2yk+1

(x0,y0)= (0,10), 0 -9 (1,10) 2 20


2x0=0, 2y0=20 1 -6 (2,10) 4 20
-1 (3,10) 6 20
2
3 6 (4,9) 8 18
4 -3 (5,9) 10 18
8 (6,8) 12 16
5
6 5 (7,7) 14 14
Midpoint Circle Algorithm. Summary
As in Bresenham's line algorithm, the midpoint method
calculates pixel positions along the circumference of a
circle using integer additions and subtractions, assuming
that the circle parameters are specified un integer screen
Coordinates.

You might also like