My Line and Circle
My Line and Circle
Scan Conversion
Line and Circle
Scan Conversion
(2, 7) (6, 7)
(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 mx or x y
m
A very simple procedure for scan converting a straight line is given as
follows:
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
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
2 3 4 5
Deriving The Bresenham Line Algorithm
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
2y xk 2x yk 2y x(2b 1)
2y xk 2x yk c
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
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 pk 2y ( xk 1 xk ) 2x( yk 1 yk )
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
p0 2y 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 2y x
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<0pk+1=pk+2dy;xk+1=xk+1;yk+1=yk
Pk>=0pk+1=pk+2dy-2dx; xk+1=xk+1; yk+1=yk+1
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
f circ ( x, y ) x 2 y 2 r 2
The equation evaluates as follows:
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