Design of Line, Circle & Ellipse Algorithms

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Design of Line, Circle & Ellipse Algorithms

Slope-Intercept Formula For A Line 6 Given a third point on the line: 5 ! "#,$% &lope ! "$ ' $(%)"# ' #(% 4 ! "$* ' $(%)"#* ' #(% &olving +or $ $ ! ,"$*'$(%)"#*'#(%-# . ,'"$*'$(%)"#*'#(%-#( . $( therefore $ ! M# . B where M ! ,"$*'$(%)"#*'#(%B ! ,'"$*'$(%)"#*'#(%-$( . $(
3 2 1

Basic Math Review

Cartesian Coordinate System

P = x!y"

P2 = x2!y2"

P1 = x1!y1"

4
RISE RUN

5
=

6
y2-y1 x2-x1

SLOPE =

/ther 0elpf1l +orm1las


Length of line segment between P1 and P2: L ! s#rt[ "#*'#(%* . "$*'$(%* idpoint of a line segment between P1 and P!:
*

! " "#(.#2%)* , "$(.$2%)* %

3wo lines are perpendicular iff (% M( ! '()M* or *% Cosine of the angle 4etween them is 56

arametric +orm /f 3he E71ation /f A *D Line &egment


Given points
(

! "#(, $(%

and

! "#*, $*%

" # "1 $ t%"2-"1& ' # '1 $ t%'2-'1& t is called the parameter6 8hen t # ( we get %"1)'1& t # 1 we get %"2)'2& As 5 9 t 9 ( we get all the other points on the line segment 4etween "#(,$(% and "#*,$*%6

(6 M1st comp1te integer coordinates of pi#els which lie on or near a line or circle6 *6 i#el level algorithms are invo:ed h1ndreds or tho1sands of times when an image is created or modified6 26 Lines m1st create vis1all$ satisfactor$ images6 Lines sho1ld appear straight Lines sho1ld terminate acc1ratel$ Lines sho1ld have constant densit$ Line densit$ sho1ld 4e independent of line length and angle6 ;6 Line algorithm sho1ld alwa$s 4e defined6

Basic Line and Circle Algorithms

&imple DDA Line Algorithm


<Based on the parametric e71ation of a line=
roced1re DDA">(,?(,>*,?* :@nteger%A Bar Length, @ :@ntegerA >,?,>inc,?inc :RealA Begin Length :! AB&">* ' >(%A @f AB&"?* ' ?(% C Length 3hen Length :! AB&"?*'?(%A >inc :! ">* ' >(%)LengthA ?inc :! "?* ' ?(%)LengthA > :! >(A ? :! ?(A For I := 0 To Length Do Begin Plot(Round(X ! Round(" # X := X $ Xin%# " := " $ "in% &nd 'For( &nd# 'DD)(

DDA "digital differential analyzer% creates good lines 41t it is too time cons1ming d1e to the ro1nd f1nction and long operations on real val1es6

DDA E#ample Comp1te which pi#els sho1ld 4e t1rned on to represent the line from
"D,E% to "((,(*%6 Length :! Ma# of "AB&"(('D%, AB&"(*'E%% ! F >inc :! ( ?inc :! 56D Bal1es comp1ted are: "D,E%, "G,E6D%, "H,(56*%, "E,(56H%, "(5,((6;%, "((,(*%

13 12 11 1' & 6 $ % & 1' 11 12 13

&imple Circle Algorithms &ince the e71ation for a circle on radi1s r centered at "5,5% is
#* . $* ! r*, an o4vio1s choice is to plot $ ! Is7rt"r* ' #*% for 'r 9! # 9! r6 3his wor:s, 41t is inefficient 4eca1se of the m1ltiplications and s71are root operations6 @t also creates large gaps in the circle for val1es of # close to R "and cl1mping for # near 5%6 A 4etter approach, which is still inefficient 41t avoids the gaps is to plot # ! r cosJ $ ! r sinJ as J ta:es on val1es 4etween 5 and 2D5 degrees6

+ast Lines Ksing 3he Midpoint Method


Ass1mptions: Ass1me we wish to draw a line 4etween points "5,5% and "a,4% with slope M 4etween 5 and ( "i6e6 line lies in first octant%6
+y

(*!+

-x

(0!0

+x

-y

3he general form1la for a line is ' # " $ * where M is the slope of the line and * is the $'intercept6 +rom o1r ass1mptions M ! 4)a and * ! 56 3herefore $ ! "4)a%# . 5 is f"#,$% ! 4# L a$ ! 5 "an e71ation for the line%6 @f "#(,$(% lie on the line with M ! 4)a and B ! 5, then f"#(,$(% ! 56

+ast Lines "cont6% +or lines in the first octant, the ne#t
pi#el is to the right or to the right and 1p6
(xi +1, yi + 1/2 + e)

T = (xi + 1, yi + 1)

e (xi +1,yi + 1/2)

Ass1me: Distance 4etween pi#els centers ! (


P = (xi,yi ) S = (xi + 1, yi )

0aving t1rned on pi#el at "#i, $i%, the ne#t pi#el is 3 at "#i.(, $i.(% or & at "#i.(, $i%6 Choose the pi#el closer to the line f"#, $% ! 4# ' a$ ! 56 3he midpoint 4etween pi#els & and 3 is "#i . (,$i . ()*%6 Let e 4e the difference 4etween the midpoint and where the line act1all$ crosses 4etween & and 36 @f e is positive the line crosses a4ove the midpoint and is closer to 36 @f e is negative, the line crosses 4elow the midpoint and is closer to &6 To pick the correct point we only need to know the sign of e.

+ast Lines ' 3he Decision Baria4le f"# .(,$ . ()* . e% ! 4"# .(% ' a"$ . ()* . e% ! 4"# . (% ' a"$ . ()*% 'ae
i i i i i i

! f"#i . (, $i . ()*% ' ae ! 5 Let di ! f"#i . (, $i . ()*% ! aeA di is :nown as the decision +ariable6 &ince a C! 5, di has the same sign as e6 Algorithm: @f di C! 5 3hen Choose 3 ! "#i . (, $i . (% as ne#t point di.( ! f"#i.( . (, $i.( . ()*% ! f"#i .(.(,$i .(.()*% ! 4"#i .(.(% ' a"$i .(.()*% ! f"#i . (, $i . ()*% . 4 ' a ! di . 4 ' a Else Choose & ! "#i . (, $i% as ne#t point di.( ! f"#i.( . (, $i.( . ()*% ! f"#i .(.(,$i .()*% ! 4"#i .(.(% ' a"$i .()*% ! f"#i . (, $i . ()*% . 4 ! di . 4

+ast Line Algorithm


The initi*l 6*lue 1or the de%i4ion 6*ri*+le! d0! 9*- +e %*l%ul*ted dire%tl- 1ro9 the 1or9ul* *t :oint (0!0 7 d0 = 1(0 $ 1! 0 $ 1/0 = +(1 . *(1/0 = + . */0 There1ore! the *lgorith9 1or * line 1ro9 (0!0 to (*!+ in the 1ir4t o%t*nt i4:

, := 0# - := 0# d := + . */0# For i := 0 to * do Plot(,!- # I1 d 2= 0 Then , := , $ 1# - := - $ 1# d := d $ + 3 * &l4e , := , $ 1# d := d $ + &nd &nd

5ote: The onl- non.integer 6*lue i4 */07 I1 8e then 9ulti:l- +- 0 to get d; = 0d! 8e %*n do *ll integer *rith9eti% u4ing onl- the o:er*tion4 $! .! *nd le1t.4hi1t7 The *lgorith9 4till 8or<4 4in%e 8e onl- %*re *+out the 4ign! not the 6*lue o1 d7

BresenhamMs Line Algorithm 8e can also generaliNe the


algorithm to wor: for lines 4eginning at points other than "5,5% 4$ giving # and $ the proper initial val1es6 3his res1lts in BresenhamOs Line Algorithm6
Note: This algorithm only works for lines with Slopes between 0 and 1

Begin 'Bre4enh*9 1or line4 8ith 4lo:e +et8een 0 *nd 1( * := )B=(,end . ,4t*rt # + := )B=(-end . -4t*rt # d := 0>+ . *# In%r1 := 0>(+.* # In%r0 := 0>+# I1 ,4t*rt 2 ,end Then , := ,end# - := -end &l4e , := ,4t*rt# - := -4t*rt &nd For I := 0 to * Do Plot(,!- # , := , $ 1# I1 d 2= 0 Then - := - $ 1# d := d $ in%r1 &l4e d := d $ in%r0 &nd &nd 'For Loo:( &nd 'Bre4enh*9(

Circle Drawing Algorithm 8e onl$ need to calc1late the val1es on the 4order of the circle in the
first octant6 3he other val1es ma$ 4e determined 4$ s$mmetr$6 Ass1me a circle of radi1s r with center at "5,5%6
roced1re CircleP oints"#,$ :@nteger%A Begin lot"#,$%A lot"$,#%A lot"$,'#%A lot"#,'$%A (-a,b) lot"'#,'$%A lot"'$,'#%A (-a,-b) lot"'$,#%A lot"'#,$% EndA

(-b,a)

(b,a)

(a,b) (a,-b)

(-b,-a)

(b,-a)

+ast Circles Consider onl$ the first octant of a circle of radi1s r centered on the
origin6 8e 4egin 4$ plotting point "r,5% and end when # 9 $6
x +y -r
2 2 2

=0

x=y

3he decision at each step is whether to choose the pi#el directl$ a4ove the c1rrent pi#el or the pi#el which is a4ove and to the left6 Ass1me
i

! "#i, $i%

is the c1rrent pi#el6 is the pi#el directl$ a4ove is the pi#el a4ove and to the left6

3i ! "#i, $i .(% &i ! "#i '(, $i .(%

+ast Circles ' 3he Decision Baria4le f"#,$% ! # . $ ' r ! 5


* * *

(xi -1/2, yi + 1)

T = (xi ,yi +1)

f"#i ' ()* . e, $i . (% ! "#i ' ()* . e%* . "$i . (%* ' r* ! "#i' ()*%* . "$i.(%* ' r* . *"#i'()*%e . e* ! f"#i ' ()*, $i . (% . *"#i ' ()*%e . e* ! 5 Let di ! f"#i ' ()*, $i.(% ! '*"#i ' ()*%e ' e* 3h1s, @f e 9 5 then di C 5 so choose point & ! "#i ' (, $i . (%6 di.( ! f"#i ' ( ' ()*, $i . ( . (% ! ""#i ' ()*% ' (%* . ""$i . (% . (%* ' r* ! di ' *"#i '(% . *"$i . (% . ( ! di . *"$i.(' #i.(% . ( @f e C! 5 then di 9! 5 so choose point 3 ! "#i, $i . (%6 di.( ! f"#i ' ()*, $i . ( . (% ! di . *$i.( . (
P = (xi ,yi ) e S = (xi -1,yi +1)

+ast Circles ' Decision Baria4le "cont6%


3he initial val1e of di is d5 ! f"r ' ()*, 5 . (% ! "r ' ()*%* . (* ' r* ! F); ' r <('r can 4e 1sed if r is an integer= 8hen point & ! "#i ' (, $i . (% is chosen then di.( ! di . '*#i.( . *$i.( . (

8hen point 3 ! " #i , $i . (% is chosen then di.( ! di . *$i.( . (

+ast Circle Algorithm


Begin <Circle= # :! rA $ :! 5A d :! ( ' rA Repeat CircleP oints"#,$%A $ :! $ . (A @f d 9! 5 3hen d :! d . *Q$ . ( Else # :! # ' (A d :! d . *Q"$'#% . ( End Kntil # 9 $ EndA <Circle= Pro%edure ?ir%le@Point4(,!- :Integer # Begin Plot(,!- # Plot(-!, # Plot(-!., # Plot(,!.- # Plot(.,!.- # Plot(.-!., # Plot(.-!, # Plot(.,!&nd#

3he circle algorithm can 4e generaliNed to wor: for an ellipse 41t onl$ fo1r wa$ s$mmetr$ can 4e 1sed6 +"#,$% ! 4*#* . a*$* 'a*4* ! 5
(0,b) (-x, y) (x, y)

+ast Ellipses

(-a,0)

(a,0)

(-x, -y) (0,-b)

(x, -y)

3he circle algorithm can 4e generaliNed to wor: for an ellipse 41t onl$ fo1r wa$ s$mmetr$ can 4e 1sed6 +"#,$% ! 4*#* . a*$* 'a*4* ! 5
(0,b) (x, y) (-x, y)

+ast Ellipses

-=,

(-a,0)

(a,0)

(x, -y) (-x, -y) (0,-b)

All the points in one 71adrant m1st 4e comp1ted6 &ince BresenhamOs algorithm is restricted to onl$ one octant, the comp1tation m1st occ1r in two stages6 3he changeover occ1rs when the point on the ellipse is reached where the tangent line has a slope of I(6 @n the first 71adrant, this is where the line $ ! # intersects the ellipses6

Line and Circle References Bresenham, R6E6, SAm4ig1ities @n @ncremental Line Rastering,S @EEE
Comp1ter Graphics And Applications, Bol6 G, To6 F, Ma$ (EHG6 Ec:land, Eric, S@mproved 3echni71es +or /ptimising @terative Decision' Baria4le Algorithms, Drawing Anti'Aliased Lines U1ic:l$ And Creating Eas$ 3o Kse Color Charts,S C&C ;D* roVect Report, Department of Comp1ter &cience, Torth Carolina &tate Kniversit$ "&pring (EHG%6 +ole$, R6D6 and A6 Ban Dam, +1ndamentals of @nteractive Comp1ter Graphics, Addison'8esle$ (EH*6 Tewman, 86M and R6+6 &pro1ll, rinciples /f @nteractive Comp1ter Graphics, McGraw'0ill, (EGE6 Ban A:en R6 and Mar: Tova:, SC1rve Drawing Algorithms +or Raster Displa$,S ACM 3ransactions /n Graphics, Bol6 ;, To6 2, April (EHF6

You might also like