Sunteți pe pagina 1din 39

Shri P. M.

Agrawal
Scan Conversion of Geometric
Primitives
Shri P. M. Agrawal
Shri V. J. Patel
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 2011
2
P. M. Agrawal and V. J. Patel
7 February 2011
3
P. M. Agrawal and V. J. Patel
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 2011
4
P. M. Agrawal and V. J. Patel
7 February 2011
5
P. M. Agrawal and V. J. Patel
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 (x
0
, y
0
) and (x
1
, y
1
) 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 y
i
and x
i
be the coordinates of i
th
pixel on the line.
7 February 2011
6
P. M. Agrawal and V. J. Patel
7 February 2011
7
P. M. Agrawal and V. J. Patel
The coordinates of (i+1) pixel satisfy the equation
Assuming that x-coordinate is sampled at unit intervals,
7 February 2011
8
P. M. Agrawal and V. J. Patel
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 2011
9
P. M. Agrawal and V. J. Patel
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 2011
10
P. M. Agrawal and V. J. Patel
Algorithm
1. Compute m from the end points (x
0
, y
0
) and (x
1
, y
1
) of the
line
2. If |m|<= 1, continue for x
0
to x
1
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 (x
I+1
, round(y
i+1)
)), Go to step 5
7 February 2011
11
P. M. Agrawal and V. J. Patel
4. if the right end point is the start point, then use the equations,
Set pixel (x
I+1
, round(y
i+1)
)), Go to step 5
5. If x
i
> x
1 ,
then go to 10.
6. If |m| > 1 continue for y
0
to y
1
7. 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(x
I+1
), y
i+1)
), Go to step 9
7 February 2011
12
P. M. Agrawal and V. J. Patel
8. if the right end point is the start point, then use the
equations,
Set pixel (round(x
I+1
), y
i+1)
), Go to step 9
9. If y
i
> y
1 ,
then go to 10.
10. End.
7 February 2011
13
P. M. Agrawal and V. J. Patel
Bresenhams Algorithm for lines
Bresenhanms 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 2011
14
P. M. Agrawal and V. J. Patel
7 February 2011
15
P. M. Agrawal and V. J. Patel
Let P be the pixel with coordinates (x
p
, y
p
) 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(x
p
, y
p+1
)
S(x
p
, y
p-1
) S(x
p
, y
p-1
)
E(x
p+1
, y
p
)
W(x
p-1
, y
p
)
NE(x
p+1
, y
p+1
)
SE(x
p+1
, y
p-1
)
NW(x
p-1
, y
p+1
)
SW(x
p-1
, y
p-1
)
7 February 2011
16
P. M. Agrawal and V. J. Patel
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 x
P
+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 2011
17
P. M. Agrawal and V. J. Patel
7 February 2011
P. M. Agrawal and V. J. Patel
18
The y-coordinate of I at the column (x
P
+1) is
y = m(x
P
+1)+b
The distance of I from E is

1
= y - y
P
= m(x
P
+1)+b - y
P
7 February 2011
P. M. Agrawal and V. J. Patel
19
= m(x
P
+1)+b - y
P
The distance of I from NE is

2
= (y
P
+1) y
= y
P
+1 - m(x
P
+1) - b
The difference between these distances is

