Professional Documents
Culture Documents
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
1 2
x; y
=
a ; x
12
+b
12
;
+c
12
;
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
1 2 1 3 2 3
px ; py
=0 =0 =0
px ; py
px ; py
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
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