You are on page 1of 13

Scan Conversion

c b

Simple Algorithms
We start from a triangle  1 1  2 2, and  3 3 Find all pixels inside Method 1 the worst algorithm For each pixel do If 2 then draw-pixel p end if End for Method 2 a slight improvement
T x ;y ; x ;y x ;y T p p T B

For each pixel 2 do If 2 then draw-pixel p end if End for The given previous algorithms suggest an important sub-problem: Given a triangle , and =   How to determine: 2
p B p T T p px ; py p T

bounding

box T

 

Ray Firing
Here's a simple approach to test if 2 1 draw a ray from outward in any direction 2 count number of intersections of this ray with boundaries of 3 If odd, then 2 , otherwise, is not in Is this method correct? What happens if the ray crosses at a vertex?
p T p T p T p T

Polygon Scan Conversion

Implicit Line Formula


A slightly easier method Consider the edge 1 2 Write down the implicit function of this line
v v l ;

1 2

x; y

=

a ; x

12

+b

12
;

+c

12
;

Pick the sign of 1 2 so that 1 2 3 3 This de nes a half-plan 1 2


l ; l ; x ;y h ; h ;

1 2 = f

x; y

:

l ;

1 2

x; y

=0

Apply the similar process shown above to 1 3 and 2 3 Construct half-planes 1 3 and 2 3 The important observation
l ; l ; h ; h ;

h ;

12

h ;

13
h ;

h ;

23

Therefore, 2 is equivalent to  2 1 2 and  2 1 3 and  2 2 3 It is the same to say


p T p h ; p h ; p l ; l ; l ;

1 2 1 3 2 3

px ; py

  

=0 =0 =0

px ; py

px ; py

Question: does this algorithm work for concave polygon ?

Sweep-line Algorithm
p T

Observation If 2 , then neighboring pixels are probably in the triangle, too Coherence Idea 1 sweep from top to bottom 2 maintain intersections of and sweep-line span" 3 paint pixels in the span Algorithm Initialize and For each scan line covered by do Paint pixels     on the current span Incrementally update and End for Question: how do we update and ? Answer: midpoint algorithm !
T xl xr T xl ; y ; : : : ; : : : ; xr ; y xl xr xl xr

Polygon Scan Conversion


Given a simple polygon with vertices  1 1  2 2   Find all pixels inside Polygon classi cation simple convex simple concave non-simple self-intersection Once again, we could compute a bounding box and use ray casting = ,   For each pixel 2 do If 2 then paint   end if End for But this would NOT take advantage of coherence Coherence Adjacent pixels in image space are likely sharing the similar graphic properties such as color
P x ;y ; x ;y ;:::; xn ; yn P B bounding box P p B p P p

Polygon Scan Conversion

Polygon Classi cation

Scan Conversion
More e cient algorithm For each scanline Identify all intersections 0 1 ,1 Sort intersections from left to right Fill pixels between consecutive pairs of intersection
x ; x ; : : : ; xk

x

i; y

; x

2 +1
i

;y

We must deal with special cases" ! horizontal lines intersecting a vertex double intersection unwanted intersection We must speed up the edge intersection detection Data structure for e cient implementation A sorted edge table The active edge list From bottom to the top

Figure 3.39 Practical polygon scan conversion Many implementations just triangulate the polygon and then convert the triangles Extremely easy to do for convex polygons Triangles are often particularly nice to work with because they are always planar and simple

Special Cases