Sie sind auf Seite 1von 73

Chapter 3

Output primitives
Topics
 Creation of simple objects
 Line
 Circle
 Ellipse
 Filling areas
 Objects are created on the display device by
bringing light to a collection of pixels. Assume
that setpixel (x, y, c) will light position (x,y)
with color c.
Line drawing
 DDA
 Uses floating point calculations
 Bresenham’s method
 Uses only integer arithmetic
Line drawing
 Designed to take inputs of end points
 With (x1,y1) and (x2,y2) as end points the
equation is y=mx+c where m=(y2-y1)/(x2-x1)
and c=y1-m*x1
 When x increase (or decrease) is more, then x is
incremented in units of 1 (or -1) and y
computed in increments of m (dy /|dx|) and
rounded
 This is DDA
DDA
 Ex: line between (20,10) and (28, 15)
 m=5/8
 xinc=1, yinc=5/8=0.625
 The points to be plotted on the device are
 (20,10), (21, round(10+.625)) / (21, 11), (22,
round(10+2*0.625)) / (22,11), (23,12), (24,
13), (25, 13), (26, 14), (27, 14), (28, 15)
DDA
 Ex: End points are (20, 20), (12, 25)
 Here x decreases rapidly when compared to y
increase
 dx=-8, dy=5
 Number of points= max(|dx|, |dy|)=steps=8
 xinc=dx/steps=-1, yinc=dy/steps=5/8
 Points generated are (20, 20), (19, round(20+.625)) /
(19, 21), (18, 20+.625+.625)/(18,21), (17, 22), (16,
23), (15, 23), (14, 24), (13, 24), (12, 25)
DDA Line drawing
 When y increase (or decrease) is more, then y is
incremented in units of 1 (or -1) and x computed
in increments of 1/m (dx/|dy|) and rounded
 Note that x=(y-c)/m
 Ex: end points be (10, 20), (15, 30)
 dx=5, dy=10, |dx| > |dy|
 xinc=1/m=5/10=0.5, yinc=1
 Points plotted are (10, 20), (10.5, 21)/(11,21), (11,
22), (11.5, 23)/(12,23), …
DDA Line drawing
 When y increase (or decrease) is more, then y is
