chapter 3 output primitives

108
Objectives At the end of this chapter the reader will be able to: • Describe scan conversion • Describe how to scan convert basic graphic primitives like point, line, circle, ellipse Output Primitives

Upload: prathima

Post on 13-Dec-2014

1.647 views

Category:

Technology


8 download

DESCRIPTION

This includes different line drawing algorithms,circle,ellipse generating algorithms, filled area primitives,flood fill ,boundary fill algorithms,raster scan fill approaches.

TRANSCRIPT

Page 1: Chapter 3 Output Primitives

Objectives At the end of this chapter the reader will be able to: • Describe scan conversion • Describe how to scan convert basic graphic primitives like point, line, circle, ellipse

Output Primitives

Page 2: Chapter 3 Output Primitives

StructureIntroduction Scan-converting a Point Scan-converting a Straight Line Scan-converting a Circle Scan-converting an Ellipse

Page 3: Chapter 3 Output Primitives

IntroductionOutput Primitives● Graphic SW and HW provide subroutines to describe a scene in terms of basic geometric structures called output primitives.

● Output primitives are combined to formcomplex structures● Simplest primitives– Point (pixel)– Line segment

Page 4: Chapter 3 Output Primitives

Scan Conversion● Converting output primitives into frame buffer updates. Choose which pixels contain which intensity value.

● Constraints– Straight lines should appear as a straight line– Primitives should start and end accurately– Primitives should have a consistent brightnessalong their length– They should be drawn rapidly

Page 5: Chapter 3 Output Primitives

Points and Lines Point plotting is accomplished by converting a single coordinate position furnished by an application program into appropriate operations for the output device.

Line drawing is accomplished by calculating intermediate positions along the line path between two specified end points positions. An output device is then directed to fill in these positions between the end points

Page 6: Chapter 3 Output Primitives

Points A point is shown by illuminating a pixel on the screen

Page 7: Chapter 3 Output Primitives

LinesA line segment is completely defined in terms of its two endpoints.A line segment is thus defined as:

Line_Seg = { (x1, y1), (x2, y2) }

Page 8: Chapter 3 Output Primitives

LinesA line is produced by means of illuminating a set of intermediary pixels between the two endpoints.

x

y

x1 x2

y1

y2

Page 9: Chapter 3 Output Primitives

Lines

Lines is digitized into a set of discrete integer positions that approximate the actual line path.Example: A computed line position of (10.48, 20.51) is converted to pixel position (10, 21).

Page 10: Chapter 3 Output Primitives

Lines

The rounding of coordinate values to integer causes all but horizontal and vertical lines to be displayed with a stair step appearance “the jaggies”.

Page 11: Chapter 3 Output Primitives

To load an intensity value into the frame buffer at a position corresponding to column x along scan line y,

setpixel (x, y)

To retrieve the current frame buffer intensity setting for a specified location we use a low level function ,

getpixel (x, y)

Page 12: Chapter 3 Output Primitives

Line Drawing Algorithms

Digital Differential Analyzer (DDA) Algorithm

Bresenham’s Line Algorithm

Parallel Line Algorithm

Page 13: Chapter 3 Output Primitives

Slope Intercept MethodThe Cartesian slope-intercept equation for a straight line is y = m . x + b (1) Where m as slope of the line and b as the y intercept Given that the two endpoints of a line segment are specified at positions (x1,y1) and (x2,y2) as in figure we can determine the values for the slope m and y intercept b with the following calculations

Page 14: Chapter 3 Output Primitives

Points and Lines m = ∆y / ∆x = y2-y1 / x2 - x1 (2) b= y1 - m . x1 (3)

For any given x interval ∆x along a line, we can compute the corresponding y interval ∆ y ∆y= m ∆x (4) We can obtain the x interval ∆x corresponding to a specified ∆y as ∆ x = ∆ y/m (5)

Y2

y1

X1 x2

Page 15: Chapter 3 Output Primitives

For lines with slope magnitudes |m| < 1, ∆x can be set proportional to a small horizontal deflection voltage and the corresponding vertical deflection is then set proportional to ∆y as calculated from Eq (4). For lines whose slopes have magnitudes |m | >1 , ∆y can be set proportional to a small vertical deflection voltage with the corresponding horizontal deflection voltage set proportional to ∆x, calculated from Eq (5) For lines with m = 1, ∆x = ∆y and the horizontal and vertical deflections voltage are equal.

Figure : Straight line Segment with five sampling positions along the x axis between x1 and x2

Y2

y1

