Sunteți pe pagina 1din 15

Loading frame-buffer values

• After generating a line or circle using scan conversion, we need to know where is the
specified pixels are stored in the frame buffer.

• Frame buffer is stored in memory as an addressable array and stored row by row.

• Pixels are labeled from (0, 0) the lower left corner to (xmax, ymax) the top right corner.

• For a bilevel system (1 bit per pixel),the Frame-Buffer bit address for pixel position (x, y)
is calculated as:

1. addr(x, y) = addr (0,0) + y (xmax +1) + x

2. addr(x + 1, y) = addr(x, y) + 1

3. addr(x + 1, y + 1) = addr(x, y) + xmax + 2

Example:

Find the address of the pixel (6,5), where the address of (0,0) =500, xmax=12, and ymax = 10.

addr(x, y) = addr (0,0) + y(xmax +1) + x

addr(6, 5) = 500 + 5(12 + 1) + 6 = 571


addr(7, 5) = addr(6, 5) + 1 = 572

addr(7, 6) = addr(6, 5) + xmax + 2 = 585

Pixel Addressing and Object Geometry

• When an object is scan converted into the frame buffer, the input description is
transformed to pixel coordinates.

• So, the displayed image may not correspond exactly with the relative dimensions of the
input object.

• To preserve the specified geometry of world objects, we need to compensate for the
mapping of mathematical input points to finite pixel area, we use one of the two ways:

 Adjust the dimensions of displayed objects to account for the amount of overlap
of pixel areas with the object boundaries. (i.e. a rectangle with 40 cm width, will
be displayed in 40 pixel)
 Map world coordinates onto screen positions between pixels, so that we align
objects boundaries with pixel boundaries instead of pixel centers.

Screen Grid Coordinates:

 An alternative to addressing display positions in terms of pixel centers is to


reference screen coordinates with respect to the grid of horizontal and vertical
pixel boundary lines spaced one unit a part.
 Screen coordinate position is then the pair of integer values identifying a grid
intersection position between two pixels.

 For example, the mathematical line path for a polyline with screen endpoints (0,
0), (5, 2), and (1,4) is shown beside.

 With the coordinate origin at the lower left of the screen, each pixel area can be
referenced by the integer grid coordinates of its lower left corner.

 The following figure illustrates this convention for an 8 by 8 section of a raster, with a
single illuminated pixel at screen coordinate position (4, 5).

 In general, we identify the area occupied by a pixel with screen coordinates ( x, y) as the
unit square with diagonally opposite corners at (x, y) and ( x + 1, y + 1 ).

 This pixel addressing scheme has several advantages:

1. It avoids half-integer pixel boundary

2. It facilitates precise object representations.

3. Simplifies the processing involved in many scan conversion algorithms


and in other raster procedures
Notes:

 The previous algorithms for drawing line, circle, …etc are still valid when applied to
input positions expressed as screen grid coordinates.
 The decision parameter Pk is a measure of screen grid separation differences rather than
separation differences from pixel centers.

Filled – Area Primitives

A polyline is a chain of connected line segments. It is specified by giving the vertices (nodes) P 0,
P1, P2, ... and so on.
The first vertex is called the initial or starting point and the last vertex is called the final or
terminal point, as shown in the Fig. 4.1(a).
When starting point and terminal point of any polyline is same, i,e' when polyline is closed then
it is called polygon. This is illustrated in Fig' 4'1 (b)'

Types of Polygons
Convex and
Concave
A convex polygon is a polygon in which the line segment joining any two points within the
polygon lies completely inside the polygon. The Fig' 4'2 shows the examples of convex Polygons
A concave polygon is a polygon in which the line segment joining any two points within the
polygon may not lie completely inside the polygon. The Fig. 4'3 shows the examples of concave
Polygons.

 A standard output primitive in general graphics packages is a solid-color or patterned


polygon area.

 There are two basic approaches to area filling on raster systems:

1. The scan-line approach

Determine the overlap intervals for scan lines that cross the area. It is typically used in

general graphics packages to fill polygons, circles, ellipses

2. Filling approaches

It start from a given interior position and paint outward from this point until we encounter
the specified boundary conditions. It useful with more complex boundaries and in
interactive painting systems.

Scan-Line Fill Algorithm:


 For each scan line crossing a polygon, the area-fill algorithm locates the intersection
points of the scan line with the polygon edges.

 These intersection points are then sorted from left to right, and the corresponding frame-
buffer positions between each intersection pair are set to the specified fill color.

 In Fig.below, the four pixel intersections positions with the polygon boundaries define
two stretches of interior pixels from x=10 to x=14 and from x=18 to x=24.

 The scan line algorithm first finds the largest and smallest y values of the polygon. It then
starts with the largest y value and works its way down, scanning from left to right, in the
manner of a raster display.
 The important task in the scan line algorithm is to find the intersection points of the scan
line with the polygon boundary. When intersection points are even, they are sorted from
left to right, paired and pixels between paired points are set to the fill colour.
 But in some cases intersection point is a vertex. When scan line intersects polygon vertex
a special handling is required to find the exact intersection points.
 To handle such cases, we must look at the other endpoints of the two line segments of the
