39
Shri P. M. Agrawal Scan Conversion of Geometric Primitives Shri P. M. Agrawal Shri V. J. Patel

3. Scan Conversion of Geometric Primitives

Embed Size (px)

DESCRIPTION

CAD , Input devices

Citation preview

Page 1: 3. Scan Conversion of Geometric Primitives

Shri P. M. Agrawal

Scan Conversion of Geometric

Primitives

Shri P. M. AgrawalShriV. J. Patel

Page 2: 3. Scan Conversion of Geometric Primitives

Digital Differential Analyzer (DDA)� DDA is technique for scan conversion of lines. When a

straight line is imposed on the raster grid, it may not pass through the pixel locations. Then, need arises to compute the coordinates of pixel locations that are as close to the line as possible in case they do not lie on the line.possible in case they do not lie on the line.

� We assume that the approximated line is one pixel thick and has slope between -1 and 1. All lines should be drawn with constant brightness.

7 February 20112 P. M. Agrawal and V. J. Patel

Page 3: 3. Scan Conversion of Geometric Primitives

7 February 20113 P. M. Agrawal and V. J. Patel

Page 4: 3. Scan Conversion of Geometric Primitives

� Assuming that the scan-lines and columns pass through the centers of pixels and that the diameter of a pixel is smaller than the interpixel spacing, we have the modified raster grid as shown in next figure, in which pixels are located at the intersection of scan lines and columns. intersection of scan lines and columns.

� The desired line and the lighted pixels (shown green ) that approximate the line are shown in fig. 10.1 and 10.2.

� The discreteness of pixels is more obvious from fig. 10.2 than from fig. 10.1

7 February 20114 P. M. Agrawal and V. J. Patel

Page 5: 3. Scan Conversion of Geometric Primitives

7 February 20115 P. M. Agrawal and V. J. Patel

Page 6: 3. Scan Conversion of Geometric Primitives

� In DDA technique, we consider a line in slope intercept form: y = mx + b where |m|<= 1 is the slope given by m = ∆y/∆x and b is the intercept.

� We sample the line between (x0, y0) and (x1, y1) at unit intervals in one coordinate and determine the corresponding integer values for other coordinate in the path of the line.integer values for other coordinate in the path of the line.

� This computation selects the pixels closest to the true line. � Starting from the first end (left end) of the line, we proceed

to the second end (right end) by computing the intermediate pixel locations.

� Let yi and xi be the coordinates of ith pixel on the line.

7 February 20116 P. M. Agrawal and V. J. Patel

Page 7: 3. Scan Conversion of Geometric Primitives

7 February 20117 P. M. Agrawal and V. J. Patel

Page 8: 3. Scan Conversion of Geometric Primitives

The coordinates of (i+1) pixel satisfy the equation

Assuming that x-coordinate is sampled at unit intervals,

7 February 20118 P. M. Agrawal and V. J. Patel

Page 9: 3. Scan Conversion of Geometric Primitives

Above equation can be written as

Since m is any real number, the calculated y value must be rounded to the nearest integer

� If we start at the right end point, the following equations � If we start at the right end point, the following equations must be considered.

7 February 20119 P. M. Agrawal and V. J. Patel

Page 10: 3. Scan Conversion of Geometric Primitives

� If |m| > 1, we have to reverse the roles of x and y by sampling y and computing the corresponding x.

� If we start at the right endpoint, the above equations are modified to

7 February 201110 P. M. Agrawal and V. J. Patel

Page 11: 3. Scan Conversion of Geometric Primitives

Algorithm1. Compute m from the end points (x0, y0) and (x1, y1) of the

line

2. If |m|<= 1, continue for x0 to x1 else go to step 6.3. If the left end point is the start point, then use the 3. If the left end point is the start point, then use the

equations else go to step 4.

Set pixel (xI+1, round(yi+1))), Go to step 5

7 February 201111 P. M. Agrawal and V. J. Patel

Page 12: 3. Scan Conversion of Geometric Primitives