X1 x2

Sampling along x axis

Page 16: Chapter 3 Output Primitives

LineAll line drawing algorithms make use of the fundamental equations:

Line Eqn. y = m.x + bSlope m = y2 − y1 / x2 − x1 = Δy / Δxy-intercept b = y1 − m.x1

x-interval→Δx = Δy / my-interval→ Δy = m Δx

Page 17: Chapter 3 Output Primitives

DDA Algorithm (Digital Differential Analyzer)

A line algorithm Based on calculating either Δy or Δx using the above equations.There are two cases:

Positive slopNegative slop

Page 18: Chapter 3 Output Primitives

DDA- Line with positive SlopeIf m ≤ 1 then take Δx = 1Compute successive y by

yk+1 = yk + m (1)Subscript k takes integer values starting from 1, for the first point, and increases by 1 until the final end point is reached.Since 0.0 < m ≤ 1.0, the calculated y values must be rounded to the nearest integer pixel position.

Page 19: Chapter 3 Output Primitives

DDAIf m > 1, reverse the role of x and y and take Δy = 1, calculate successive x from

xk+1 = xk + 1/m (2)

In this case, each computed x value is rounded to the nearest integer pixel position.

The above equations are based on the assumption that lines are to be processed from left endpoint to right endpoint.

Page 20: Chapter 3 Output Primitives

DDAIn case the line is processed from Right endpoint to Left endpoint, then

Δx = −1, yk+1 = yk − m for m ≤ 1 (3)

orΔy = −1, xk+1 = xk −1/m for m > 1 (4)

Page 21: Chapter 3 Output Primitives

DDA- Line with negative SlopeIf m < 1,

use(1) [provided line is calculated from left to right] anduse(3) [provided line is calculated from right to left].

If m ≥ 1use (2) or (4).

Page 22: Chapter 3 Output Primitives

Merits + DemeritsFaster than the direct use of line Eqn.It eliminates the multiplication in line Eqn.For long line segments, the true line Path may be mislead due to round off.Rounding operations and floating-point arithmetic are still time consuming.The algorithm can still be improved.Other algorithms, with better performance also exist.

Page 23: Chapter 3 Output Primitives

DDA AlgorithmStart with starting and ending coordinates of the line:

(x0, y0) and (x1, y1)

Color first pixel (round to nearest integer)Suppose x1-x0 > y1-y0 (gentle slope)

There will be x1-x0 steps (# pixels to be colored)

Set x=x0, y=y0At each step,

increment x by (x1-x0)/numsteps, and increment y by (y1-y0)/numsteps

For each step, round off x and y to nearest integer, and color pixel

Page 24: Chapter 3 Output Primitives

// assume that slope is gentleDDA(float x0, float x1, float y0, float y1) { float x, y; float xinc, yinc; int numsteps;

numsteps = Round(x1) – Round(x0); xinc = (x1 – x0) / numsteps; yinc = (y1 – y0) / numsteps; x = x0; y = y0; ColorPixel(Round(x),Round(y)); for (int i=0; i<numsteps; i++) { x += xinc; y += yinc; ColorPixel(Round(x),Round(y)); }}

Q: For each step, how many floating point operations are there?A: 4

Q: For each step, how many integer operations are there?A: 2

DDA Pseudo-code

Page 25: Chapter 3 Output Primitives

DDA ExampleSuppose we want to draw a line starting at pixel (2,3) and ending at pixel (12,8).What are the values of the variables x and y at each timestep?What are the pixels colored, according to the DDA algorithm?

numsteps = 12 – 2 = 10xinc = 10/10 = 1.0yinc = 5/10 = 0.5

t x y R(x) R(y)

0 2 3 2 3

1 3 3.5 3 4

2 4 4 4 4

3 5 4.5 5 5

4 6 5 6 5

5 7 5.5 7 6

6 8 6 8 6

7 9 6.5 9 7

8 10 7 10 7

9 11 7.5 11 8

10 12 8 12 8

Page 26: Chapter 3 Output Primitives

Bresenham’s Line AlgorithmIt is an efficient raster line generation algorithm. It can be adapted to display circles and other curves.

The algorithmAfter plotting a pixel position (xk, yk) , what is the next pixel to plot?

Consider lines with positive slope.

Page 27: Chapter 3 Output Primitives

Bresenham’s LineFor a positive slope, 0 < m < 1 and line is starting from left to right.

After plotting a pixel position (xk, yk) we have two choices for next pixel:

(xk +1, yk)

