# CMPE 466 COMPUTER GRAPHICS

## Presentation on theme: "CMPE 466 COMPUTER GRAPHICS"— Presentation transcript:

CMPE 466 COMPUTER GRAPHICS
Chapter 6 Algorithms for Graphics Primitives Instructor: D. Arifler Material based on Computer Graphics with OpenGL®, Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. Carithers Fundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve Marschner Computer Graphics by F. S. Hill

Device-level algorithms for implementing primitives
Line-drawing algorithms Circle-generating algorithms Ellipse-generating algorithms Scan-line polygon-fill algorithms Antialiasing

Jaggies in line-drawing
Figure Stair-step effect (jaggies) produced when a line is generated as a series of pixel positions.

Line equations Figure Line path between endpoint positions (x0, y0) and (x end, y end).

Determining deflection voltages

Digital differential analyzer (DDA)
A scan-conversion line algorithm For m≤1 Sample at unit x intervals: δx=1 Compute yk+1=yk+m Calculated y values must be rounded to nearest int For m>1 Sample at unit y intervals: δy=1 Compute xk+1=xk+1/m Calculated x values must be rounded to nearest int If we process from right to left and m≤1, then we have δx=-1 and yk+1=yk-m If we process from right to left and m>1, then we have δy=-1 and xk+1=xk-1/m For negative slopes, look at the the magnitude of m

DDA

Problems of DDA Accumulation of round-off errors
Calculated pixel positions may drift away from the true line path for long segments Rounding operations and floating point arithmetic are time-consuming

Bresenham’s line algorithm
Goal: Use only incremental integer calculations (fast!) Figure A section of a display screen where a straight-line segment is to be plotted, starting from the pixel at column 10 on scan line 11. Figure A section of a display screen where a negative slope line segment is to be plotted, starting from the pixel at column 50 on scan line 50.

Bresenham’s line algorithm
Consider lines with slope m<1 Decide which pixel to plot in column xk+1=xk+1 Choices: (xk+1, yk) or (xk+1, yk+1) Figure A section of the screen showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope 0 < m < 1.

Bresenham’s line algorithm
At sampling position xk+1, label vertical pixel separations from the line path as dlower and dupper Figure Vertical distances between pixel positions and the line y coordinate at sampling position xk + 1.

Bresenham To determine which pixel is closet , we first calculate:

Bresenham We would like to use only integer calculations
Define a decision parameter pk. Letting m=Δy/Δx: Sign of pk is the same as the sign of dlower-dupper because Δx>0 Parameter c is constant and is 2Δy+Δx(2b-1) which is independent of the pixel position and will be eliminated in the recursive calculation

Bresenham If pixel at yk is closer to the line path than pixel at yk+1 (i.e. dlower<dupper) the pk is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixel We can obtain successive decision parameters recursively. At step k+1 using where yk+1-yk is either 0 or 1 depending on the sign of pk First parameter is calculated as

Bresenham algorithm

Example Figure Pixel positions along the line path between endpoints (20, 10) and (30, 18), plotted with Bresenham’s line algorithm.

Example

Circle-generating algorithms
Figure Circle with center coordinates (xc , yc) and radius r .

Circle-generating algorithms
One could use Sqrt is time-consuming Figure Upper half of a circle plotted with Equation 6-27 and with (xc , yc) = (0, 0).

Circle equation in parametric polar form
Trig calculations are time-consuming We can use symmetry Figure Symmetry of a circle. Calculation of a circle point (x, y) in one octant yields the circle points shown for the other seven octants.

Midpoint circle algorithm
First, define: We will perform the tests for the midpositions between pixels near the circle path at each sampling step

Midpoint circle algorithm
Figure Midpoint between candidate pixels at sampling position xk + 1 along a circular path.

Circles: decision parameter
Define the decision parameter If pk<0 (midposition is inside), yk is closer Otherwise, midposition is outside or on the circle boundary and we select the pixel on scan line yk-1

Recursive expression for pk

Recursive expression

Example

Example

Example Figure Pixel positions (solid circles) along a circle path centered on the origin and with radius r = 10, as calculated by the midpoint circle algorithm. Open (“hollow”) circles show the symmetry positions in the first quadrant.

Ellipse-generating algorithms
Figure Ellipse generated about foci F1 and F2.

Ellipse in standard position
Figure Ellipse centered at (xc, yc) with semimajor axis rx and semiminor axis ry.

Ellipse in standard position
Figure The bounding circle and eccentric angle θ for an ellipse with rx > ry .

Midpoint ellipse algorithm
Figure Ellipse processing regions. Over region 1, the magnitude of the ellipse slope is less than 1.0; over region 2, the magnitude of the slope is greater than 1.0.

Midpoint ellipse algorithm

Midpoint ellipse algorithm (region 1)
Figure Midpoint between candidate pixels at sampling position xk + 1 along an elliptical path.

Ellipse

Ellipse (region 2) Figure Midpoint between candidate pixels at sampling position yk − 1 along an elliptical path.

Ellipse

Midpoint ellipse algorithm

Midpoint ellipse algorithm

Example

Example

Example

Example Figure Pixel positions along an elliptical path centered on the origin with rx = 8 and ry = 6, using the midpoint algorithm to calculate locations within the first quadrant.

Scan-line polygon-fill algorithms
For each scan-line that crosses the polygon, the edge intersections are sorted from left to right, and then pixel positions between and including each intersection pair are set to the specified fill color Figure Interior pixels along a scan line passing through a polygon fill area.