4. if the right end point is the start point, then use the equations,

Set pixel (xI+1, round(yi+1))), Go to step 5 5. If xi > x1 ,then go to 10.6. If |m| > 1 continue for y0 to y17. If the left end point is the start point, then use the equations else 7. If the left end point is the start point, then use the equations else

go to step 8.

Set pixel (round(xI+1), yi+1)), Go to step 9

7 February 201112 P. M. Agrawal and V. J. Patel

Page 13: 3. Scan Conversion of Geometric Primitives

8. if the right end point is the start point, then use the equations,

Set pixel (round(xI+1), yi+1)), Go to step 9

9. If yi > y1 ,then go to 10.10. End.

7 February 201113 P. M. Agrawal and V. J. Patel

Page 14: 3. Scan Conversion of Geometric Primitives

Bresenham’s Algorithm for lines� Bresenhanm’s formulations uses integer arithmetic and

allows calculation of pixel positions incrementally, thus avoiding the found function and floating point addition as in DDA technique.

� This formulation provides the best-fit approximation to the � This formulation provides the best-fit approximation to the range 0 to 1 and other slopes can be easily handled as explained in the above technique.

7 February 201114 P. M. Agrawal and V. J. Patel

Page 15: 3. Scan Conversion of Geometric Primitives

7 February 201115 P. M. Agrawal and V. J. Patel

Page 16: 3. Scan Conversion of Geometric Primitives

� Let P be the pixel with coordinates (xp, yp) as shown in fi. 10.4, we can designate its 8-neighbors by N (North), S(South), E(East), W(West), NE(North East), SE(South East), NW(North West) and SW(South West) with the coordinates� N(xp, yp+1)� S(xp, yp-1)� S(xp, yp-1)� E(xp+1, yp)� W(xp-1, yp)� NE(xp+1, yp+1)� SE(xp+1, yp-1)� NW(xp-1, yp+1)� SW(xp-1, yp-1)

7 February 201116 P. M. Agrawal and V. J. Patel

Page 17: 3. Scan Conversion of Geometric Primitives

� Assuming that the point P has been selected in the previous iteration to be on the desired line, the next choice is between the pixels E and NE depending on whichever is close to the intersection I of the desired line with the column xP+1 (Refer Fig. 10.5).

� The vertical distance ∆1 from I to E and ∆2 from I to Ne shown in � The vertical distance ∆1 from I to E and ∆2 from I to Ne shown in fig. 10.5 are calculated and the sign of their difference (∆1 - ∆2) is used to make a choice.

� If ∆1 < ∆2, sign (∆1 - ∆2) is negative, then E is selected as the current pixel.

� If ∆1 > ∆2, sign (∆1 - ∆2) is positive, then NE is selected as the current pixel.

7 February 201117 P. M. Agrawal and V. J. Patel

Page 18: 3. Scan Conversion of Geometric Primitives

7 February 2011P. M. Agrawal and V. J. Patel18

Page 19: 3. Scan Conversion of Geometric Primitives

The y-coordinate of I at the column (xP+1) isy = m(xP+1)+b

The distance of I from E is∆1 = y - yP

= m(xP+1)+b - yP

7 February 2011P. M. Agrawal and V. J. Patel19

= m(xP+1)+b - yP

The distance of I from NE is∆2 = (yP+1) – y

= yP+1 - m(xP+1) - b The difference between these distances is

∆1 - ∆2 = 2m(xP+1) - yP+ 2b - 1

Page 20: 3. Scan Conversion of Geometric Primitives

Substituting for m = (y1- y0)/(x1- x0) = ∆y/∆x in above equation yields

Let the decision parameter dp be defined as

7 February 2011P. M. Agrawal and V. J. Patel20

Let the decision parameter dp be defined asdp = ∆x (∆1 - ∆2) = 2 ∆y• yp - 2∆x• yp + c … 10.15

where c = 2• ∆y + ∆x•(2b - 1), is independent of pixel positions.

