Design of Line, Circle & Ellipse Algorithms
Design of Line, Circle & Ellipse Algorithms
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
P = x!y"
P2 = x2!y2"
P1 = x1!y1"
4
RISE RUN
5
=
6
y2-y1 x2-x1
SLOPE =
3wo lines are perpendicular iff (% M( ! '()M* or *% Cosine of the angle 4etween them is 56
! "#(, $(%
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
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;%, "((,(*%
&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
(*!+
-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)
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
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
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
(xi -1/2, 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)
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)
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)
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