Complexities of polygon filling
Figure Intersection points along scan lines that intersect polygon vertices. Scan line y generates an odd number of intersections, but scan line y’ generates an even number of intersections that can be paired to identify correctly the interior pixel spans.

Polygon filling For scan line y, the two edges sharing an intersection vertex are on opposite sides of the scan line For scan line y’, the two intersection edges are both above the scan line A vertex that has adjoining edge on opposite sides of an intersecting scan line should be counted as just one boundary intersection point. Endpoints of segments can be adjusted as shown in the next slide. In determining edge intersections, coherence methods are used. This often involves incremental calculations applied along a scan line or between scan lines

Figure Adjusting endpoint y values for a polygon, as we process edges in order around the polygon perimeter. The edge currently being processed is indicated as a solid line. In (a), the y coordinate of the upper endpoint of the current edge is decreased by 1. In (b), the y coordinate of the upper endpoint of the next edge is decreased by 1.

Edge tables for polygon filling
To perform a polygon-fill efficiently, we first store polygon boundary in a sorted edge table Proceeding around the edges (clockwise or counterclockwise), we can use a bucket sort to store the edges, sorted on the smallest y value of each edge, in the correct scan-line positions kth position points to a list of all edges having their lower endpoint on scan line k At kth position, edges are in sorted order from left to right. Next, we process the scan lines from the bottom of the polygon to its top, producing an active edge list (AEL) for each scan line crossing the polygon boundaries The active edge list for a scan line contains all edges crossed by that scan line

Edge tables Figure A polygon and its sorted edge table, with edge DC shortened by one unit in the y direction.

Fill methods for areas with irregular boundaries
Start at an inside position and paint the interior point by point out to the boundary Boundary-fill Fill the interior of a region pixel by pixel until the boundary color is encountered Flood-fill Fill in (recolor) an area that is not defined within a single color boundary Paint by replacing a specified interior color instead of searching for a particular boundary color

Boundary-fill Boundary-ﬁll algorithm starts from an interior point (x, y) and tests the color of neighboring positions If a tested position is not displayed in the boundary color, its color is changed to the ﬁll color and its neighbors are tested (see next slide) This procedure continues until all pixels are processed up to the designated boundary color for the area

Processing neighboring pixels
Figure Fill methods applied to a 4-connected area (a) and to an 8-connected area (b). Hollow circles represent pixels to be tested from the current test position, shown as a solid color.

4-connected vs. 8-connected algorithm
Figure The area defined within the color boundary (a) is only partially filled in (b) using a 4-connected boundary-fill algorithm.

Boundary-fill

Flood-fill Sometimes we want to ﬁll in (or recolor) an area that is not deﬁned within a single color boundary We can paint such areas by replacing a speciﬁed interior color instead of searching for a particular boundary color. Figure An area defined within multiple color boundaries.

Flood-fill

Antialiasing Aliasing: Distortion of information due to low-frequency sampling (undersampling) Increase sampling rate in raster systems Simply display at higher resolution: Requires large frame-buffers! Apple MacBook Retina Display: Over 5 million on the 15‑inch model: With so high pixel density, your eyes can’t discern individual pixels at normal viewing distance With raster systems that are capable of displaying more than two intensity levels per color, we can apply antialiasing methods to modify pixel intensities Antialiasing methods: Supersampling, postfiltering, prefiltering, pixel phasing We will focus on antialiasing methods for straight-line segments

Antialiasing Blurring to smooth an image
E.g. Sharp transitions from black to white is softened by using a mixture of gray pixels near the borders When looking from a distance, eye blends the gracefully varying shades together

Supersampling (a special case of post-filtering)
Sampling object characteristics at a higher resolution and displaying the results at a lower resolution Divide each pixel into a number of subpixels and count the number of subpixels that overlap the line path. The intensity for each pixel is set to a value proportional to this subpixel count “Post”filtering: involves computing intensities at subpixel grid positions and then combining the results to obtain the pixel intensities

Supersampling Figure Supersampling subpixel positions along a straight-line segment whose left endpoint is at screen coordinates (10, 20). Figure Supersampling subpixel positions in relation to the interior of a line of finite width.

Give more weight to subpixel near the center of a pixel area Figure Relative weights for a grid of 3 × 3 subpixels. Center subpixel is weighted by a factor of 1/4; the top, bottom, and side subpixels are each weighted by a factor of 1/8; and the corner subpixels are each weighted by a factor of 1/16.

(b) and (c) are approximations to the Bartlett window: Weights grow linearly from the edges toward the center

Application of weighting masks or window functions

Area sampling (prefiltering)
Set pixel intensity proportional to the area of overlap of pixel with the finite-width line “Pre”filtering: the intensity of the pixel as a whole is determined without calculating subpixel intensities Computationally expensive

General filtering techniques
More accurate method for antialiasing Similar to applying a weighted pixel mask but now we have a continuous weighting surface (or filter function) covering the pixel. Figure Common filter functions used to antialias line paths. The volume of each filter is normalized to 1.0, and the height gives the relative weight at any subpixel position.

Pixel phasing On raster systems that can address subpixel positions within the screen grid (hardware solution) Move (microposition) pixel positions closer to the line path Electron beam can be shifted by a fraction of a pixel diameter Figure Jagged lines (a), plotted on the Merlin 9200 system, are smoothed (b) with an antialiasing technique called pixel phasing. This technique increases the number of addressable points on the system from 768 by 576 to 3072 by (Courtesy of Peritek Corp.)