Since ∆x is positive, sign of dp is dictated by the sign of (∆1 -∆2). Hence, the decision on E and NE remains the same.

Page 21: 3. Scan Conversion of Geometric Primitives

If dp is negative, then E(xP+1, yP) is selected and the new decision parameter dp+1 is evaluated from 10.15 asdp+1 = 2• ∆y•xP+1 -2 ∆x• yP + c … 10.16

From eqs. 10.15 and 10.16, we obtaind - d = 2• ∆y• (x –x )

7 February 2011P. M. Agrawal and V. J. Patel21

dp+1 - dP = 2• ∆y• (xP+1 –xP) Since xP+1 = xP + 1, we can write above equation as

dp+1 = dP + 2• ∆y

Page 22: 3. Scan Conversion of Geometric Primitives

If dp is positive, then NE(xP+1, yP+1) is selected and the new decision parameter dp+1 is evaluated from 10.15 asdp+1 = 2• ∆y•xP+1 -2 ∆x• yP+1 + c … 10.19

From eqs. 10.15 and 10.19, we obtaindp+1 - dP = 2• ∆y• (xP+1 –xP) - 2• ∆x• (yP+1 –yP)

7 February 2011P. M. Agrawal and V. J. Patel22

dp+1 - dP = 2• ∆y• (xP+1 –xP) - 2• ∆x• (yP+1 –yP) Since, xP+1 = xP + 1 and yP+1 = yP + 1

dp+1 = dP + 2• (∆y - ∆x)� At the starting location (x0, y0), we have from eqn. 10.15 for

the initial decision parameterd0 = 2• ∆y - ∆x

Page 23: 3. Scan Conversion of Geometric Primitives

Algorithm1. Input the two end points (x0, y0) and (x1, y1).2. Calculate the constants ∆x, ∆y, 2∆x, 2∆y, 2(∆y - ∆x) and also

the initial decision parameter d0 .3. Plot the left end point (x0, y0) as the first point.4. If d0 < 0, the next point to plot is (xP+1, yP) with

x = x +1

7 February 2011P. M. Agrawal and V. J. Patel23

xP+1 = xP+1dp+1 = dP + 2• ∆y

If d0 > 0, the next point to plot is (xP+1, yP+1) withxP+1 = xP+1yP+1 = yP+1dp+1 = dP + 2• (∆y - ∆x)

5. Repeat step 4 until the right end point is reached.

Page 24: 3. Scan Conversion of Geometric Primitives

� The Bresenham’s algorithm can be generalized to lines with slopes greater then 1.

� In this case, we interchange the x and y coordinates. That is, we treat the original x-coordinate as y-coordinate and vice-versa.

� The algorithm can start from either endpoint.� If the initial position for a line is the right endpoint with positive

7 February 2011P. M. Agrawal and V. J. Patel24

� If the initial position for a line is the right endpoint with positive slope, then both x an y coordinates are decreased as we step from the right end point to the left end point.

� For negative slopes, one coordinate decreases as the other increase.

� Special cases involving horizontal lines, vertcal lines and the diagonal lines are dealt with separately by loading the values directly into the frame buffer as there is no need to apply the algorithm.

Page 25: 3. Scan Conversion of Geometric Primitives

DDA versus Bresenham’s Algorithm� DDA works with floating point arithmetic

� Rounding to integers necessary

� Bresenham’s algorithm uses integer arithmetic� Constants need to be computed only once

7 February 2011P. M. Agrawal and V. J. Patel25

� Constants need to be computed only once

� Bresenham’s algorithm generally faster than DDA

Page 26: 3. Scan Conversion of Geometric Primitives

Circle Generating algorithms� Direct Algorithm� Polar coordinate based Algorithm� Midpoint Algorithm (Bresenham’s Algorithm)

7 February 2011P. M. Agrawal and V. J. Patel26

Page 27: 3. Scan Conversion of Geometric Primitives

Direct Circle AlgorithmProperties of a circle:� A circle is defined as a set of points that are all the given distance

(xc,yc).This distance relationship is expressed by the pythagoreantheorem in Cartesian coordinates as