(xk +1, yk+1)

Page 28: Chapter 3 Output Primitives

Bresenham’s LineFor a positive slope, 0 < m < 1 and line is starting from left to right.

After plotting a pixel position (xk, yk) we have two choices for next pixel:

(xk +1, yk)

(xk +1, yk+1)

Page 29: Chapter 3 Output Primitives

At position xk +1, we pay attention to the intersection of the vertical pixel and the mathematical line path.

Bresenham’s Line

Page 30: Chapter 3 Output Primitives

At position xk +1, we label vertical pixel separations from the mathematical line path as

dlower , dupper.

Bresenham’s Line

Page 31: Chapter 3 Output Primitives

The y coordinate on the mathematical line at xk+1 is calculated as

y = m(xk +1)+ bthen

dlower = y − yk

= m (xk +1) + b − yk

and

dupper =(yk +1) − y

= yk +1− m(xk +1)− b

Bresenham’s Line

Page 32: Chapter 3 Output Primitives

Bresenham’s LineTo determine which of the two pixels is closest to the line path, we set

an efficient test based on the difference between the two pixel

separations

dlower - dupper = 2m (xk +1) − 2yk + 2b - 1= 2 (Δy / Δx) (xk +1) − 2yk + 2b - 1

Consider a decision parameter pk such thatpk = Δx (dlower - dupper )

= 2Δy.xk − 2Δx.yk + c

wherec = 2Δy + Δx(2b −1)

Page 33: Chapter 3 Output Primitives

Bresenham’s LineSince Δx > 0, Comparing (dlower and dupper ), would tell which pixel is closer to the line path; is it yk or yk + 1

If (dlower < dupper ) Then pk is negative

Hence plot lower pixel.Otherwise

Plot the upper pixel.

Page 34: Chapter 3 Output Primitives

Bresenham’s LineWe can obtain the values of successive decision parameter as follows:

pk = 2Δy.xk − 2Δx.yk + cpk+1=2Δy.xk+1−2Δx.yk+1+c

Subtracting these two equations

pk+1− pk = 2Δy (xk+1 − xk) − 2Δx ( yk+1 − yk)

But xk+1 − xk = 1, Therefore

pk+1 = pk +2Δy − 2Δx (yk+1 − yk)

Page 35: Chapter 3 Output Primitives

( yk+1 − yk) is either 0 or 1, depending on the sign of pk (plotting lower or upper pixel).

The recursive calculation of pk is performed at integer x position, starting at the left endpoint.p0 can be evaluated as:

p0 = 2Δy − Δx

Bresenham’s Line

Page 36: Chapter 3 Output Primitives

Bresenham’s Line Drawing for m< 11. Input the two line end points and store the left end point in

(x0 , y0 ).

2. Load (x0 , y0 ) into the frame buffer; that is, plot the first point.

3. Calculate the constants Δx, Δy, 2Δy, and 2Δy − 2Δx, and obtain the starting value for the decision parameter as

p0 = 2Δy − Δx