1
-
2
= 2m(x
P
+1) - y
P
+ 2b - 1
Substituting for m = (y
1
- y
0
)/(x
1
- x
0
) = y/x in above
equation yields
Let the decision parameter d
p
be defined as
7 February 2011
P. M. Agrawal and V. J. Patel
20
Let the decision parameter d
p
be defined as
d
p
= x (
1
-
2
) = 2 y y
p
- 2x y
p
+ c 10.15
where c = 2 y + x(2b - 1), is independent of pixel
positions.
Since x is positive, sign of d
p
is dictated by the sign of (
1
-

2
). Hence, the decision on E and NE remains the same.
If d
p
is negative, then E(x
P
+1, y
P
) is selected and the new
decision parameter d
p+1
is evaluated from 10.15 as
d
p+1
= 2 yx
P+1
-2 x y
P
+ c 10.16
From eqs. 10.15 and 10.16, we obtain
d - d = 2 y (x x )
7 February 2011
P. M. Agrawal and V. J. Patel
21
d
p+1
- d
P
= 2 y (x
P+1
x
P
)
Since x
P+1
= x
P
+ 1, we can write above equation as
d
p+1
= d
P
+ 2 y
If d
p
is positive, then NE(x
P
+1, y
P
+1) is selected and the new
decision parameter d
p+1
is evaluated from 10.15 as
d
p+1
= 2 yx
P+1
-2 x y
P+1
+ c 10.19
From eqs. 10.15 and 10.19, we obtain
d
p+1
- d
P
= 2 y (x
P+1
x
P
) - 2 x (y
P+1
y
P
)
7 February 2011
P. M. Agrawal and V. J. Patel
22
d
p+1
- d
P
= 2 y (x
P+1
x
P
) - 2 x (y
P+1
y
P
)
Since, x
P+1
= x
P
+ 1 and y
P+1
= y
P
+ 1
d
p+1
= d
P
+ 2 (y - x)
At the starting location (x
0
, y
0
), we have from eqn. 10.15 for
the initial decision parameter
d
0
= 2 y - x
Algorithm
1. Input the two end points (x
0
, y
0
) and (x
1
, y
1
).
2. Calculate the constants x, y, 2x, 2y, 2(y - x) and also
the initial decision parameter d
0
.
3. Plot the left end point (x
0
, y
0
) as the first point.
4. If d
0
< 0, the next point to plot is (x
P
+1, y
P
) with
x = x +1
7 February 2011
P. M. Agrawal and V. J. Patel
23
x
P+1
= x
P
+1
d
p+1
= d
P
+ 2 y
If d
0
> 0, the next point to plot is (x
P
+1, y
P
+1) with
x
P+1
= x
P
+1
y
P+1
= y
P
+1
d
p+1
= d
P
+ 2 (y - x)
5. Repeat step 4 until the right end point is reached.
The Bresenhams 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 2011
P. M. Agrawal and V. J. Patel
24
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.
DDA versus Bresenhams Algorithm
DDA works with floating point arithmetic
Rounding to integers necessary
Bresenhams algorithm uses integer arithmetic
Constants need to be computed only once
7 February 2011
P. M. Agrawal and V. J. Patel
25
Constants need to be computed only once
Bresenhams algorithm generally faster than DDA
Circle Generating algorithms
Direct Algorithm
Polar coordinate based Algorithm
Midpoint Algorithm (Bresenhams Algorithm)
7 February 2011
P. M. Agrawal and V. J. Patel
26
Direct Circle Algorithm
Properties of a circle:
A circle is defined as a set of points that are all the given distance
(x
c
,y
c
). This distance relationship is expressed by the pythagorean
theorem in Cartesian coordinates as
(x x
c
)
2
+ (y y
c
)
2
= r
2
We could use this equation to calculate the points on the circle
7 February 2011
P. M. Agrawal and V. J. Patel
27
We could use this equation to calculate the points on the circle
circumference by stepping along x-axis in unit steps from x
c
-r to
x
c
+r and calculate the corresponding y values at each position as
y = y
c
+(- ) (r
2
(xc x )
2
)
1/2
This is not the best method:
Considerable amount of computation
Spacing between plotted pixels is not uniform
Polar Coordinated Based Circle
Algorithm
We could use polar coordinates r and ,
x = x
c
+ r cos y = y
c
+ 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 2011
P. M. Agrawal and V. J. Patel
28
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.
Eight-way Symmetry
7 February 2011
P. M. Agrawal and V. J. Patel
29
Fig. 10.6
Bresenhams 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 2011
P. M. Agrawal and V. J. Patel
30
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, (X
p
, Y
p
) 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.
7 February 2011
P. M. Agrawal and V. J. Patel
31
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 = [(X
p
+ 1)
2
+ (Y
p
)
2
R
2
]
D = [(X
p
+ 1)
2
+ (Y
p
- 1)
2
R
2
]
7 February 2011
P. M. Agrawal and V. J. Patel
32
V = [(X
p
)
2
+ (Y
p
- 1)
2
R
2
]
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.
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 2011
P. M. Agrawal and V. J. Patel
33
To apply the midpoint method, we define a circle function
f(x, y) = x
2
+ y
2
r
2
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.
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 X
p
+ 1. Assuming we have just plotted the pixel (X
p
,
Y
p
), we next need to determine whether the pixel at position (X
p
+1,
Y
p
) or the one at position (X
p
+ 1, Y
p
- 1) is closer to the circle
7 February 2011
P. M. Agrawal and V. J. Patel
34
Our decision parameter is the circle function at the midpoint between
these two pixels:
d
p
= f(X
p
+ 1, Y
p
1/2)
= (X
p
+ 1)
2
+ (Y
p
)
2
R
2
If d
p
< 0, this midpoint is inside the circle and pixel on scan line Y
p
is
closer to the circle boundary. Otherwise, the midpoint is outside or on
the circle boundary, and we select the pixel on scan line Y
p
1.
7 February 2011
P. M. Agrawal and V. J. Patel
35
If d
p
< 0, M is inside the circle, E is closer to the circle than
SE, so E is chosen and the next mid point M
NE
is at (X
p
+2,
Y
p
), which has one increment over in X with respect to
M. The increment in d
p
when E is chosen as the current pixel
be denoted by . It is given by
7 February 2011
P. M. Agrawal and V. J. Patel
36
be denoted by . It is given by