(x – xc)2 + (y – yc)

2 = r2

� We could use this equation to calculate the points on the circle

7 February 2011P. M. Agrawal and V. J. Patel27

� We could use this equation to calculate the points on the circle circumference by stepping along x-axis in unit steps from xc-r to xc+r and calculate the corresponding y values at each position as

y = yc +(- ) (r2 – (xc –x )2)1/2

� This is not the best method:� Considerable amount of computation� Spacing between plotted pixels is not uniform

Page 28: 3. Scan Conversion of Geometric Primitives

Polar Coordinated Based Circle

Algorithm� We could use polar coordinates r and θ,

x = xc + r cosθ y = yc + r sinθ� A fixed angular step size can be used to plot equally spaced

points along the circumference.� A step size of 1/r can be used to set pixel positions to

approximately 1 unit apart for a continuous boundary.

7 February 2011P. M. Agrawal and V. J. Patel28

approximately 1 unit apart for a continuous boundary.� But, note that circle sections in adjacent octants within one

quadrant are symmetric with respect to the 45 deg line dividing the to octants.

� Thus we can quickly generate all pixel positions around a circle by using this symmetricity.

� This method is still computationally expensive.

Page 29: 3. Scan Conversion of Geometric Primitives

Eight-way Symmetry

7 February 2011P. M. Agrawal and V. J. Patel29

Fig. 10.6

Page 30: 3. Scan Conversion of Geometric Primitives

Bresenham’s Circle Algorithm� Values of a circle centered at the origin are computed in the sector

X=0 to X=R/2.� The symmetry of the circle is used to obtain the pixels

corresponding to other sectors.� Consider an origin-centered circle.

7 February 2011P. M. Agrawal and V. J. Patel30

Consider an origin-centered circle. � The algorithm begins at X=0 and Y=R� In the first quadrant of the circle, Y is monotonically decreasing

function of X. � Referring fig. 10.7, (Xp, Yp) is a point on the circle. For clockwise

generation of the circle there are only three possible selections of the next pixel, which represents the circle.

Page 31: 3. Scan Conversion of Geometric Primitives

7 February 2011P. M. Agrawal and V. J. Patel31

Page 32: 3. Scan Conversion of Geometric Primitives

� The algorithm is designed to choose the pixel which minimizes the square of the distance between one of these pixels and the true circle, i.e., the minimum of

H = [(Xp + 1)2 + (Yp)2 – R2]D = [(Xp + 1)2 + (Yp - 1)2 – R2]

7 February 2011P. M. Agrawal and V. J. Patel32

V = [(Xp )2 + (Yp - 1)2 – R2]

� A method for direct distance comparison is to test the halfway position between two pixels to determine if this midpoint is inside or outside the circle boundary.

Page 33: 3. Scan Conversion of Geometric Primitives

Midpoint Circle Algorithm� Along the circle section from X = 0, Y=R to X = Y = R/√2 in the

second octant, the slope of the curve varies from 0 to -1. Therefore, we can take unit steps in the positive X direction over this octant and use a decision parameter to determine which of the two possible Y positions is closer to the circle path at each step.To apply the midpoint method, we define a circle function

7 February 2011P. M. Agrawal and V. J. Patel33

� To apply the midpoint method, we define a circle functionf(x, y) = x2 + y2 – r2

� Any point (x,y) on the boundary of the circle with radius r satisfies the equation f(x, y) = 0

� If the point is in the interior of the circle, the function is negative and if the point is outside the circle, the function is positive.

Page 34: 3. Scan Conversion of Geometric Primitives

� To summarize, the relative position of any point (x, y) can be determined by checking the sign of the function

� Fig 10.8 shows the midpoint between the two candidate pixels at sampling position Xp + 1. Assuming we have just plotted the pixel (Xp, Yp), we next need to determine whether the pixel at position (Xp +1, Yp) or the one at position (Xp + 1, Yp - 1) is closer to the circle

7 February 2011P. M. Agrawal and V. J. Patel34

