Você está na página 1de 67

1

COMPUTER GRAPHICS
Algorithms for Graphics Primitives

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 6-1 Stair-step effect (jaggies) produced when a line is generated as a
series of pixel positions.

Line equations
Figure 6-2 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 m1
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 m1, 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

Bresenhams line algorithm


Goal: Use only incremental integer calculations (fast!)
Figure 6-4 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 6-5 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.

10

Bresenhams 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 6-6 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.

11

Bresenhams line algorithm


At sampling position xk+1, label vertical pixel separations

from the line path as dlower and dupper

Figure 6-7 Vertical distances


between pixel positions and the line y
coordinate at sampling position xk + 1.

12

Bresenham

To determine which pixel is closet , we first calculate:

13

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 2y+x(2b-1) which is
independent of the pixel position and will be eliminated in
the recursive calculation

14

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

15

Bresenham algorithm

16

Example

Figure 6-8 Pixel positions along the line path


between endpoints (20, 10) and (30, 18),
plotted with Bresenhams line algorithm.

17

Example

18

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

19

Circle-generating algorithms
One could use
Sqrt is time-consuming

Figure 6-12 Upper half of a circle plotted with


Equation 6-27 and with (xc , yc) = (0, 0).

20

Circle equation in parametric polar form

Trig calculations are time-consuming


We can use symmetry

Figure 6-13 Symmetry of a circle. Calculation


of a circle point (x, y) in one octant yields the
circle points shown for the other seven octants.

21

Midpoint circle algorithm


First, define:

We will perform the tests for the midpositions between

pixels near the circle path at each sampling step

22

Midpoint circle algorithm


Figure 6-14 Midpoint between candidate pixels at sampling position xk + 1 along a circular
path.

23

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

24

Recursive expression for pk

25

Recursive expression

26

The algorithm (only integer add/subtract)

27

Example

28

Example

29

Example
Figure 6-15 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.

30

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

31

Ellipse in standard position


Figure 6-17 Ellipse centered at (xc, yc) with
semimajor axis rx and semiminor axis ry.

32

Ellipse in standard position


Figure 6-18 The bounding circle and eccentric angle for an ellipse with rx > ry .

33

Midpoint ellipse algorithm


Figure 6-20 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.

34

Midpoint ellipse algorithm

35

Midpoint ellipse algorithm (region 1)


Figure 6-21 Midpoint between candidate pixels at sampling position xk + 1 along an elliptical
path.

36

Ellipse

37

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

38

Ellipse

39

Midpoint ellipse algorithm

40

Midpoint ellipse algorithm

41

Example

42

Example

43

Example

44

Example
Figure 6-23 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.

45

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 6-46 Interior pixels


along a scan line passing
through a polygon fill area.

46

Complexities of polygon filling


Figure 6-47 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.

47

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

48

Polygon filling: endpoint adjustments


Figure 6-48 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.

49

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

50

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

51

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

52

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

53

Processing neighboring pixels


Figure 6-53 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.

54

4-connected vs. 8-connected algorithm


Figure 6-54 The area defined within the color boundary (a) is only partially filled in (b) using a 4-connected boundaryfill algorithm.

55

Boundary-fill

56

Flood-fill
Sometimes we want to ll in (or recolor) an area that is

not dened within a single color boundary


We can paint such areas by replacing a specied interior
color instead of searching for a particular boundary color.

Figure 6-56 An area defined


within multiple color boundaries.

57

Flood-fill

58

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 cant 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

59

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

60

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
Postfiltering: involves computing intensities at subpixel
grid positions and then combining the results to obtain the
pixel intensities

61

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

62

Postfiltering: Subpixel weighting masks


Give more weight to subpixel near the center of a pixel

area

Figure 6-61 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.

63

Example masks or window functions

(b) and (c) are approximations to the Bartlett window:


Weights grow linearly from the edges toward the center

64

Application of weighting masks or window functions

65

Area sampling (prefiltering)


Set pixel intensity proportional to the area of overlap of

pixel with the finite-width line


Prefiltering: the intensity of the pixel as a whole is
determined without calculating subpixel intensities
Computationally expensive

66

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 6-62 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.

67

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 6-63 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 2304. (Courtesy
of Peritek Corp.)

Você também pode gostar