chapter 2 - wordpress.com … · web viewscan-converting a point . a mathematical point (x,y)...
TRANSCRIPT
Chapter 2OUTPUT PRIMITIVE
Somnath[Pick the date]
2Chapter2
Scan Conversion
The screen of a computer is divided into rows and columns. The intersection area of these rows and columns is known as a pixel. Scan conversion is the process of creating a pattern of points identical to the objects to be displayed. Each pixel on the display surface has a finite size depending on the screen resolution and hence a pixel cannot represent a single point. However, we consider each pixel as unit square area identified by the coordinate with respect to lower (or upper) left corner.
Scan-converting a point
A mathematical point (x,y) where x and y are real numbers within an image area, needs to be scan converted to a pixel at location (x/,y/). This may be done by making x/ to be the integer part of x and y/ the integer part of y. In other words, x/=Floor(x) and y/=floor(y), where function Floor returns the largest integer that is less than or equal to the argument. All points that satisfy x /≤ x<x/+1 and y/≤y<y+1 are mapped to a pixel (x/,y/). For example, point P1 (1.7,0.8) is represented by pixel (1,0). Points P2 (2.2, 1.3) and P3 (2.8,1.9) are both represented by pixel (2,1).
In another approach we can convert (x,y) by making x/=Floor(x+.5) and y/=Floor(y+.5). All points that satisfy x/-0.5≤x<x/+0.5 and y/-0.5≤y<y/+0.5 are mapped to pixel (x/,y/). This means that points P1 and P2 are now both
3Chapter2
represented by pixel (2,1), whereas point P3 is represented by pixel (3,2).
Scan converting a line
In order to draw a line on the screen, first we have to determine which pixels are to be switched ON. The process of determining which combination of pixels provides the best approximation to the desired line is known as Rasterization. The choice of pixels is determined by the orientation of the line which is to be drawn. There is little difficulty in selecting the pixels for horizontal, vertical and diagonal lines. For any other orientation of lines, selection of pixels is a difficult process.
General requirement for drawing a line
Lines must appear to be straight Lines should start and end accurately Lines should have constant brightness
along their length.
4Chapter2
Lines should be drawn rapidly.
On the raster scan systems, lines are plotted with pixels and step sizes in the horizontal and vertical directions are constrained by pixel separations. We must sample a line at discrete positions and determine the nearest pixel to the line at each sample position. This is decided by the equation of a straight line.
Equation for a straight line is,
y=mx+c where m is the slope of the line and c is the y-intercept
m= =
DDA Algorithm
Let the line is to be drawn between the end points (xstart,ystart) and (xend and yend) . Let the equation of the line as y=mx+c where m is slope of the line and c is the intercept. The slope can be expressed as
m=
In fact any two consecutive points (xi,yi) and (xi+1,yi+1) lying on this line segment should
5Chapter2
satisfy the equation m= if we say yi+1-
yi=Δy and xi+1-xi=Δx then
So Δy=mΔx
Thus for a given x interval Δx along the line , we can compute the corresponding y interval Δy. Now if Δx=1 i.e. xi+1-xi=1 or xi+1=xi+1 then Δy=m which implies yi+1=yi+m.
Thus a unit change in x changes y by m which is a constant for a given line. We know that if xi+1=xi+1, then yi+1=yi+m, the values of x and y are defined in terms of their previous values. Initializing (xi,yi) as (xstart,ystart) the line can be generated by incrementing the previous x values by one unit and solving the corresponding y value at each step till xend is reached. At each step we make incremental based on the previous step. This is what is defined as incremental algorithm and often referred to as the Digital Differential Analyzer (DDA) algorithm.
While incrementing the values of y by constant m, in this DDA method we have to keep in mind that m being the slope of the line, it can be any
6Chapter2
real number negative or positive – so that the calculated y values must be round off to the nearest integer for the sake of plotting on the screen.
But the question is why not increment y values by 1 instead of x and accordingly calculate x values from the line’s equation. The determining factor here is absolute value of the slope of the line |m|. If |m|≤1 which implies |Δy|≤|Δx| we sample the line at unit x intervals. But if |m|˃1 means |Δy|>|Δx| a unit step in x creates a step in y that is greater than 1, which is not desirable. In that case we reverse the roles of x and y by sampling at unit y intervals as
Δy=yi+1-yi=1 so yi+1=yi+1 and Δx= , xi+1=xi+
Both the equations are based on the assumption that xstart<xend and ystart<yend i.e. slope is positive. But if it is not so then we have to apply negative increment
7Chapter2
The Pseudo code for rasterizing a line according to DDA is presented below.
ALGORITHM
Step 1: Initialize(xstart,ystart), (xend,yend) if abs(xend – xstart) ≥ abs(yend – ystart) then len = abs(xend – xstart) else len = abs(yend – ystart) Δx = (xend – xstart) / len Δy = (yend – ystart) / len x = xstart
y = ystart
Step 2: begin main loop i=1 while( i≤ len) Setpixel (Round(x),Round(y)) x = (x + Δx) y = (y + Δy) i = i + 1 end while end
Advantage
The DDA algorithm is faster method for calculating pixel positions.
It eliminates the multiplication by making use of raster characteristics, so that appropriate increments are applied in the x or y direction to step to pixel positions along the line path
8Chapter2
Drawback
Although DDA is fast, the accumulation of round-off error in successive additions of floating point increment, however can cause the calculated pixel position to drift away from the true line path for long line segment.
Floating point operation and rounding off in DDA is time-consuming.
Mathematical Example
Consider a line from (0,0) to (6,7). Use DDA algorithm to rasterize this line.
Solution Initializations: (xstart,ystart) = (0,0) (xend,yend) = (6,7)
(xend – xstart)=6 ,(yend – ystart) = 7 so len=7
Δx=6/7=0.857 Δy=7/7=1
x=0 y=0Plotting begins
9Chapter2
Bresenham’s Line Algorithm
The algorithm avoids the round function and scan converts lines using only incremental integer calculation. The algorithm samples a line by incrementing one unit either x or y depending on the slope of the line and then selecting the pixels which one is close to the true line.
Let us consider a line L with a positive slope less than 1. Also consider that xi,yi be the point which is already selected to be displayed over the line
10Chapter2
segment. The next point to be select over the line is obviously xi+1 in the pixel grid. The question is whether it is (xi+1,yi+1) or (xi+1,yi) i.e. A or B. let C be the intersection of the line with the pixel grid with x=xi+1. In Bresenham’s approach the distance AC and BC is computed and the sign of their difference is used to select the pixel. Whose distance from C is smaller is selected as the best approximation.
Let the vertical distance from A to C is denoted by d2 and from B to C is denoted by d1. If C is a point on the true line path the x coordinate of the point c is clearly xi+1. The y coordinate of the point can be obtained by the equation of the line as y=m(xi+1)+c considering the equation of the line in the standard slope intercept form , y=mx+c.Then d1=y-yi= m(xi+1)+c-yi and d2=(yi+1)-y=yi+1- m(xi+1)-cThe difference between the two distances is d1-d2=2m(xi+1)-2yi+2c-1
Substituting m= where Δy and Δx are vertical
and horizontal separations between the end points of the line. Substituting the value of m we haveΔx(d1-d2)=2Δy(xi)-2Δx(yi)+2Δy+2cΔx-ΔxSince Δx>0 , Δx(d1-d2) has same sign as (d1-d2). Therefore Δx(d1-d2) can be used as decision parameter for choosing the pixel (xi+1,yi+1) next to (xi,yi).We call this Δx(d1-d2) as a parameter Pi.
If Pi>0 then d1>d2 hence we select A with the coordinate (xi+1,yi+1).If Pi<0 then d1<d2 hence we select B with the coordinate (xi+1,yi) .
11Chapter2
If Pi=0 then d1=d2 hence we select either A or B.It is interesting to note that the value of the decision parameter Pi and therefore the location of the next pixel (xi+2,yi+2) depends upon our choice of pixel A or B.The parameter Pi+1 that decides the pixel (xi+2,yi+2) can be evaluated in similar manner.Pi+1=2Δy(xi+1)-2Δx(yi+1)+2Δy+2cΔx-ΔxSubtracting Pi from Pi+1 we get the incremental differencePi+1-Pi=2Δy(xi+1-xi)-2Δx(yi+1-yi)Pi+1=Pi+2Δy(xi+1-xi)-2Δx(yi+1-yi)= Pi+2Δy-2Δx(yi+1-yi) since xi+1=xi+1.If A is chosen then yi+1-yi=1 so Pi+1=Pi+2Δy-2ΔxElse if B is chosen then yi+1-yi=0 so Pi+1=Pi+2ΔyNow checking the sign of Pi+1 the choice can be made for the next pixel (xi+2,yi+2) i.e. whether the y coordinate of the next pixel is incremented or not.The process is continued until the end point of the line is reached.Now in case of every line the first point is specified. Let it be (x0,y0). The initial decision parameter can be easily calculated in terms of the starting point. Let P0 be the initial decision parameter.As (x0,y0)is over the line then we can say c=y0-
mx0 and 2cΔx=2y0Δx-2x0Δy substituting m as
Now from the equation of Pi substituting i as 0 we haveP0=2Δy(x0)-2Δx(y0)+2Δy+2y0Δx-2x0Δy-Δx = 2Δy-Δx
Algorithm (for 0<m<1)
12Chapter2
Step 1: Set the pixel (x0,y0) i.e. the starting point. Calculate the horizontal separation Δx and vertical separation Δy of the given end point.Step 2 : Calculate P0=2Δy-ΔxStep 3: At each xi along the line starting at i=0, check the sign of the decision parameter Pi. If Pi<0 then the next point to be plot (x i+1,yi) and Pi+1=Pi+2Δy otherwise the next point to be plot is (xi+1,yi+1) and Pi+1=Pi+2Δy-2Δx. Set i=i+1Step 4: repeat step 3 as long as i<Δx
Note: This version of the Bresenham’s algorithm work for slope between 0 and 1. A little modification is needed for make it apply for lines having other tendency.
Advantages:• Accurate• Efficient• Integer Calculations• Uses Symmetry.• Adapted to display circles, ellipses
and curves.• It has been proven that the
algorithm gives an optimal fit for lines
Disadvantage:• Cannot generalize to arbitrary conics.
Example: Scan convert a line between the point (0,2) and (4,5) using Bresenham’s approach.
Here (x0,y0)=(0,2) , Δy=5-2=3 ,Δx=4-0=4
13Chapter2
Slope m= <1
The successive decision parameter Pi and the corresponding pixel position (xi+1,yi+1) are as followsP0=2Δy-Δx=2>0 so x1=1,y1=y0+1=3P1=P0+2Δy-2Δx=2+2x3-2x4=0So x2=2,y2=y1+1=4P2=P1+2Δy-2Δx=0+2x3-2x4=-2<0 so x3=3 and y3=y2=4P3=P2+2Δy=-2+2x3=4>0 so x4=4 and y4=y3+1=5
We have to stop here because we reach at (4,5) the end point
The result is tabulated as follows
Generalized Bresenham’s Algorithm
A Possible approach of Bresenham’s algorithm is to consider the pixel coordinate in different way for the lines having slope other than between 0 and 1. When the absolute magnitude of the slope of the line is greater than 1, we step along the direction of Y instead of X in unit steps and calculate successive X values using the algorithm.
Unit sampling direction
14Chapter2
However for negative slope s the procedure is imilar except that now one coordinates decreases as the other increases from start point to the end point of such lines. Therefore the decreasing coordinate is incremented by -1 instead of +1 in Bresenham’s logic.
Considering all the cases a generalized version of the Bresenham’s algorithm (Pseudocode) is given below.
The line end points are (xs,ys) and (xe,ye).Initialize variables x=xs,y=ys
15Chapter2
Δx=abs(xe-xs), Δy=abs(ye-ys) S1= sign(xe-xs), s2= sign(ye-ys) // sign function return +1 or -1 according to positive or negativeif Δy>Δx then
t=Δx Δx=Δy Δy=t flag=1 else flag=0end if
n=1P=2Δy-ΔxSetpixel(x,y)while(n≤Δx) if P≥0 then x=x+s1
y=y+s2
P=P+2(Δy-Δx) Else if flag=1 then y=y+s2
else x=x+s1
end if P=P+2Δyend if ,setpixel(x,y)n=n+1end while
Scan converting a circle
Approach of generating a circle is based on the symmetric property of the circle to plot eight points for each value that the algorithm calculates. A circle is a symmetric figure. Eight-
16Chapter2
way symmetric is used by reflecting each calculated point around each 45º axis.
Symmetry about the pair of lines with slopes of one and minus one
We can find any point's symmetric complement about these lines by permuting the indices. For example the point (x,y) has a complementary point (y,x) about the line x=y. And the total set of complements for the point (x,y) are
Symmetric complement of the point’s can be obtained easily about these lines by permuting the indices only : {(x,-y), (-x,y), (-x,-y), (y,x), (y,-x), (-y,x),(-y,-x)} as the equation of the circle is
(±x)2+(±y)2=r2 with center at (0,0) and with radius r.
17Chapter2
Bresenham’s circle drawing algorithm
It uses eight-way symmetric property of a circle points over the entire circle. Initially we generate the points over the angle 90º to 45º where moves are made in the positive x direction and negative y direction. It is to be noted that while plotting the points over the said angle we have to take two actions
Move in the positive x direction by one unit.
Move in the positive x direction by one unit and in the negative y direction by one unit as well.
These two choices are necessary to generate all the points’ closest to the true circle over the angle 90º to 45º.
Xi+1,Yi
Xi+1,yi-1
(T)
(S)
18Chapter2
Assume that (xi,yi) are the coordinates of the point which is already to be selected over the true circle. Let the distance from the origin to pixel T squared minus the distance to the true circle squared =D(T) and the distance from the origin to the pixel S squared minus the distance to the true circle squared=D(S). As the coordinate of T are (xi+1,yi) and those of S are (xi+1,yi-1), the following expression can be developed : D(T)=(xi+1)2+yi
2-r2 and D(S)=(xi+1)2+(yi-1)2-r2 The function D provides a relative measurement of the distance from the center of a pixel to the true circle. Since D(T) is always positive and D(S) is always negative a decision variable di
may be defined as follows. di=D(T)+D(S) so di=2(xi+1)2+yi
2+(yi-1)2-2r2 When di<0 we have |D(T)|<|D(S)| and pixel T is chosen. When di≥0, we have |D(T)|≥|D(S)| and pixel S is selected. We can also write the decision variable di+1 for the next step di+1=2(xi+1+1)2+yi+1
2+(yi+1-1)2-2r2
19Chapter2
Hence di+1-di= 2(xi+1+1)2+yi+12+(yi+1-1)2 -2(xi+1)2-
yi2-(yi-1)2
Since xi+1=xi+1 we have di+1=di+4xi+2(yi+12-yi
2)-2(yi+1-yi)+6If T is chosen i.e. if di<0 then yi+1=yi and so di+1=di+4xi+6On the other hand if S is chose i.e. di≥0 then yi+1=yi -1 and so di+1=di+4(xi-yi) +10Hence we have di+1= di+1=di+4xi+6 if di<0 or di+1= di+1=di+4(xi-yi)+10 if di≥0The plotting begins at a point over the y axis where the x coordinate is =0 and x coordinate is obviously =r because the radius of the circle is =r. If we consider the polar coordinate (r,Ө) of circle then x=r cosӨ and y=r sinӨ ,if we put Ө=90° then we have x=0 and y=r as the beginning point to be plot. As the starting point of the algorithm is the pixel (0,r) we have the initial decision parameter d1=2(0+1)2+r2+(r-1)2-2r2=3-2r
The algorithm is as follows:
Step 1: Input radius r of the circle and the first point on the circumference of a circle centered on the origin as x=0,y=r
Step2: Calculate the initial value of the decision parameter as d=3-2r
Step 3: Setpixel (x,y)Step4: At each x position starting from the initial
point perform the following test :if d<0 then
d=d+4x+6; x=x+1
20Chapter2
Otherwise d=d+4(x-y)+10; x=x+1;y=y-1
Step 4: Repeat step 2 to 3 until x≥y
note : The circle is drawn only over the angle
from 90º to 45º. For other part of the circle we have to apply the eight way symmetric property of the circle.
Remember that the circle is centered at origin. For any arbitrary centered circle we have to apply transformation over the entire circle. For the circle with center at xc,yc we have to plot the coordinate as x=x+xc and y=y+yc
Pseudocode for the algorithm:
int x=0,y=r,d=3-2r, center(xc,yc);while (x≤y) { setpixel(x+xc,y+yc); setpixel(-x+xc,-y+yc); setpixel(y+xc,x+yc); setpixel(y+xc,-x+yc); setpixel(-y+xc,x+yc); setpixel(y+xc,-x+yc); setpixel(-x+xc,y+yc); setpixel(x+xc,-y+yc); if(d<0) d=d+4x+6; else { d=d+4(x-y)+10; y--; }
21Chapter2
x++; }Example:
Consider an origin centered circle of radius 10. Determine the pixel that will be put ON for the first quadrant only.Initialization:x=0,y=10,d=3-2x10=-17Plotting begins
Midpoint Circle AlgorithmThis procedure describes another incremental circle algorithm similar to Bresenham’s approach. It is based on the following function for testing the spatial relationship between an arbitrary point (x,y) and a circle of radius r centered at the origin:
22Chapter2
f(x,y)=x2+y2-r2
Let us now consider the coordinate of the point midway between the pixel T and the pixel S (xi+1,yi-½) and with the midpoint we define a decision parameter as pi=f(xi+1,yi-½)=(xi+1)2+(yi-½)2-r2 If pi is negative the midpoint is inside the circle and we choose the pixel T. on the other hand if p i
is positive the midpoint is outside the circle and we choose pixel S. the decision parameter for the next step is pi+1=(xi+1+1)2+(yi-½)2-r2 Since xi+1=xi+1 we have pi+1-pi=[(xi+1)+1]2-(xi+1)2+(yi+1-½)2-(yi-½)2
Hence pi+1=pi+2(xi+1)+1+(yi+12-yi
2)-(yi+1-yi)
23Chapter2
If pixel T is chosen (meaning pi<0) we have yi+1=yi . on the other hand if pixel S is chosen (meaning pi≥0) , we have yi+1=yi-1 so we have
pi+1=
The initial value of the decision parameter p i can be obtained by taking the value of xi=0 and yi=r
pi=(0+1)2+(r-½)2-r2= -r,
However we can set the initial decision parameter p1=1-r for integer computation. It does not affect the scan conversion process because the decision parameter is used only for integer computation of the subsequent coordinates.
The algorithm is as follows:Step 1: Input radius r of the circle and the first point on the circumference of a circle centered on the origin as x=0,y=r
Step2: Calculate the initial value of the decision parameter as p=1-r
Step 3: Set pixel (x, y)Step4: At each x position starting from the initial point
perform the following test: if p<0 then p=p+2x+3; x=x+1 Otherwise p=p+2(x-y)+5; x=x+1;y=y-
1Step 4: Repeat step 2 to 3 until x≤y
24Chapter2
Note: when eight-way symmetric is used to obtain a full circle from pixel coordinate generated for the 0° to 45° or the 90° to 45° octant certain pixels are set or plotted twice. This phenomenon is sometimes referred to as overstrike. Over striking is not harmful because resetting a pixel with the same value does not really change the image of the frame buffer. But it is wasting of time because some pixels are drawn twice
Pseudocode for the algorithm:
int x=0,y=r,p=1-r, center(xc,yc);while (x≤y) { setpixel(x+xc,y+yc); setpixel(-x+xc,-y+yc); setpixel(y+xc,x+yc); setpixel(y+xc,-x+yc); setpixel(-y+xc,x+yc); setpixel(y+xc,-x+yc); setpixel(-x+xc,y+yc); setpixel(x+xc,-y+yc); if(p<0) p=p+2x+3; else { p=d+2(x-y)+5; y--; }
25Chapter2
x++; }
Example:Consider an origin centered circle of radius 10. Determine the pixel that will be put ON for the first quadrant only.
Initialization:x=0,y=10,d=1-10=-9Plotting begins
Midpoint Ellipse Algorithm
Midpoint ellipse algorithm is a method for drawing ellipses in computer graphics. This method is modified from Bresenham’s algorithm. The advantage of this modified method is that only addition operations are
26Chapter2
required in the program loops. This leads to simple and fast implementation in all processors.Let us consider one quarter of an ellipse. The curve is divided into two regions. In region I, the slope on the curve is greater than –1 while in region II less than –1.
Consider the general equation of an ellipse, b2x2
+ a2y2 – a2b2 = 0
Where a is the horizontal radius and b is the vertical radius (major and minor axis), we can define an function f(x,y) by which the error due to a prediction coordinate (x,y) can be obtained. The appropriate pixels can be selected according to the error so that the required ellipse is formed. The error can be confined within half a pixel.Set f(x,y) = b2x2 + a2y2 – a2b2
In region I (dy/dx > –1),
27Chapter2
x is always incremented in each step, i.e. xk+1 = xk
+ 1.yk+1 = yk if E is selected, or yk+1 = yk – 1 if SE is selected.In order to make decision between E and SE, a prediction (xk+1, yk–½) is set at the middle between the two candidate pixels. A prediction function Pk can be defined as follows:
Pk = f(xk+1, yk–½) = b2(xk+1)2 + a2(yk–½)2 – a2b2
= b2(xk2 + 2xk + 1) + a2(yk
2 – yk + ¼) – a2b2
If Pk < 0, midpoint is inside so select E : the next decision parameter is defined by
Pk+1E = f(xk+2, yk–½)
= b2(xk+2)2 + a2(yk–½)2 – a2b2
= b2(xk2 + 4xk + 4) + a2(yk
2 – yk + ¼) – a2b2
Change of PkE is: ΔPk
E = Pk+1E – Pk = b2(2xk + 3)
28Chapter2
If Pk > 0, mid point is outside select SE : the next decision parameter is defined byPk+1
SE = f(xk+2, yk–3/2)= b2(xk+2)2 + a2(yk–3/2)2 – a2b2
= b2(xk2 + 4xk + 4) + a2(yk
2 – 3yk + 9/4) – a2b2
Change of PkSE is ΔPk SE = Pk+1
SE – Pk = b2(2xk + 3) – 2a2(yk – 1)
Calculate the changes of ΔPk: If E is selected,ΔPk+1 E = b2(2xk + 5)∆2Pk
E = ∆Pk+1E – ∆Pk
E = 2b2
∆Pk+1SE
= b2(2xk + 5) – 2a2(yk – 1)∆2Pk
SE = ∆Pk+1SE
– ∆PkSE = 2b2
If SE is selected,Pk+1 E = b2(2xk + 5)2Pk
E = Pk+1E
– PkE = 2b2
Pk+1SE = b2(2xk + 5) – 2a2(yk – 2)
2PkSE = Pk+1
SE – PkSE = 2(a2 + b2)
Initial values:x0 = 0, y0 = b, P0 = b2 + ¼a2(1 – 4b)P0
E = 3b2, P0SE = 3b2 – 2a2(b – 1)
In region II (dy/dx < –1), all calculations are similar to that in region I except that y is decremented in each step.
29Chapter2
y is always decremented in each step, i.e. yk+1 = yk – 1.xk+1 = xk if S is selected, or xk+1 = xk + 1 if SE is selected.Pk = f(xk+½, yk–1)= b2(xk+½)2 + a2(yk–1)2 – a2b2
= b2(xk2 + xk + ¼) + a2(yk
2 – 2yk + 1) – a2b2
If Pk > 0, select S :Pk+1
S = f(xk+½, yk–2)= b2(xk+½)2 + a2(yk–2)2 – a2b2
= b2(xk2 + xk + ¼) + a2(yk
2 – 4yk + 4) – a2b2
Change of Pk S is: ∆Pk
S = Pk+1S – Pk = a2(3 – 2yk)
If Pk < 0, select SE :Pk+1
SE = f(xk+3/2, yk–2)= b2(xk+3/2)2 + a2(yk–2)2 – a2b2
= b2(xk2 + 3xk + 9/4) + a2(yk
2 – 4yk + 4) – a2b2
Change of PkSE is ∆Pk
SE = Pk+1SE – Pk = 2b2(xk + 1)
+ a2(3 – 2yk)Calculate the changes of∆Pk:If S is selected,∆Pk+1
S = a2(5 – 2yk)∆2Pk
S = ∆Pk+1S – ∆Pk
S = 2a2
30Chapter2
∆Pk+1SE = 2b2(xk + 1) + a2(5 – 2yk)
∆2PkSE = ∆Pk+1
SE – ∆PkSE = 2a2
If SE is selected,∆Pk+1
S = a2(5 – 2yk)∆2Pk
S = ∆Pk+1S – ∆Pk
S = 2a2
∆Pk+1SE = 2b2(2xk + 2) – a2(5 – 2yk)
Determine the boundary between region I and II:
Set f(x, y) = 0, when ,
x= and y=
At region I, , x< and y>
therefore
∆PkSE <b2 -2a2
=2a2+3b2
Initial value at region II:
x0 = and y0 =
x0 and y0 will be the accumulative results from region I at the boundary. It is not necessary to calculate them from values of a and b.
P = b2 (x+ )2 +a2(y-1)2- a2b2 at the boundary.
∆P0E = b2(2x0 + 3)
31Chapter2
∆P0SE = 2a2 + 3b2
Implementation of the algorithm:
The algorithm described above shows how to obtain the pixel coordinates in the first quarter only. The ellipse centre is assumed to be at the origin. In actual implementation, the pixel coordinates in other quarters can be simply obtained by use of the symmetric characteristics of an ellipse. For a pixel (x, y) in the first quarter, the corresponding pixels in other three quarters are (x, –y), (–x, y) and (–x, –y) respectively. If the centre is at (xC, yC), all calculated coordinate (x, y) should be adjusted by adding the offset (xC, yC). For easy implementation, a function
PlotEllipse() is defined as follows:PlotEllipse (xC, yC, x, y)
putpixel(xC+x, yC+y)putpixel(xC+x, yC–y)putpixel(xC–x, yC+y)putpixel(xC–x, yC–y)
end PlotEllipse
The function to draw an ellipse is described in the following pseudo-codes:
DrawEllipse (xC, yC, a, b)Declare integers x, y, P, ∆PE, ∆PS, ∆PSE, ∆2PE, ∆2PS and ∆2PSE
// Set initial values in region ISet x = 0 and y = b
32Chapter2
P = b2 + (a2(1 – 4b) – 2)/4 PE = 3b2
2PE = 2b2
PSE = PE – 2a2(b – 1)2PSE = 2PE + 2a2
// Plot the pixels in region I
PlotEllipse(xC, yC, x, y)while PSE < 2a2 + 3b2
if P < 0 then // Select EP = P + PE
PE = PE + 2PE
PSE = PSE + 2PE
else // Select SEP = P + PSE
PE = PE + 2PE
PSE = PSE + 2PSE
decrement yend ifincrement xPlotEllipse(xC, yC, x, y)end while
// Set initial values in region II
P = b2 (x+ )2 +a2(y-1)2- a2b2
PS = a2(3 – 2y)PSE = 2b2 + 3a2
2PS = 2a2
// Plot the pixels in region II
while y > 0if P > 0 then // Select S
P = P + PE
PE = PE + 2PS
33Chapter2
PSE = PSE + 2PS
else // Select SEP = P + PSE
PE = PE + 2PS
PSE = PSE + 2PSE
increment xend if
decrement yPlotEllipse(xC, yC, x, y)
end while
end DrawEllipse
ExerciseMultiple Choice Questions
1. It is desired to draw a line starting at A(3,6) and ending at B(6,2) using Bresenham’s line drawing algorithm, the value of the starting decision parameter is ? (a) 11 (b) -11 (c) 12 (d) None of these2. Midpoint line and circle algorithm uses the sign of (a) Distant parameter
34Chapter2
(b) Decision parameter (c) Describe point (d) both (A) & (B).3. What will be the value of starting Decision Parameter if we intend to draw a line between A ( 3, 6 ) and B ( 4, 9 ) using Bresenham's Algorithm ? (a) 6 (b) 5 (c) 3 (d) None of these.4. The initial decision parameter for Bresenham’s line algorithm is (a) 3Δy-Δx (b) 2Δx-Δy (c) 2Δy-Δx (d) 2Δy-3Δx5. The initial decision parameter for Bresenham’s circle algorithm is (a) 2-3r (b) 3-2r (c) 2r-3 (d) 3-r6. DDA stands for (a) Digital Differential Analyzer (b) Digital Distributed Analyzer (c) Digital Data Analyzer (d) None of these7. In the Bresenham’s Algorithm, error term initialized to (a) 0 (b) 1 (c) -1 (d) None of these
35Chapter2
8. In the generation of circle by Bresenham's algorithm, it is simple to generate (a) all octants (b) one octant first and others by successive reflection (c) one octant first and others' by successive rotation one (d) octant first and others by successive translation
Short Answer Type
1. What do you mean by 'symmetry of points' in a circle? How does it help in drawing a circle through an algorithm? 1+2
2. Explain what you understand by 8 point symmetry of a circle.
Descriptive Question
1. Write and explain Bresenham's algorithm for drawing a straight line. How does it remove the drawback of DDA algorithm?
2. Write mid-point circle drawing algorithm3.
a. Find the points required to plot to draw the circle with center ( 2 , 2 )
36Chapter2
and radius 3 using Bresenham's circle drawing algorithm.
Ans. Initialization:x=0,y=3,d=3-2x3=-3 if the center is at (0,0). Here we have to add 2 with the x component and y component of the coordinate as the center is at (2,2) at each successive steps while plotting the circle.Plotting begins
step (x,y) dInitial (0+2,3+2) -3
1 (1+2,3+2) 32 (2+2,2+2) 5
The loop terminates here because both x and y coordinates are same at the same. Now we have to use the 8-way symmetric property of a circle to plot the points of the other octants. b. Explain DDA algorithm. What are the
advantages of Bresenham's algorithm with respect to DDA?
4. Find the points required to plot to draw the circle with center as (100,90) and radius 10 using Bresenham’s circle drawing algorithm.
Ans.Initialization:x=0,y=10,d=3-2x3=-17 if the center is at (0,0). Here we have to add 2 with the x component and y component of the coordinate as the center is at (2,2) at each successive steps while plotting the circle.Plotting begins
step (x,y) d
37Chapter2
Initial (0+100,10+90) -171 (1+100,10+90) -112 (2+100,10+90) -13 (3+100,10+90) 134 (4+100,9+90) -55 (5+100,9+90) 176 (6+100,8+90) 77 (7+100,7+90) 9
The loop terminates here because both x and y coordinates are same at the same. Now we have to use the 8-way symmetric property of a circle to plot the points of the other octants.