Filling Rectangles

advertisement
Filling Rectangles
for (y=YMIN; y<YMAX; y++) // by scan line
{
for (x=XMIN; x<XMAX; x++) // by pixel in span
{
WritePixel(x,y,color);
}
}
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
1/19
Filling Polygons
• Simple version
– For each scan line
• Compute intersection points with each edge
• Generate spans
• Display spans
– Example
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
2/19
Filling Examples
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
3/19
Span Filling
1. Find the intersections of the scan line with al edges of the
polygon
2. Sort the intersections by increasing x coordinate
3. Fill in all pixels between pairs of intersections that lie
interior to the polygon, using the odd-parity rule (parity is
initially even, and each intersection encountered thus
inverts the parity bit---draw when parity is odd, do not
draw if even)
Note: Step 1 and 2 will be discussed later
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
4/19
Span-filling Strategy
• Question with Step 3:
3.1 Intersection may be at fractional x value, how to
determine which pixel one either side is interior?
3.2 How to deal with intersections at integer coordinates?
3.3 How to deal with shared vertices?
3.4 How to deal with Horizontal edges?
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
5/19
Answers
3.1 If we are approaching the intersection from the left side
and are inside the polygon, we round down the x
coordinate; If we are outside, we round up to be inside.
3.2 Use these rules:
– leftmost is defined interior
– rightmost is defined exterior
3.3 Count only ymin vertex of an edge for parity calculation
but not ymax
3.4 Do not count vertices of horizontal edges
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
6/19
Edge Coherence and Scan-Line Algorithm
• Many edges intersected by scan line i are also intersected
by scan line i+1 (edge coherence)
xi+1 = xi + 1/m
where m = (ymax-ymin)/(xmax-xmin)
• For polygon filling, we will step in y direction, thus 1/m
will be used to update x
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
7/19
Edge Table and Active Edge Table
• ET: A (bucket) sorted table that stores list of all edges
• AET: List of edges that intersect with current scan line
(sorted by x)
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
8/19
Scan-line Algorithm
1. Set y to smallest coordinate
2. Initialize the AET to be empty
3. Repeat until the AET and ET are empty:
3.1 Move from ET bucket y to the AET those edges whose ymin
= y, then sort the AET on x
3.2 Fill in desired pixel on scan line y by using pairs of x
coordinates from the AET
3.3 Remove those entries for which y=ymax from the AET
3.4 Increment y by 1
3.5 For each non-vertical edge remaining in the AET, update x
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
9/19
Edge Table
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
10/19
Active Edge Table
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
11/19
Pattern Filling
(1/2)
• To fill an area with a pattern
• Transparent mode:
– perform WritePixel() with foreground color if pattern = 1
– inhibit WritePixel() if pattern = 0
• Opaque mode:
– perform WritePixel() with foreground color if pattern = 1
– perform WritePixel() with background color if pattern = 0
• Where the pattern is “anchored” ?
– Left most polygon vertex (doesn’t work with circles)
– Screen origin (fast, seamless connection
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
12/19
Pattern Filling
(2/2)
• Patterns are defined as small M by N bitmaps
• Assume that the Pattern [0, 0] pixel is coincident with the
screen origin, we can write a pattern in transparent mode
with the following code:
if pattern[x mod M, y mod n] then WritePixel(x, y, color);
• Note: In opaque mode, a whole row can be copied at
once!
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
13/19
Pattern Filling Without Repeated Scan Conversion
• Scan convert a primitive first into a rectangular work area,
then write each pixel to the appropriate place in the
canvas
• Twice as much work but good for primitives that are
scan-converted repeatedly (e.g. characters)
• Don’t have to worry about clipping
• Use copyPixel() (or BitBlt()) for faster speed (drawing the
entire rectangle encompassing the primitive at once)
• Problem with objects with “holes” when writing in opaque
mode
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
14/19
Example
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
15/19
Thick Primitives
(1/3)
• Replicating pixels
– write multiple pixels at each selected pixel in scan
conversion process
– thickness inconsistent, gaps may occur when connecting 2
lines
• The moving pen
– using a rectangular pen whose center moves along the
single-pixel outline of the primitive
– line seems thicker at endpoints
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
16/19
Thick Primitives
(2/3)
• Filling areas between boundaries
– A thick line is drawn as a rectangle with thickness t
– A thick circle is draw as 2 circles of radius R-t/2 and R+t/2
• Approximation by thick polyline
– decompose each primitive into rectangular pieces
– draw each piece
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
17/19
Thick Primitives
17-Jul-01
(3/3)
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
18/19
Line Style and Pen Style
• Sample code for a write mask of 16 booleans:
if bitstring[i mod 16] then WritePixel(x, y, color);
– index i is a new variable incremented in the inner loop
• Thick lines are created as sequences of altering solid and
transparent rectangles.
• Line style is used to calculate the rectangle for each dash
• Pen style is used to fill each rectangle
17-Jul-01
240-422 Computer Graphics : Lecture 5 Filling ellipses and Thick Primitives
19/19
Download