0% found this document useful (0 votes)
33 views26 pages

Scan Conversion 2

This document discusses algorithms for scan converting circles to raster images. It begins by describing initial naive algorithms and identifies issues with them. It then presents an incremental, symmetric algorithm that only considers one eighth of the circle. This algorithm uses incremental computation to efficiently update decision variables at each step to determine the next pixel. It computes "delta" terms to update decision variables as it moves along the circle, allowing it to render the entire circle through symmetry. Pseudocode is provided for this "midpoint eighth circle algorithm".

Uploaded by

kamar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

Scan Conversion 2

This document discusses algorithms for scan converting circles to raster images. It begins by describing initial naive algorithms and identifies issues with them. It then presents an incremental, symmetric algorithm that only considers one eighth of the circle. This algorithm uses incremental computation to efficiently update decision variables at each step to determine the next pixel. It computes "delta" terms to update decision variables as it moves along the circle, allowing it to render the entire circle through symmetry. Pseudocode is provided for this "midpoint eighth circle algorithm".

Uploaded by

kamar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 23, 2008 ScanConversion2 #/26


Scan Conversion 2
(pages 81-91 in the textbook)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26

Version 1: really bad
For x = R to R
y = sqrt(R * R x * x);
Pixel (round(x), round(y));
Pixel (round(x), round(-y));


Version 2: slightly less bad
For x = 0 to 360
Pixel (round (R cos(x)), round(R sin(x)));
(17, 0)
(0, 17)
(17, 0)
(0, 17)
Scan Converting Circles
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Symmetry: If (x
0
+ a, y
0
+ b) is on circle
also (x
0
a, y
0
b) and (x
0
b, y
0
a);
hence 8-way symmetry.

Reduce the problem to finding the pixels
for 1/8 of the circle



R
(x
0
+ a, y
0
+ b)
(x-x
0
)
2
+ (y-y
0
)
2
= R
2
(x
0
, y
0
)
Version 3 Use Symmetry
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Scan top right 1/8 of circle of radius R









Circle starts at (x
0
, y
0
+ R)
Lets use another incremental algorithm
with decision variable evaluated at
midpoint
(x
0
, y
0
)
Using the Symmetry
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
E
SE
x = x
0
; y = y
0
+ R; Pixel(x, y);
for (x = x
0
+1; (x x
0
) > (y y
0
); x++) {
if (decision_var < 0) {
/* move east */
update decision_var;
}
else {
/* move south east */
update decision_var;
y--;
}
Pixel(x, y);
}

Note: can replace all occurrences of x
0
, y
0
with 0, 0
and Pixel (x
0
+ x, y
0
+ y) with Pixel (x, y)
Shift coordinates by (-x
0
,-y
o
)
Sketch of Incremental Algorithm
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Decision variable
negative if we move E, positive if we
move SE (or vice versa).

Follow line strategy: Use implicit equation
of circle
f(x,y) = x
2
+ y
2
R
2
= 0

f(x,y) is zero on circle, negative inside,
positive outside

If we are at pixel (x, y)
examine (x + 1, y) and (x + 1, y 1)

Compute f at the midpoint
What we need for Incremental
Algorithm
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
P = (x
p
, y
p
)
M M
E
M
SE
SE
Decision Variable
E
Evaluate f(x,y) = x
2
+ y
2
R
2

at the point


We are asking: Is



positive or negative? (it is zero on circle)

If negative, midpoint inside circle, choose E
vertical distance to the circle is less at
(x + 1, y) than at (x + 1, y1).