4. At each xk along the line, starting at k = 0 , perform the following test: If pk < 0,the next point to plot is (xk +1, yk and

pk+1=pk+2Δy

Otherwise, the next point to plot is (xk +1, yk +1) andpk+1=pk+2Δy−2Δx

5. Repeat step 4, Δx−1 times.

Page 37: Chapter 3 Output Primitives

SummaryThe constants 2Δy and 2Δy − 2Δx are calculated once for each line to be scan converted.

Hence the arithmetic involves only integer addition and subtraction of these two constants.

Page 38: Chapter 3 Output Primitives

ExampleTo illustrate the algorithm, we digitize the line with endpoints (20,10) and (30,18). This line has slope of 0.8, with

Δx = 10Δy =8

The initial decision parameter has the valuep0 = 2Δy − Δx = 6

and the increments for calculating successive decision parameters are

2 Δy = 162 Δy - 2 Δx = -4

Page 39: Chapter 3 Output Primitives

ExampleWe plot the initial point (x0 , y0)=(20,10) and determine successive pixel positions along the line path from the decision parameter as

K pk (xk +1, yk +1) K pk (xk +1, yk +1)

0 6 (21,11) 5 6 (26,15)

1 2 (22,12) 6 2 (27,16)

2 -2 (23,12) 7 -2 (28,16)

3 14 (24,13) 8 14 (29,17)

4 10 (25,14) 9 10 (30,18)

Page 40: Chapter 3 Output Primitives

Circle Generating AlgorithmsA circle is defined as the set of points that are all at a given distance r from a center point (xc, yc).

For any circle point (x, y), this distance is expressed by the Equation

(x − xc)2 + (y − yc)2 = r 2

We calculate the points by stepping along the x-axis in unit steps from xc-r to xc+r and calculate y values as

Page 41: Chapter 3 Output Primitives

Circle Generating AlgorithmsThere are some problems with this approach:1. Considerable computation at each step.2. Non-uniform spacing between plotted pixels as in this

Figure.

Page 42: Chapter 3 Output Primitives

Circle Generating AlgorithmsProblem 2 can be removed using the polar form:

x = xc + r cos θ

y = yc + r sin θ

Using a fixed angular step size, a circle is plotted with equally spaced points along the circumference.

Page 43: Chapter 3 Output Primitives

Circle Generating AlgorithmsProblem 1 can be overcome by considering the symmetry of circles as in Figure. But it still requires a good deal of computation time.

Efficient Solutions Midpoint Circle Algorithm

Page 44: Chapter 3 Output Primitives

Mid point Circle AlgorithmTo apply the midpoint method, we define a circle function:

Any point (x,y) on the boundary of the circle with radius r satisfies the equation fcircle(x, y)= 0.

Page 45: Chapter 3 Output Primitives

Mid Point Circle AlgorithmIf the points is in the interior of the circle, the circle function is negative.

If the point is outside the circle, the circle function is positive.

To summarize, the relative position of any point (x,y) can be determined by checking the sign of the circle function:

Page 46: Chapter 3 Output Primitives

The circle function tests in (3) are performed for the mid positions between pixels near the circle path at each sampling step. Thus, the circle function is the decision parameter in the midpoint algorithm, and we can set up incremental calculations for this function as we did in the line algorithm.

Mid Point Circle Algorithm

Page 47: Chapter 3 Output Primitives

This Figure shows the midpoint between the two candidate pixels at sampling position xk +1. Assuming we have just plotted the pixel at (xk , yk), we next need to determine whether the pixel at position (xk +1, yk) or the one at position (xk +1, yk −1) is closer to the circle.

Mid Point Circle Algorithm

Page 48: Chapter 3 Output Primitives

Mid Point Circle AlgorithmOur decision parameter is the circle function (2) evaluated at the midpoint between these two pixels:

Page 49: Chapter 3 Output Primitives

Mid Point Circle AlgorithmIf pk < 0, this midpoint is inside the circle and the pixel on scan line yk is closer to the circle boundary.

Otherwise, the midpoint is outside or on the circle boundary, and we select the pixel on scan line yk −1.

Successive decision parameters are obtained using incremental calculations.

Page 50: Chapter 3 Output Primitives

We obtain a recursive expression for the next decision parameter by evaluating the circle function at sampling position xk+1 +1 = xk + 2

where yk+1 is either yk or yk-1,depending on the sign of pk.

Mid Point Circle Algorithm

Page 51: Chapter 3 Output Primitives

Mid Point Circle AlgorithmIncrements for obtaining pk+1 are either :

2xk+1 +1 (if pk is negative) or

2xk+1 +1− 2yk+1 (if pk is positive)

Evaluation of the terms 2xk+1 and 2yk+1 can also be

done incrementally as:

Page 52: Chapter 3 Output Primitives

Mid Point Circle Algorithm

At the start position (0, r), these two terms (2x, 2y) have the values 0 and 2r, respectively.

Each successive value is obtained by adding 2 to the previous value of 2x and subtracting 2 from the previous value of 2y.

Page 53: Chapter 3 Output Primitives

Mid Point Circle AlgorithmThe initial decision parameter is obtained by evaluating the circle function at the start position (x0 , y0)=(0, r):

Page 54: Chapter 3 Output Primitives

Mid Point Circle AlgorithmIf the radius r is specified as an integer, we can simply round p0 to

since all increments are integers.

Page 55: Chapter 3 Output Primitives

As in Bresenham’s line algorithm, the midpoint method calculates pixel positions along the circumference of a circle using integer additions and subtractions, assuming that the circle parameters are specified in screen coordinates.

Summary

Page 56: Chapter 3 Output Primitives

Algorithm

Page 57: Chapter 3 Output Primitives

ExampleGiven a circle radius r = 10, we demonstrate the midpoint circle algorithm by determining positions along the circle octant in the first quadrant from x = 0 to x = y . The initial value of the decision parameter is

Page 58: Chapter 3 Output Primitives

ExampleFor the circle centered on the coordinate origin, the initial point is (x0 , y0) =(0,10), and initial increment terms for calculating the decision parameters are

Successive decision parameter values and positions along the circle path are calculated using the midpoint method as shown in the table.

Page 59: Chapter 3 Output Primitives

Example

Page 60: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmEllipse equations are greatly simplified if the major and minor axes are oriented to align with the coordinate axes.

In Fig. 3-22, we show an ellipse in “standard position” with major and minor axes oriented parallel to the x and y axes.

Parameter rx for this example labels the semimajor axis, and parameter ry labels the semiminor axis.

Page 61: Chapter 3 Output Primitives

The equation for the ellipse shown in Fig. 3-22 can be written in terms of the ellipse center coordinates and parameters rx and ry as

Midpoint Ellipse Algorithm

Page 62: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmUsing polar coordinates r and θ, we can also describe the ellipse in standard position with the parametric equations :

Page 63: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmThe midpoint ellipse method is applied throughout the first quadrant in two parts.

Figure 3-25 shows the division of the first quadrant according to the slope of an ellipse with rx < ry.

Page 64: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmRegions 1 and 2 (Fig. 3-25) can be processed in various ways.

We can start at position (0, ry) and step clockwise along the elliptical path in the first quadrant, shifting from unit steps in x to unit steps in y when the slope becomes less than −1.0.

Alternatively, we could start at (rx, 0) and select points in a counterclockwise order, shifting from unit steps in y to unit steps in x when the slope becomes greater than −1.0.

Page 65: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmWe define an ellipse function from Eq. 3-37 with (xc , yc) = (0, 0) as:

which has the following properties:

Page 66: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmStarting at (0, ry), we take unit steps in the x direction until we reach the boundary between region 1 and region 2 (Fig. 3-25).

Then we switch to unit steps in the y direction over the remainder of the curve in the first quadrant.

At each step we need to test the value of the slope of the curve.

Page 67: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmThe ellipse slope is calculated from Eq. 3-39 as

At the boundary between region 1 and region 2, dy/dx = −1.0 and

Therefore, we move out of region 1 whenever

Page 68: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmFigure 3-26 shows the midpoint between the two candidate pixels at sampling position xk +1 in the first region.

Assuming position (xk , yk) has been selected in the previous step, we determine the next position along the ellipse path by evaluating the decision parameter (that is, the ellipse function 3-39) at this midpoint:

Page 69: Chapter 3 Output Primitives

Midpoint Ellipse Algorithm

If p1k < 0, the midpoint is inside the ellipse and the pixel on scan line yk is closer to the ellipse boundary.

Otherwise, the mid position is outside or on the ellipse boundary, and we select the pixel on scan line yk − 1.

Page 70: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmAt the next sampling position (xk+1 + 1 = xk + 2), the decision parameter for region 1 is evaluated as

Page 71: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmDecision parameters are incremented by the following amounts:

Page 72: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmAt the initial position (0, ry), these two terms evaluate to :

As x and y are incremented, updated values are obtained by adding 2r 2

y to the current value of the increment term in Eq. 3-45 and subtracting 2r 2

x from the current value of the increment term in Eq. 3-46.

The updated increment values are compared at each step, and we move from region 1 to region 2 when condition 3-42 is satisfied.

Page 73: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmIn region 1, the initial value of the decision parameter is obtained by evaluating the ellipse function at the start position (x0, y0) = (0, ry):

Page 74: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmOver region 2, we sample at unit intervals in the negative y direction, and the midpoint is now taken between horizontal pixels at each step (Fig. 3-27).

For this region, the decision parameter is evaluated as

Page 75: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmIf p2k > 0, the midposition is outside the ellipse boundary, and we select the pixel at xk.

If p2k <= 0, the midpoint is inside or on the ellipse boundary, and we select pixel position xk+1.

Page 76: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmTo determine the relationship between successive decision parameters in region 2,we evaluate the ellipse function at the next sampling step yk+1 −1 = yk −2:

Page 77: Chapter 3 Output Primitives

Midpoint Ellipse AlgorithmWhen we enter region 2, the initial position (x0, y0) is taken as the last position selected in region 1 and the initial decision parameter in region 2 is then

Page 78: Chapter 3 Output Primitives

Algorithm

Page 79: Chapter 3 Output Primitives

Algorithm

Page 80: Chapter 3 Output Primitives

ExampleGiven input ellipse parameters rx =8 and ry = 6, we illustrate the steps in the midpoint ellipse algorithm by determining raster positions along the ellipse path in the first quadrant.

Initial values and increments for the decision parameter calculations are

Page 81: Chapter 3 Output Primitives

ExampleFor region 1, the initial point for the ellipse centered on the origin is (x0, y0) = (0, 6), and the initial decision parameter value is

Successive midpoint decision parameter values and the pixel positions along the ellipse are listed in the following table.

Page 82: Chapter 3 Output Primitives

ExampleFor region 1, the initial point for the ellipse centered on the origin is (x0, y0) = (0, 6), and the initial decision parameter value is

Successive midpoint decision parameter values and the pixel positions along the ellipse are listed in the following table.

Page 83: Chapter 3 Output Primitives

Example

Page 84: Chapter 3 Output Primitives

ExampleWe now move out of region 1, since

2r 2 y x > 2r 2 x y.

For region 2, the initial point is

(x0, y0) = (7, 3)

and the initial decision parameter is

Page 85: Chapter 3 Output Primitives

ExampleThe remaining positions along the ellipse path in the first quadrant are then calculated as

Page 86: Chapter 3 Output Primitives

ExampleA plot of the calculated positions for the ellipse within the first quadrant is shown bellow:

Page 87: Chapter 3 Output Primitives

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. is typically used in general graphics packages to fill polygons, circles, ellipses

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

Filled Area Primitives

Page 88: Chapter 3 Output Primitives

Filled Area PrimitivesScan-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.

Page 89: Chapter 3 Output Primitives

Filled Area Primitives

Page 90: Chapter 3 Output Primitives

Filled Area PrimitivesCalculations performed in scan-conversion and other graphics algorithms typically take advantage of various coherence properties of a scene that is to be displayed.

Coherence is simply that the properties of one part of a scene are related in some way to other parts of the scene so that the relationship can be used to reduce processing.

Coherence methods often involve incremental calculations applied along a single scan line or between successive scan lines.

Page 91: Chapter 3 Output Primitives

Inside-Outside TestsArea-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

Page 92: Chapter 3 Output Primitives

Inside-Outside TestsOdd-Even rule (Odd Parity Rule, Even-Odd Rule):

Draw a line from any position P to a distant point outside theco ordinate extents of the object and counting the number of

edgecrossings along the line.

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

P is an interior point.Else P is an exterior point

Page 93: Chapter 3 Output Primitives

Inside-Outside TestsNonzero Winding Number Rule :

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

valuefor the winding number.

1. Initializing the winding number to Zero.2. Imagine a line drawn from any position P to a distant point

beyond the coordinate extents of the object.

Page 94: Chapter 3 Output Primitives

Inside-Outside TestsNonzero Winding Number Rule :

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.

Page 95: Chapter 3 Output Primitives

Inside-Outside Tests

Page 96: Chapter 3 Output Primitives

Boundary Fill algorithmStart 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

Page 97: Chapter 3 Output Primitives

Boundary Fill AlgorithmStarting 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:

Page 98: Chapter 3 Output Primitives

Boundary Fill Algorithm1. The 4-connected

method.

2. The 8-connected method.

Page 99: Chapter 3 Output Primitives

Boundary Fill Algorithmvoid boundaryFill4 (int x, int y, int fillColor, int borderColor)

{ int interiorColor;

/* set current color to fillColor, then perform following

operations. */

interiorColor =getPixel (x, y);

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);

}

}

Page 100: Chapter 3 Output Primitives

Boundary Fill Algorithm

Page 101: Chapter 3 Output Primitives

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.

Boundary Fill Algorithm

Page 102: Chapter 3 Output Primitives

Pixel Span MethodStart from the initial interior point, then fill in the contiguous span of pixels on this starting scan line. Then we locate and stack starting positions for spans on the adjacent scan lines, where spans are defined as the contiguous horizontal string of positions bounded by pixels displayed in the area border color.

At each subsequent step, we unstack the next start position and repeat the process. An example of how pixel spans could be filled using this approach is illustrated for the 4-connected fill region in the following figure.

Page 103: Chapter 3 Output Primitives

Pixel Span Method

Page 104: Chapter 3 Output Primitives

Pixel Span Method

Page 105: Chapter 3 Output Primitives

Flood Fill algorithmSometimes 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.

Page 106: Chapter 3 Output Primitives

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.

Flood Fill algorithm

Page 107: Chapter 3 Output Primitives

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);

}

}

Flood Fill algorithm

Page 108: Chapter 3 Output Primitives

CS 380

Flood-Fill Algorithm (cont.)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);

}

}