polygon which meet at this vertex. If these points lie on the same (up or down) side of the
scan line, then the point in question counts as an even number of intersections. If they lie
on opposite sides of the scan line, then the point is counted as single intersection. This is
illustrated in Fig. 4.12.
As shown in the Fig. 4.1,2, each scan line intersects the vertex or vertices of the polygon.
For scan line 1, the other end points (B and D) of the two line segments of the polygon lie on the
same side of the scan line, hence there are two intersections resulting two pairs : 1 -2 and 3 - 4.
Intersections points 2 and 3 are actually same Points. For scan line 2 the other endpoints (D and
F) of the two line segments of the Polygon lie on the opposite sides of the scan line, hence there
is a single intersection resulting two pairs : l - 2 and 3 - 4. For scan line 3, two vertices are the
intersection points.
For vertex F the other end points E and G of the two line segments of the polygon lie on the
same side of the scan line whereas for vertex H, the other endpoints G and I of the two line
segments of the polygon lie on the opposite side of the scan line.
Therefore, at vertex F there are two intersections and at vertex H there is only one intersection.
This results two pairs :1 - 2 and 3 - 4 and points 2 and 3 are actuallv same points.
We have seen that it is necessary to calculate x intersection points for scan line with every
polygon side.
We can simplify these calculations by using coherence properties.
A coherence property of a scene is a property of a scene by which we can relate one part of a
scene with the other parts of a scene.
Here, we can use a slope of an edge as a coherence property, By using this property we can
determine the x intersection value on the lower scan line if the x intersection value, for current
scan line is known.
This is given as
where m is the slope of the edge
As we scan from top to bottom value of y co-ordinates between the two scan line changes by 1.

Many times it is not necessary to compute the x intersections for scan line with every polygon
side. We need to consider only the polygon sides with endpoints straddling the current scan line.
See Fig. 4.13.

It will be easier to identify which polygon sides should be tested for x-intersection, if we first
sort the sides in order of their maximum y value.
Once the sides are sorted we can process the scan lines from the top of the polygon to its bottom
producing an active edge list for each scan line crossing the polygon boundaries.
The active edge list for a scan line contains all edges crossed by that scan line.
The Fig. 4.14 shows sorted edges of the polygon with active edges.
An Inside - Outside Test

 Area-filling algorithms and other graphics processes often need to identify interior
regions of objects.

 To identify interior regions of an object graphics packages normally use either:

1. Odd-Even rule

2. Nonzero winding number rule

Odd-Even rule (Odd Parity Rule, Even-Odd Rule):

1. draw a line from any position P to a distant point outside the coordinate extents of the
object and counting the number of edge crossings along the line.

2. If the number of polygon edges crossed by this line is odd then

P is an interior point.

Else

P is an exterior point

Nonzero Winding Number Rule :

 Counts the number of times the polygon edges wind around a particular point in the
counterclockwise direction. This count is called the winding number, and the interior
points of a two-dimensional object are defined to be those that have a nonzero value for
the winding number.

1. Initializing the winding number to 0.

2. Imagine a line drawn from any position P to a distant point beyond the coordinate extents
of the object.

3. Count the number of edges that cross the line in each direction. We add 1 to the winding
number every time we intersect a polygon edge that crosses the line from right to left, and
we subtract 1 every time we intersect an edge that crosses from left to right.
4. If the winding number is nonzero, then

P is defined to be an interior point

Else

P is taken to be an exterior point.

Boundary-Fill Algorithm

 Start at a point inside a region and paint the interior outward toward the boundary. If the
boundary is specified in a single color, the fill algorithm proceeds outward pixel by pixel
until the boundary color is encountered.

 It is useful in interactive painting packages, where interior points are easily selected.

 The inputs of the this algorithm are:

• Coordinates of the interior point (x, y)

• Fill Color

• Boundary Color

 Starting from (x, y), the algorithm tests neighboring pixels to determine whether they are
of the boundary color. If not, they are painted with the fill color, and their neighbors are
tested. This process continues until all pixels up to the boundary have been tested.
 There are two methods for proceeding to neighboring pixels from the current test
position:

1. The 4-connected method.

2. The 8-connected method.

void boundaryFill4 (int x, int y, int fillColor, int borderColor)

{ int interiorColor;

/* set current color to fillColor, then perform following operations. */

getPixel (x, y, interiorColor);


if ( (interiorColor != borderColor) && (interiorColor != fillColor) )

setPixel (x, y); // set color of pixel to fillColor

boundaryFill4 (x + 1, y, fillColor, borderColor);

boundaryFill4 (x - 1, y, fillColor, borderColor);

boundaryFill4 (x, y + 1, fillColor, borderColor);

boundaryFill4 (x, y - 1, fillColor, borderColor);

 4-connected and 8-connected methods involve heavy recursion which may consume
memory and time. More efficient methods are used. These methods fill horizontal pixel
spans across scan line. This called a Pixel Span method.

 We need only stack a beginning position for each horizontal pixel span, instead of
stacking all unprocessed neighboring positions around the current position, where spans
are defined as the contiguous horizontal string of positions.
Flood-Fill Algorithm

 Sometimes we want to fill in (or recolor) an area that is not defined within a single color
boundary. We can paint such areas by replacing a specified interior color instead of
searching for a boundary color value. This approach is called a flood-fill algorithm.

 We start from a specified interior point (x, y) and reassign all pixel values that are
currently set to a given interior color with the desired fill color.

 If the area we want to paint has more than one interior color, we can first reassign pixel
values so that all interior points have the same color. Using either a 4-connected or 8-
connected approach, we then step through pixel positions until all interior points have
been repainted.
void floodFill4 (int x, int y, int fillColor, int interiorColor)

{ int color;

/* set current color to fillColor, then perform following operations. */

getPixel (x, y, color);

if (color = interiorColor)

setPixel (x, y); // set color of pixel to fillColor

floodFill4 (x + 1, y, fillColor, interiorColor);

floodFill4 (x - 1, y, fillColor, interiorColor);

floodFill4 (x, y + 1, fillColor, interiorColor);

floodFill4 (x, y - 1, fillColor, interiorColor);

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