If positive, opposite is true, choose SE
2
2
2
2
1
) 1 (
2
1
, 1 R y x y x f

|
.
|

\
|
+ + =
|
.
|

\
|
+
|
.
|

\
|
+
2
1
, 1 y x
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Decision based on vertical distance
Ok for lines, since d and d
vert
are
proportional
For circles, not true:








Which d is closer to zero? (i.e. which of the
two values below is closer to R):

R y x Circ y x d
R y x Circ y x d
+ + = +
+ + = +
2 2
2 2
) 1 ( ) 1 ( ) ), 1 , 1 ((
) 1 ( ) ), , 1 ((
2 2 2 2
) 1 ( ) 1 ( ) 1 (
+ + + +
y x or y x
The right decision variable?
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
We could ask instead:
Is (x + 1)
2
+ y
2
or (x + 1)
2
+ (y 1)
2

closer to R
2
?

The two values in equation above differ by
1 2 ] ) 1 ( ) 1 [( ] ) 1 [(
2 2 2 2
= + + + +
y y x y x
f
E
f
SE
= 290 257 = 33
(0, 17)
(1, 17)
(1, 16)
f
E
= 1
2
+ 17
2
= 290
E
SE
f
SE
= 1
2
+ 16
2
= 257
2y 1 = 2(17) 1 = 33
Alternate Phrasing (1/3)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
The second value, which is always less,
is closer if its difference from R
2
is less than


i.e., if


then


so

so

so
<
2
) 1 2 (
1

y ] ) 1 ( ) 1 [(
2 2 2
+ +
y x R
) 1 2 (
2
1

|
.
|

\
|
y
2
1
) 1 ( 0
2 2 2
R y y x
+ + + <
2
1
1 2 ) 1 ( 0
2 2 2
R y y y x
+ + + + <
) 1 ( ) 1 (
2
1
0
2 2 2
R y x y
+ + + <
2
2
2
4
1
2
1
) 1 ( 0 R y x
+
|
.
|

\
|
+ + <
Alternate Phrasing (2/3)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
The radial distance decision is whether



is positive or negative

And the vertical distance decision is whether



is positive or negative; d1 and d2 are
apart.


The integer d1 is positive only if d2 + is
positive (except special case where d2 = 0).




4
1
4
1
2
2
2
4
1
2
1
) 1 ( 1 R y x d
+
|
.
|

\
|
+ + =
2
2
2
2
1
) 1 ( 2 R y x d

|
.
|

\
|
+ + =
Alternate Phrasing (3/3)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
How to compute the value of




at successive points?

Answer: Note that


is just


and that


is just

(1/2)
2
2
2
2
1
) 1 ( ) , ( R y x y x f

|
.
|

\
|
+ + =
2 2 3 2 ) , (
) , ( ) 1 , 1 (
3 2 ) , (
) , ( ) , 1 (
SE
E
+ + = A
+
+ = A
+
y x y x
y x f y x f
x y x
y x f y x f
Incremental Computation, Again
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
F
F
F
Incremental Computation (2/2)
If we move E, update by adding 2x + 3

If we move SE, update by adding
2x + 3 2y + 2.

Forward differences of a 1
st
degree polynomial
are constants and those of a 2
nd
degree
polynomial are 1
st
degree polynomials
this first order forward difference, like a
partial derivative, is one degree lower
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
The function is linear, hence
amenable to incremental computation, viz:







Similarly
Second Differences (1/2)
3 2 ) , (
E
+ = A
x y x
2 ) , ( ) 1 , 1 (
E E
= A + A
y x y x
2 ) , ( ) , 1 (
E E
= A + A
y x y x
4 ) , ( ) 1 , 1 (
SE SE
= A + A
y x y x
2 ) , ( ) , 1 (
SE SE
= A + A
y x y x
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
For any step, can compute new
E
(x, y) from old

E
(x, y) by adding appropriate second constant
increment update delta terms as we move.
This is also true of
SE
(x, y)

Having drawn pixel (a,b), decide location of new
pixel at (a + 1, b) or (a + 1, b 1), using
previously computed d(a, b).

Having drawn new pixel, must update d(a, b) for
next iteration; need to find either d(a + 1, b) or
d(a + 1, b 1) depending on pixel choice

Must add
E
(a, b)

or
SE
(a, b) to d(a, b)

So we
Look at d(i) to decide which to draw next,
update x and y
Update d using
E
(a,b) or
SE
(a,b)
Update each of
E
(a,b) and
SE
(a,b) for future
use
Draw pixel
Second Differences (2/2)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26


Midpoint Eighth Circle Algorithm
MEC (R) /* 1/8
th
of a circle w/ radius R */
{
int x = 0, y = R;
int delta_E, delta_SE;
float decision;
delta_E = 2*x + 3;
delta_SE = 2(x-y) + 5;
decision = (x+1)*(x+1) + (y + 0.5)*(y + 0.5) R*R;
Pixel(x, y);
while( y > x ) {
if (decision > 0) {/* Move east */
decision += delta_E;
delta_E += 2; delta_SE += 2; /*Update delta*/
}
else {/* Move SE */
y--;
decision += delta_SE;
delta_E += 2; delta_SE += 4; /*Update delta*/
}
x++;
Pixel(x, y);
}
}
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Uses floats!
1 test, 3 or 4 additions per pixel
Initialization can be improved
Multiply everything by 4 No Floats!
Makes the components even, but sign of
decision variable remains same






Questions
Are we getting all pixels whose distance
from the circle is less than ?
Why is y > x the right stopping criterion?
What if it were an ellipse?

Analysis
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Patterned primitives



Aligned Ellipses



Non-integer
primitives



General conics
Other Scan Conversion Problems
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Patterned line from P to Q is not same
as patterned line from Q to P.





Patterns can be geometric or cosmetic
Cosmetic: Texture applied after
transformations
Geometric: Pattern subject to
transformations

Cosmetic patterned line


Geometric patterned line
Patterned Lines
P Q
P Q
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Geometric Pattern vs. Cosmetic Pattern
+
Geometric
(Perspectivized/Filtered)
Cosmetic
(Contact Paper)
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Equation is





i.e,




Computation of and is similar
Only 4-fold symmetry
When do we stop stepping horizontally and
switch to vertical?


Aligned Ellipses
1
2
2
2
2
= +
b
y
a
x
2 2 2 2 2 2
b a y a x b
= +
E
A
SE
A
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
When absolute value of slope of ellipse is
more than 1, viz:





How do you check this? At a point (x,y) for
which f(x,y) = 0, a vector perpendicular to
the level set is f(x,y) which is




This vector points more right than up when
Direction Changing Criterion (1/2)
(

(



c
c
c
c
) , ( ), , ( y x
y
f
y x
x
f
0 ) , ( ) , (
>
c
c

c
c
y x
y
f
y x
x
f
V
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
In our case,


and



so we check for



i.e.



This, too, can be computed incrementally
Direction Changing Criterion (2/2)
x a y x
x
f
2
2 ) , (
=
c
c
y b y x
y
f
2
2 ) , (
=
c
c
0 2 2
2 2
>
y b x a
0
2 2
>
y b x a
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26

Now in ENE octant, not ESE octant


This problem is artifact of aliasing much
more on this later
Problems with Aligned Ellipses
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Non-Integer Primitives
Initialization is harder
Endpoints are hard, too
making Line (P,Q) and Line (Q,R)
join properly is a good test
Symmetry is lost
Non Integer Primitives and
General Conics
General Conics
Very hard--the octant-changing test is
tougher, the difference computations
are tougher, etc.
do it only if you have to.

Note that drawing gray-scale conics is
easier than drawing B/W conics
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S
Andries van Dam September 23, 2008 ScanConversion2 #/26
Generic Polygons
(More information and these pictures on page
92-93 of textbook)

You might also like