E
= d
p+1
d
p
= f(X
p
+ 2, Y
p
) - f(X
p
+ 1, Y
p
)
= (X
p
+ 2)
2
+ (Y
p
)
2
R
2
- (X
p
+ 1)
2
- (Y
p
)
2
+ R
2
= 2X
p
+ 3
So, we have the updation equation,
d
p+1
= d
p
+
E
= d
p
+ (2X
p
+ 3)
If d
p
>= 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 M
E
is at (X
p
+2, Y
p
3/2), which has one
increment over in X and one increment down in Y, with
respect to M. The increment in d
p
when SE is chosen as the
current pixel be denoted by . It is given by
7 February 2011
P. M. Agrawal and V. J. Patel
37
current pixel be denoted by . It is given by

SE
= d
p+1
d
p
= f(X
p
+ 2, Y
p
3/2) - f(X
p
+ 1, Y
p
)
= (X
p
+ 2)
2
+ (Y
p
3/2)
2
R
2
- (X
p
+ 1)
2
- (Y
p
)
2
+ R
2
= 2(X
p
-Y
p
) + 5
So, we have the updation equation,
d
p+1
= d
p
+
SE
= d
p
+ 2(X
p
-Y
p
) + 5
To start the process, we need the initial decision parameter d
0
.
Since the starting pixel lies on the circle at (0, R), the second pixel is at
(1, R - ) .
Therefore, d
0
is calculated from
d
0
= f(1, R ) = 1 + (R )
2
R
2
= 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 2011
P. M. Agrawal and V. J. Patel
38
In order to eliminate this fraction, we define a new decision parameter
h
0
= d
0
.
This will transform the equation to h
0
= 1 R. Now the test on d
0
have
to be performed on h
0
. The test d
0
< 0 becomes h
0
< . Since h
0
starts with an integer value for integer radius and is incremented by the
values of
E
and
SE
we can change the test just h
0
< 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.
Algorithm
1. Input the radius R.
2. Calculate the initial value of decision parameter as
h
0
= 1 R
3. Test for d
p
< 0 starting at X
p
= 0 and Y
p
= R. If yes, the next
pixel is E (X
p+1
, Y
p
) . Update
X = X +1
7 February 2011
P. M. Agrawal and V. J. Patel
39
X
p+1
= X
p
+1
d
p+1
= d
p
+
E
= d
p
+ (2X
p
+ 3)
4. If no, the next pixel is SE (X
p+1
, Y
p-1
). Update
X
p+1
= X
p
+ 1
Y
p+1
=Y
p
- 1
d
p+1
= d
p
+
SE
= d
p
+ 2(X
p
-Y
p
) + 5
5. Repeat steps 3 to 4 until X
p
>= Y
p

S-ar putea să vă placă și