� Our decision parameter is the circle function at the midpoint between these two pixels:

dp = f(Xp + 1, Yp – 1/2) = (Xp + 1)2 + (Yp – ½)2 – R2

� If dp < 0, this midpoint is inside the circle and pixel on scan line Yp is closer to the circle boundary. Otherwise, the midpoint is outside or on the circle boundary, and we select the pixel on scan line Yp – 1.

Page 35: 3. Scan Conversion of Geometric Primitives

7 February 2011P. M. Agrawal and V. J. Patel35

Page 36: 3. Scan Conversion of Geometric Primitives

� If dp < 0, M is inside the circle, E is closer to the circle than SE, so E is chosen and the next mid point MNE is at (Xp +2, Yp – ½), which has one increment over in X with respect to M. The increment in dp when E is chosen as the current pixel be denoted by . It is given by

7 February 2011P. M. Agrawal and V. J. Patel36

be denoted by . It is given by∆E = dp+1 – dp

= f(Xp + 2, Yp – ½) - f(Xp + 1, Yp – ½)= (Xp + 2)2 + (Yp – ½)2 – R2 - (Xp + 1)2 - (Yp – ½)2 + R2

= 2Xp + 3 So, we have the updation equation,dp+1 = dp + ∆E = dp + (2Xp + 3)

Page 37: 3. Scan Conversion of Geometric Primitives

� If dp >= 0, M is outside the circle or on the circle, SE is closer to the circle than E, so SE is chosen and the next midpoint ME is at (Xp +2, Yp – 3/2), which has one increment over in X and one increment down in Y, with respect to M. The increment in dp when SE is chosen as the current pixel be denoted by . It is given by

7 February 2011P. M. Agrawal and V. J. Patel37

current pixel be denoted by . It is given by∆SE = dp+1 – dp

= f(Xp + 2, Yp – 3/2) - f(Xp + 1, Yp – ½)= (Xp + 2)2 + (Yp – 3/2)2 – R2 - (Xp + 1)2 - (Yp – ½)2 + R2

= 2(Xp -Yp) + 5 So, we have the updation equation,dp+1 = dp + ∆SE = dp + 2(Xp -Yp) + 5

Page 38: 3. Scan Conversion of Geometric Primitives

� To start the process, we need the initial decision parameter d0.� Since the starting pixel lies on the circle at (0, R), the second pixel is at

(1, R - ½) .� Therefore, d0 is calculated from

d0 = f(1, R – ½) = 1 + (R – ½)2 – R2 = 5/4 -R

� Equation contains a fraction term, which forces us to do real arithmetic. In order to eliminate this fraction, we define a new decision parameter

7 February 2011P. M. Agrawal and V. J. Patel38

In order to eliminate this fraction, we define a new decision parameter h0 = d0 – ¼ .

� This will transform the equation to h0 = 1– R. Now the test on d0 have to be performed on h0. The test d0 < 0 becomes h0 < – ¼ . Since h0starts with an integer value for integer radius and is incremented by the values of ∆E and ∆SE we can change the test just h0 < 0. We now have an integer algorithm in terms of h. However, to maintain consistency in notation with regard to mid point line algorithm, we replace h by d though out the algorithm.

Page 39: 3. Scan Conversion of Geometric Primitives

Algorithm1. Input the radius R. 2. Calculate the initial value of decision parameter as

h0 = 1– R

3. Test for dp < 0 starting at Xp = 0 and Yp = R. If yes, the next pixel is E (Xp+1 , Yp) . UpdateX = X +1

7 February 2011P. M. Agrawal and V. J. Patel39

Xp+1 = Xp +1dp+1 = dp + ∆E = dp + (2Xp + 3)

4. If no, the next pixel is SE (Xp+1 , Yp-1). UpdateXp+1 = Xp + 1Yp+1 =Yp - 1dp+1 = dp + ∆SE = dp + 2(Xp -Yp) + 5

5. Repeat steps 3 to 4 until Xp >= Yp