incremented in units of 1 (or -1) and x computed
in increments of 1/m (dx/|dy|) and rounded
 Note that x=(y-c)/m
 Ex: end points be (10, 20), ( 5, 30)
 dx=-5, dy=10, |dx| > |dy|
 xinc= -5/10= -0.5, yinc=1
 Points plotted are (10, 20), (9.5, 21)/(10,21), (9,
22), (8.5, 23)/(9,23), …
DDA
Procedure DDA(xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
If abs (dx) >=abs(dy) then steps=abs(dx)
else steps=abs(dy)
xinc=dx/steps, yinc=dy/steps
x=xa, y=ya
Setpixel(x, y, c)
For i=1 to steps
x=x+xinc, y=y+yinc
Setpixel (round(x), round(y),c)
End for
End DDA
Bresenham’s line drawing
 Suppose dx, dy are positive and dx > dy
 Let (x1, y1) be the first point of the line
 Then call setpixel(x1, y1, c). First pixel is lit.
 Suppose that equation to the line to be displayed is y=mx+c
 As x increment is positive and greater xinc=1
 Suppose that k pixels are plotted
 xk+1= xk +1 and yk+1 = yk +1 or yk
 Actual y coordinate at xk+1 is m* xk+1 +c = m* (xk +1) +c =y and this may
be a floating point number
 We find the difference between the actual y and these two possible y’s
and go for that y which is closer to actual y
 pk=(y- yk ) -(yk +1-y) is the decision parameter
 pk =2*dy* xk – 2*dx* yk + 2*c*dx-dx (prove this!)
 Sign of pk will determine the next y coordinate
Bresenham’s line drawing
 Case 1
 If pk is positive or zero then yk+1is closer to y
 We set yk+1 = yk +1 and plot (xk+1,yk+1 )
 We compute the next step decision parameter pk+1
 xk+2= xk+1 +1 and yk+2 = yk+1 +1 or yk+1 (these are possible y’s)
 Actual y coordinate at xk+2 is m* xk+2 +c = m* (xk+1 +1) +c
=y and this may be a floating point number
 As in the previous step we calculate ‘p’ as difference
between actual y and the possible y’s
Bresenham’s line drawing
 pk+1=(y- yk+1 ) -(yk+1 +1-y) is the decision
parameter
 pk+1 = (y- yk +1) -(yk +1+1-y) = pk +2*dy-2*dx
 Case 2
 If pk is negative then yk is closer to y
 We set yk+1 = yk and plot (xk+1,yk+1 )
 ‘p’ is updated as
 pk+1 = (y- yk ) -(yk +1-y) = pk +2*dy
Bresenham’s line drawing
 Using current ‘p’ at any step a point is plotted – integer arithmetic
 ‘p’ is updated – integer arithmetic
 Line drawing this way uses only integer arithmetic so far
 We find initial ‘p’ and if this also uses only integers then all
computations in the algorithm are all integers
 Plot first point (x1, y1)
 x2=x1+1, y2=y1 or y1+1 and p1=(y- y1 ) -(y1 +1-y)
 Here y=m*x2 +c = m (x1+1)+c
 p1 = 2*dy-dx (prove this!) also uses only integer operations
 Thus the points to be plotted on the display device are found using
only integer operations
Bresenham’s algorithm
The algorithm for the special case of dx, dy are positive and dx >= dy is given here
Bresenham-line1 (xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
d1=2*dy, d2=2*dy-2*dx, p= 2*dy-dx
x=xa, y=ya
setpixel(x, y, c)
For i=1 to dx-1 in steps of 1
x=x+1
If p>=0 then y= y+1 and p=p+d2
else p=p+d1
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bresenham-line1
Bresenham’s line drawing
 For the case of dx, dy positive and dy > dx swap the role of x, y and dx and
dy in the previous algorithm
Bresenham-line2 (xa, ya, xb, yb, c)
dx=xb-xa, dy=yb-ya
d1=2*dx, d2=2*dx-2*dy, p= 2*dx-dy
x=xa, y=ya
setpixel(x, y, c)
For i=1 to dy-1 in steps of 1
y=y+1
If p>=0 then x= x+1 and p=p+d2
else p=p+d1
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bresenham-line2
Bresenham’s line drawing
 By adjusting x and y increments these two algorithms can be made to
work for all cases
Bres-line1 (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
d1=2*dy, d2=2*dy-2*dx, p= 2*dy-dx
If xa < xb, xinc=1 endif If xa=xb, xinc=0 endif If xa > xb, xinc=-1 endif
If ya < yb yinc=1 endif If ya=yb, yinc=0 endif If ya > yb, yinc=-1 endif
x=xa, y=ya, setpixel(x, y, c)
For i=1 to dx-1 in steps of 1
x=x+xinc
If p>=0 then y= y+yinc and p=p+d2
else p=p+d1 Endif
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bres-line1
Bresenham’s line drawing
 For the case of more of y change than that of x
Bres-line2 (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
d1=2*dx, d2=2*dx-2*dy, p= 2*dx-dy
If xa < xb, xinc=1 endif If xa=xb, xinc=0 endif If xa > xb, xinc=-1 endif
If ya < yb yinc=1 endif If ya=yb, yinc=0 endif If ya < yb, yinc=-1 endif
x=xa, y=ya, setpixel(x, y, c)
For i=1 to dy-1 in steps of 1
y=y+yinc
If p>=0 then x= x+xinc and p=p+d2
else p=p+d1 Endif
setpixel(x, y, c)
End for
setpixel(xb, yb, c)
End Bres-line2
Bresenham’s line drawing
 For any case the algorithm is given as follows
Bres-line (xa, ya, xb, yb, c)
dx=abs(xb-xa), dy=abs(yb-ya)
If dx >= dy call Bres-line1(xa, ya, xb, yb, c)
Else call Bres-line2 (xa, ya, xb, yb, c)
End Bres-line
Tracing Bresenham’s algorithm
 End points be (12, 24), (22, 20)
 dx=10, dy=-4 (|dx| > |dy| so execute Bres-line1)
 xinc= 1, yinc=-1
 p= 2*|dy|-|dx|= -2, d1= 2*|dy|= 8, d2=2*|dy|-2*|dx|= -12
 p -2 6 -6 -2 6
 Pixel (12, 24) (13, 24) (14, 23) (15, 23) (16, 23) (17, 22)
 Next p p+d1 p+d2 p+d1 p+d1 p+d2
 p -6 2 -10 -2 6
 Pixel (18, 22) (19, 21) (20, 21) (21, 21) (22, 20)
 Next p p+d1 p+d2 p+d1 p+d1
Tracing Bresenham’s algorithm
 End points be (12, 24), (8, 30)
 dx=-4, dy= 6 (|dx| < |dy| so execute Bres-line2)
 xinc= -1, yinc= 1
 p= 2*|dx|-|dy|= -4, d1= 2*|dx|= 8, d2=2*|dx|-2*|dy|= -4
 p -4 4 0 -4 4
 Pixel (12, 24) (12, 25) (11, 26) (10, 27) (10, 28) (9, 29)
 Next p p+d1 p+d2 p+d2 p+d1 p+d2
 p 0
 Pixel (8, 30)
 Next p
Circle drawing
 Simple methods
 Uses floating point calculations
 Bresenham’s method
 Uses only integer arithmetic
 Midpoint circle method
 Uses only integer arithmetic
Circle drawing
 Symmetry in circle
 4 quadrants are shown using white lines
 If (a, b) is on the circle in I quadrant, then (-a, b),
(-a, -b), (a, -b) are points on the circle in II, III, IV quadrants
 Blue and white lines show 8 way
symmetry
 If (a, b) is a point on the circle in 8 1
2
region 1 then (b, a), (b, -a), (a, -b), 7

(-a, -b), (-b, -a), (-b, a), (-a, b) are points 6


5 4
3

on the other 7 regions in order


Circle drawing
 We device algorithms to generate points in region 1 and
plot other 7 points in various other symmetrical regions
 Region 1 points are generated, because it is here the x
increase is more than y decrease
 xinc= 1 and y is computed
 In region 1, x < y at the boundary of regions and 2 x= y.
Generation stops when x > y
 Circle with centre (a, b) and radius r is drawn by
generating points on the circle with centre at origin and
radius r and shift every point by a, b in x and y directions
Simplecircle
 Circle with centre at origin
Simplecircle (0, 0, r, c)
x=0, y=r
setpixel(0, r, c)
setpixel(r, 0, c), setpixel (0, -r, c), setpixel(-r, 0, c)
do
x=x+1, y= sqrt (r2-x2) rdy=round(y)
setpixel(x, rdy, c), setpixel(rdy, x, c)
setpixel(rdy, -x, c), setpixel(x, -rdy, c)
setpixel(-x, -rdy, c), setpixel(-rdy, -x, c)
setpixel(-rdy, x, c), setpixel(-x, rdy, c)
until(x>=y)
End Simplecircle
 Involves square root operation
Simplecircle1
 Circle anywhere
Simplecircle1(h, k, r, c)
x=0, y=r
setpixel(0+h, r+k, c)
setpixel(r+h, 0+k, c), setpixel (0+h, -r+k, c)
setpixel(-r+h, 0+k, c)
do
x=x+1, y= sqrt (r2-x2) rdy=round(y)
setpixel(x+h, rdy+k, c), setpixel(rdy+h, x+k, c)
setpixel(rdy+h, -x+k, c), setpixel(x+h, -rdy+k, c)
setpixel(-x+h, -rdy+k, c), setpixel(-rdy+h, -x+k, c)
setpixel(-rdy+h, x+k, c), setpixel(-x+h, rdy+k, c)
until (x+h >=y+k)
End Simplecircle1
Polarcircle
 Polar coordinates of points on the circle with centre (0, 0) are (rcos,
rsin)

polarcircle(0, 0, r, c)
pi=22/7
for phi=pi/2 to pi/4 in steps of 
x=round(r*cos(phi)), y=round(r*sin(phi))
setpixel(x, y, c), setpixel(y, x, c)
setpixel(y, -x, c), setpixel(x, -y, c)
setpixel(-x, -y, c), setpixel(-y, -x, c)
setpixel(-y, x, c), setpixel(-x, y, c)
endfor
end polarcircle
Polarcircle1
 Circle with center (h, k)

polarcircle1(h, k, r, c)
pi=22/7
for phi=pi/2 to pi/4 in steps of 
x=round(r*cos(phi)), y=round(r*sin(phi))
setpixel(x+h, y+k, c), setpixel(y+h, x+k, c)
setpixel(y+h, -x+k, c), setpixel(x+h, -y+k, c)
setpixel(-x+h, -y+k, c), setpixel(-y+h, -x+k, c)
setpixel(-y+h, x+k, c), setpixel(-x+h, y+k, c)
endfor
end polarcircle1
Bresenham’s circle
 Circle drawn using only integer operations
 As in the line algorithm a decision parameter is found and based on the sign of
this, y is updated (points close to portion of the circle in sector 1 is generated
and symmetrical points in the remaining sectors are plotted)
 Suppose that k points are generated by the algorithm and the k th point be (xk,
yk)
 xk+1 =xk + 1 and yk+1 =yk or yk -1
 y is such that yk -1 ≤ y ≤ yk
 D(T)= (xk +1)2 + (yk -1)2-r2 is ≤ 0
 D(S)= (xk +1)2 + (yk )2-r2 is ≥ 0
 pk=D(T)+D(S)
 If pk ≥ 0, then D(T) is smaller and hence (xk +1, yk -1) is closer to the circle and
xk+1 =xk + 1 and yk+1 = yk -1
 We can show that pk+1 = pk + 4 xk -4yk +10 = pk + 4 xk+1 -4yk+1 +2
 Else, (xk +1, yk ) is closer to circle
 We can show that pk+1 = pk + 4 xk +6 = pk + 4xk+1 +2
 The first point on the circle is (0,r). P1=2(1)+(r-1)2+r2-2r2=3-2r
Bresenham’s circle
Bresenham-circle(h,k,c)
x=0, y=r, p=3-2r
setpixel(x+h, y+k, c), setpixel(x+h, -y+k, c), setpixel(y+h, x+k, c), setpixel(-y+h, x+k, c)
while (x<=y)
if p>=0 then p=p+4x-4y+10, y=y-1
else p=p+4x+6
endif
x=x+1
setpixel(x+h, y+k,c ), setpixel(y+h, x+k,c)
setpixel(x+h, -y+k,c), setpixel(y+h, -x+k, c)
setpixel(-x+h, -y+k,c), setpixel –y+h, -x+k, c)
setpixel(-x+h, y+k,c), setpixel (-y+h, x+k, c)
endwhile
end Bresenham-circle
Midpoint circle
 Circle drawn using only integer operations
 As in the line algorithm a decision parameter is found and based on the sign of
this, y is updated (points close to portion of the circle in sector 1 is generated
and symmetrical points in the remaining sectors are plotted)
 Let f(x, y)= x2+y2-r2
 If f(x, y) <, =, > 0 then (x, y) is inside, on, outside the circle
 Suppose that k points are generated by the algorithm and the k th point be (xk,
yk)
 xk+1 =xk + 1 and yk+1 =yk or yk -1
 y is such that yk -1 ≤ y ≤ yk
 Possible (k+1)th pixels are (xk + 1, yk ) and (xk + 1, yk -1)
 Midpoint of these two pixels is (xk + 1, yk -1/2)
 Sign of f(xk + 1, yk -1/2) decides the next pixel
 f(xk + 1, yk -1/2) = (xk + 1)2+( yk -1/2)2-r2 =pk
Midpoint circle
 Case 1
 pk>=0
 Then midpoint is outside. So the point inside is
close to circle.
 xk+1=xk+1 and yk+1=yk-1
 pk+1 = (xk+1 + 1)2+( yk+1 -1/2)2-r2 = (xk + 1+1)2+
( yk -1-1/2)2-r2 = pk+ 2xk -2 yk +5 =pk+ 2xk+1
-2yk+1 -1
Midpoint circle
 Case 2
 Pk<0
 Then midpoint is inside. So the point outside is close to circle.
 xk+1=xk+1 and yk+1=yk
 pk+1 = (xk+1 + 1)2+( yk+1 -1/2)2-r2 = (xk + 1+1)2+( yk -1/2)2-r2 = pk+
2xk +3 = pk+ 2xk+1 +1
 Circle is plotted from (0,r). The next pixel is (1, r) or (1, r-1).
Midpoint of these two pixels is (1, r-1/2) and p1= (0 + 1)2+( r
-1/2)2-r2 =p1= 5/4-r
 If r is integer (usually it is) we can take p= 1-r. Thus starting p is
integer and all updations on p are integer and so the algorithm is
integer arithmetic algorithm
Midpoint circle algorithm
Midpointcircle(h,k,c)
x=0, y=r, p=1-r
setpixel(x+h, y+k, c), setpixel(x+h, -y+k, c), setpixel(y+h, x+k, c), setpixel(-y+h, x+k, c)
while (x<=y)
x=x+1
if p>=0 then y=y-1, p=p+2x-2y+1,
else p=p+2x+1
endif
setpixel(x+h, y+k,c ), setpixel(y+h, x+k,c)
setpixel(x+h, -y+k,c), setpixel(y+h, -x+k, c)
setpixel(-x+h, -y+k,c), setpixel –y+h, -x+k, c)
setpixel(-x+h, y+k,c), setpixel (-y+h, x+k, c)
endwhile
end Midpointcircle
Bresenham’s circle drawing
 Circle centre(10, 15) and radius 5
 If p >= 0 then p=p+4x-4y+10, x=x+1, y=y-1
 Else p=p+4x+6, x=x+1
p=3-2r=-7, x=0, y=5
points (0+10, 5+15), (0+10, -5+15), (5+10, 0+15) and (-5+10, 0+15) are
plotted
current p -7
next p, x, y p=-7+6=-1, x=1, y=5
points plotted (1+10, 5+15), (5+10, 1+15),
(1+10, -5+15), (5+10, -1+15),
(-1+10, -5+15), (-5+10, -1+15),
(-1+10, 5+15), (-5+10, 1+15)
x<=y? true
Bresenham’s circle drawing
current p -1
next p, x, y p=-1+4+6=9, x=2, y=5
points plotted (2+10, 5+15), (5+10, 2+15),
(2+10, -5+15), (5+10, -2+15),
(-2+10, -5+15), (-5+10, -2+15),
(-2+10, 5+15), (-5+10, 2+15)
x<=y? True
current p 9
next p, x, y p=9+8-20+10=7, x=3, y=4
points plotted (3+10, 4+15), (4+10, 3+15),
(3+10, -4+15), (4+10, -3+15),
(-3+10, -4+15), (-4+10, -3+15),
(-3+10, 4+15), (-4+10, 3+15)
x<=y? true
Bresenham’s circle drawing
current p 7
next p, x, y p=7+12-16+10=13, x=4, y=3
points plotted (4+10, 3+15), (3+10, 4+15),
(4+10, -3+15), (3+10, -4+15),
(-4+10, -3+15), (-3+10, -4+15),
(-4+10, 3+15), (-3+10, 4+15)
x<=y? No
All pixels close to circle are plotted.
Midpoint circle drawing
 Circle centre(10, 15) and radius 5
 If p >= 0 then x=x+1, y=y-1, p= p+2x-2y+1
 Else x=x+1, p=p+2x+1
p=1-r=-4, x=0, y=5
points (0+10, 5+15), (0+10, -5+15), (5+10, 0+15) and (-5+10, 0+15) are
plotted
current p -4
next x, y, p x=1, y= 5, p=-4+2+1=-1
points plotted (1+10, 5+15), (5+10, 1+15),
(1+10, -5+15), (5+10, -1+15),
(-1+10, -5+15), (-5+10, -1+15),
(-1+10, 5+15), (-5+10, 1+15)
x<=y? true
Midpoint circle drawing
current p -1
Next x, y, p x=2, y=5, p=-1+ 4+1=4
points plotted (2+10, 5+15), (5+10, 2+15),
(2+10, -5+15), (5+10, -2+15),
(-2+10, -5+15), (-5+10, -2+15),
(-2+10, 5+15), (-5+10, 2+15)
x<=y? True
current p 4
next x, y, p x=3, y=4, p=4+6-8+1=3
points plotted (3+10, 4+15), (4+10, 3+15),
(3+10, -4+15), (4+10, -3+15),
(-3+10, -4+15), (-4+10, -3+15),
(-3+10, 4+15), (-4+10, 3+15)
x<=y? true
Midpoint circle drawing
current p 4
next x, y, p x=4, y=3, p=4+8-6+1=7
points plotted (4+10, 3+15), (3+10, 4+15),
(4+10, -3+15), (3+10, -4+15),
(-4+10, -3+15), (-3+10, -4+15),
(-4+10, 3+15), (-3+10, 4+15)
x<=y? No
All pixels close to circle are plotted.
Note that both algorithms plot the same set of points in the same
order
Ellipse
 Ellipse with centre origin axes parallel to co- ordinate axes
with half major and minor axes as a, b is x 2/a2+y2/b2=1

dy/dx=-1
dy/dx=-1

 There is four way symmetry. Points in quadrant 1 is


computed and others plotted by symmetry
 In the first quadrant in the region closer to (0, b), x increase
is more than y decrease. So xinc=1 and y is computed here.
 In the same quadrant in the region closer to (a, 0) x
increase is lesser than y decrease. Here yinc=-1 and x is
computed.
Ellipse
Angle made by tangents dy/dx
At various points from
(0,b)
180 0
170 -0.176 |dy| < |dx|
160 -0.363
135 -1
125 -1.42
115 -2.144
100 -5.671 |dy| > |dx|
95 -11.430
92 -28.636

Region 1 is when |dy| < |dx|. Here x=x+1, and y


computed. In region 2 where |dy| > |dx|, y=y-1 and x
computed
Ellipse
 This switch of |dx| > |dy| and |dx| < |dy| does not
happen exactly at x=a/2
 This shift change is at different positions in different
ellipses and depends on values of a, b
 At (0,b) dy/dx is 0. It is always negative and reaches
infinity at (a,0)
 In region closer to (0,b), dy/dx is negative and abs(dy/dx)
is <1. abs(dy) < abs(dx). So here xinc=1 and y computed
 In the region closer to (a, 0), dy/dx is negative but
abs(dy/dx) >1 . So here yinc=-1 and x computed
 We start from (0, b) set xinc=1 and compute y. We keep
track of when dy/dx=-1 and change to yinc=-1 and
compute x
Ellipse
 dy/dx= -b2x/a2y
 After every ‘setpixel’, check if -b2x/a2y >=-1. Whenever this fails, we make
yinc=-1 and compute x.
simell1(0,0,a,b,c)
x=0, y=b, setpixel(o,b,c), setpixel(0,-b,c), xinc=1
while (-b2x/a2y >=-1)
x=x+xinc, y= round(sqrt(1-x2/a2)b2)
setpixel(x,y,c), setpixel(x,-y,c)
setpixel(-x,-y,c), setpixel(-x,y,c)
endwhile
yinc=-1
while(y>0)
y=y+yinc, x= round(sqrt(1-y2/b2)a2)
setpixel(x,y,c), setpixel(x,-y,c)
setpixel(-x,-y,c), setpixel(-x,y,c)
endwhile
end simell1
Ellipse
 Ellipse with center (h,k) axes parallel to coordinate axes and lengths of major
and minor axes 2a, 2b
simell2(h,k,a,b,c)
x=0, y=b, setpixel(o+h,b+k,c), setpixel(0+h,-b+k,c), xinc=1
while (-b2x/a2y >=-1)
x=x+xinc, y= round(sqrt(1-x2/a2)b2)
setpixel(x+h,y+k,c), setpixel(x+h,-y+k,c)
setpixel(-x+h,-y+k,c), setpixel(-x+h,y+k,c)
endwhile
yinc=-1
while(y>0)
y=y+yinc, x= round(sqrt(1-y2/b2)a2)
setpixel(x+h,y+k,c), setpixel(x+h,-y+k,c)
setpixel(-x+h,-y+k,c), setpixel(-x+h,y+k,c)
endwhile
end simell2
Polar ellipse
 Polar coordinates of any point on the ellipse is (acos,
bsin)
polarellipse1(0,0,a,b,c)
pi=3.14, thetainc=0.01
for theta=0 to pi/2 in steps of thetainc
x=round(a*cos(theta)), y= round(b*sin(theta))
setpixel(x,y,c), setpixel(x, -y,c)
setpixel(-x,-y,c), setpixel(-x, y, c)
endfor
end polarellipse1
Polar ellipse
 General ellipse generation
polarellipse2(h,k,a,b,c)
pi=3.14, thetainc=0.01
for theta=0 to pi/2 in steps of thetainc
x=round(a*cos(theta)), y= round(b*sin(theta))
setpixel(x+h,y+k,c), setpixel(x+h, -y+k,c)
setpixel(-x+h,-y+k,c), setpixel(-x+h, y+k, c)
endfor
end polarellipse2
Efficient ellipse generation
 Generation of ellipse using decision parameter updation is
possible as done in midpoint circle algorithm. This method
does not require square root computation. The ellipse (1)
has (0, 0) as centre and major, minor axes parallel to co
ordinate axes and of lengths 2a, 2b (figure in left). Ellipse
(2) with centre at (h, k) and axes parallel to coordinate
axes can be drawn by adding h, k to the calculated pixels
with centre at (0, 0). Ellipse (3) with at origin but axes not
parallel to co ordinate axes are rotated version of ellipse
(1). Ellipse with centre (h, k) and axes not parallel to
coordinate axes can be plotted by translating ellipse (3)
Plotting different ellipses
(x+h,y+k)

(x,y)

translation

Ellipse with centre (h,k)


rotation
(x+h,y+k)
(x,y)

translation

Ellipse with center (0, 0)


Ellipse with center (h, k)
Axes not parallel to co-odt axes
Axes not parallel to co-odt axes
Midpoint ellipse
 Let f(x, y)= x2/a2+y2/b2-1 =(b2x2+a2y2 -a2b2)/(a2b2)
 If f(x, y) <, =, > 0 then (x, y) is inside, on, outside the ellipse
 Sign of f(x, y) decide the position of (x, y) and denominator is always
positive. So f(x, y) is taken as b2x2+a2y2 -a2b2
 Suppose that k points are generated by the algorithm in region 1 and the
kth point be (xk, yk)
 xk+1 =xk + 1 and yk+1 =yk or yk -1
 y is such that yk -1 ≤ y ≤ yk
 Possible (k+1)th pixels are (xk + 1, yk ) and (xk + 1, yk -1)
 Midpoint of these two pixels is (xk + 1, yk -1/2)
 Sign of f(xk + 1, yk -1/2) decides the next pixel
 f(xk + 1, yk -1/2) = b2(xk + 1)2+a2(yk -1/2)2 -a2b2
Midpoint ellipse
 Case 1
 pk>=0
 Then midpoint is outside. So the point inside is
close to circle.
 xk+1=xk+1 and yk+1=yk-1
 pk+1 = b2(xk+1 + 1)2+a2( yk+1 -1/2)2- a2b2 =b2(xk +
1+1)2+a2( yk -1-1/2)2 -a2b2 = pk+ 2b2(xk+1) -2a2
(yk+1) + b2 (Prove!)
Midpoint ellipse
 Case 2
 Pk<0
 Then midpoint is inside. So the point outside is
close to circle.
 xk+1=xk+1 and yk+1=yk
 pk+1 = b2(xk+1 + 1)2+a2( yk+1 -1/2)2- a2b2 =b2(xk +
1+1)2+a2( yk -1/2)2 -a2b2 = pk+2b2(xk+1)+ b2
(Prove!)
Midpoint ellipse
 Starting ‘p’ in region 1
 Ellipse is drawn from (0, b). This is x1, y1
 The next pixel is (1, b-1) or (1, b)
 p1= b2(x1 + 1)2+a2(y1 -1/2)2 -a2b2 = b2(0 + 1)2+a2(b
-1/2)2 -a2b2 = b2+a2(b2 –b +1/4) -a2b2 = b2+ a2b2
-a2b +(1/4)a2 -a2b2 = b2 +(1/4)a2 -a2b
 We need to calculate similar expressions
(updation on decision parameter, starting ‘p’ for
region 2
Midpoint ellipse
 Suppose that k points are generated by the algorithm
in region 2 and the kth point be (xk, yk)
 xk+1 =xk + 1 or xk and yk+1 = yk -1
 x is such that xk ≤ x ≤ xk+1
 Possible (k+1)th pixels are (xk + 1, yk -1 ) and (xk , yk
-1)
 Midpoint of these two pixels is (xk + 1/2, yk -1)
 Sign of f(xk + 1/2, yk -1) decides the next pixel
 f(xk + 1/2, yk -1) = b2(xk + 1/2)2+a2(yk -1)2 -a2b2
Midpoint ellipse
 Case 1
 qk>=0
 Then midpoint is outside. So the point inside is
close to circle.
 xk+1=xk and yk+1=yk-1
 qk+1 = b2(xk+1 + 1/2)2+a2( yk+1 -1)2- a2b2 =b2(xk +
1/2)2+a2( yk -1-1)2 -a2b2 = qk-2a2(yk+1) +a2
(Prove!)
Midpoint ellipse
 Case 2
 qk<0
 Then midpoint is inside. So the point outside is
close to circle.
 xk+1=xk +1 and yk+1=yk-1
 qk+1 = b2(xk+1 + 1/2)2+a2( yk+1 -1)2- a2b2 =b2(xk +
1+1/2)2+a2( yk -1-1)2 -a2b2 = qk+2b2(xk+1)-
2a2(yk+1) +a2 (Prove!)
Midpoint ellipse
 Starting ‘q’ in region 2 is
 q1=b2(x1 + 1/2)2+a2(y1 -1)2 -a2b2 where (x1, y1)
is the last pixel generated in region 1
 Region 1 points start from (0, b) and upto
dy/dx >-1
 -b2x/a2y >-1 or b2x/a2y <1
 For region 2 the stopping condition is y >0
Midpoint ellipse

midptell (h,k,a,b,c)
x=0, y=b, p= b2 +(1/4)a2 -a2b
setpixel (x, y, c), setpixel (x, -y, c)
while (b2x/a2y <1)
x=x+1
if p>=0 then y=y-1, p=p+2b2x-2a2y+b2
else p=p+2b2x+ b2 , endif
setpixel(x+h, y+k, c), setpixel(-x+h, y+k, c)
setpixel(-x+h, -y+k, c), setpixel(x+h, -y+k, c)
end while
q= b2(x + 1/2)2+a2(y -1)2 -a2b2
for i=y to 1
y=y-1
if q >=0 then q=q-2a2y +a2
else x=x+1, q=q+2b2x-2a2y+a2 , endif
setpixel(x+h, y+k, c), setpixel(-x+h, y+k, c)
Drawing ellipse using midpoint ellipse
 Suppose centre is (10, 5), a= 4, b=3
 Plot (0+10, 4+5) and (0+10, -4+5)
 p= 9+(1/4)16 – 16*3=-35
 b2x/a2y = 0 <1
 x=1, current p=-35, p=-35+2*9*1+9=-8
 Points plotted are (1+10, 4+5), (-1+10, 4+5), (-1+10, -4+5), (1+10,
-4+5)
 b2x/a2y = 9/64 <1
 x=2, current p=-8, p=-8+2*9*2+9=37
 Points plotted are (2+10, 4+5), (-2+10, 4+5), (-2+10, -4+5), (2+10,
-4+5)
 b2x/a2y = 18/64 <1
 x=3, current p=37, y=3, p=37+2*9*3-2*16*3=-5
 Points plotted are (3+10, 3+5), (-3+10, 3+5), (-3+10, -3+5), (3+10,
-3+5)
Drawing ellipse using midpoint ellipse
 b2x/a2y = 27/48 <1
 x=4, current p=-5, p=-5+2*9*4+9=76
 Points plotted are (4+10, 3+5), (-4+10, 3+5),
 (-4+10, -3+5), (4+10, -3+5)
 b2x/a2y = 36/48 <1
 x=5, current p=76, y=2, p=76+2*9*5-2*16*2+9=111
 Points plotted are (5+10, 2+5), (-5+10, 2+5),
 (-5+10, -2+5), (5+10, -2+5)
 b2x/a2y = 45/32 is not <1
 q= 9*(5.5*5.5)+16(1)-16*9=144.25
Drawing ellipse using midpoint ellipse
 i=2
 y=1
 Current q=144.25 > 0, q=144.25-2*16+16=128.25
 Points plotted are ( 5+10, 1+5), (-5+10, 1+5), (-5+10,
-1+5), (5+10, -1+5)
 i=1
 y=0
 Current q=128.5 > 0, q=128.25-2*16*0+16=144.25
 Points plotted are ( 5+10, 0+5), (-5+10, 0+5), (-5+10,
-0+5), (5+10, -0+5)
Other curves
 Curve functions are useful for
 Modeling objects, setting paths for animation, data graphing etc
 Common curves are
 Conics, polynomials, trigonometric functions, probability distributions, spline
functions etc
 Methods to draw curves
 Use equations, find efficient methods such as the ones for circles and ellipse,
approximate using straight line segments-intermediary points are needed
and can be found using parametric equations, etc
 Second degree conic sections are described by
 F(x, y)= Ax2+Bxy+Cy2+Dx+Ey+F=0
 We get ellipse, parabola and hyperbolas if B2-4AC (discriminant) is <, =, > 0
 The equation becomes a circle when A=C, B=0, D=-2x1, E=-2y1, F=x12+y12-r2
 The equation becomes a line when A=B=C=0
Filled area primitives
 User may want to fill a closed region
 Three algorithms discussed
 Boundary fill, Flood fill, Scan line fill
 Boundary and flood fill take a seed point
inside the region to be painted
 Boundary fill requires a uniform color
boundary such as not
Boundary fill
bdryfill(x, y, fc, bc)
cc=getpixel(x, y)
if cc fc and cc  bc
setpixel (x, y, fc)
bdryfill(x+1, y, fc, bc)
bdryfill(x-1, y, fc, bc)
bdry(x, y+1, fc, bc)
bdryfill(x, y-1, fc, bc)
endif
end bdryfill
Recursive calls can be made with eight neighbors of (x, y) instead of 4
neighbors as shown in the algorithm
Simulation of boundary fill
****
**** ****
**** ****
****
****
**** ****
**** ****
****
**** **** ****
The boundary is shown in blue and fill color be red.
Pixels in yellow are to filled with red and the seed is shown in magenta
Suppose that the top left pixel is (10, 10) then bottom right pixel is (13,
14)
Seed (12,11) is neither fc nor bc so it is colored with fc=red
The function is called with (13, 11), (11, 11), (12, 12), (12, 10)
Stack of calls (13, 11), (11, 11), (12, 12), (12, 10)
The top of stack is call to function with (12, 10). This is bc. No more
function calls are added to stack
Stack of calls (13, 11), (11, 11), (12, 12)
Function is called with (12, 12). This is neither fc nor bc. So colored red.
Function calls with (13, 12), (11, 12), (12, 13), (12, 11) generated and
added to stack
Simulation of boundary fill
****
**** ****
**** ****
****
****
**** ****
**** ****
****
**** **** ****
Stack of calls (13, 11), (11, 11), (13, 12), (11, 12), (12, 13), (12, 11)
Function called with (12, 11). This is cored with fc. No more calls to
function.
Stack of calls (13, 11), (11, 11), (13, 12), (11, 12), (12, 13)
Function called with (12, 13). This is neither bc nor fc. So painted red
and more calls added to stack.
Stack has (13, 11), (11, 11), (13, 12), (11, 12), (13, 13), (11, 13), (12,
14), (12, 11)
(12, 11) is fc, (12, 14) is bc. (11, 13) is neither fc nor bc so painted
red and the stack has
(13, 11), (11, 11), (13, 12), (11, 12), (13, 13), (12, 13), (10, 13), (11,
14), (11, 12)
****
****
Simulation
****
****
****
****
of boundary fill
**** **** ****
****
**** ****
**** ****
****
Function called with (11, 12) and this pixel is colored and
more calls added
Stack has (13, 11), (11, 11), (13, 12), (11, 12), (13, 13),
(12, 13), (10, 13), (11, 14), (12, 12), (10, 12), (11, 13),
(11, 11)
(11, 11) is painted red and more calls are added. By this
time all pixels in the stack are either fc or bc. So all calls
from stack removed in order and found to be of fc or bc
and stack becomes empty and function terminates
Flood fill
floodfill(x, y, fc, oc)
cc=getpixel (x, y)
if cc = oc
setpixel (x, y, fc)
floodfill(x+1, y, fc, oc)
floodfill(x-1, y, fc, oc)
floodfill (x, y+1, fc, oc)
floodfill (x, y-1, fc, oc)
endif
end floodfill
oc is the background color and fc is the fill color
Scan line fill
 Suitable for filling polygons (boundaries are
lines)
 Principle is to select all scan lines (horizontal
lines) and find points of intersections with
polygon and identify which part of the line is
to be painted

X=0 X=XMAX
Scan line fill
SOME CONCAVE POLYGONS

In the scan line there are 5 points of


In top scan line there are 4 points of intersections. Vertex counted as two. If
intersections. Color and not color we color and not color alternatively ,
alternatively . Bottom scan line goes then color is not proper. Here the
through vertex. Count this as two vertex is intersection of edges with
points of intersections. Now do the increasing and decreasing y. If so count
color and not coloring alternatively. this as one point of intersection. Then
The vertex is colored do color and no color
Scan line fill

With correct count of


points of intersection (4)
points and alternate
coloring we get the right
area colored
Questions
1. Derive all line, circle, ellipse drawing
algorithms and write an algorithm for these.
2. Describe boundary, flood and scan line fill
procedures. Write algorithms.
3. Trace all line, circle, ellipse and fill
algorithms.
Lab problems
1. DDA
2. Bresenham - for all lines
3. Simple circle-square root, trigonometric
4. Bresenham circle
5. Mid point circle
6. Ellipse-floating point and integer
7. Filling -all 3 methods
Line
 DDA

Das könnte Ihnen auch gefallen