Download presentation
Presentation is loading. Please wait.
Published byRuby O’Neal’ Modified over 9 years ago
1
Basic Raster Graphics Algorithms for Drawing 2D Primitives
Chapter 3
2
1. Overview The purpose of this chapter is to look at a graphics package from the implementor’s point of view -- that is, in terms of the fundamental algorithms for scan converting primitives to pixels, subject to their attributes, and for clipping them against an upright clip rectangle Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
3
The final section introduces the concept of antialiasing
The algorithms in this chapter are discussed in terms of the 2D integer Cartesian grid, but most of the scan-conversion algorithms can be extended to floating point, and the clipping algorithms can be extended to both floating point and 3D. The final section introduces the concept of antialiasing the minimizing of jaggies by making use of a systems ability to vary a pixels intensity. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
4
2. Scan Converting Lines A scan conversion algorithm for lines computes the coordinates of the pixel that lie on or near the ideal line. For now, consider only a 1-pixel-thick line, we will consider thick primitives, and deal with style later in the chapter. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
5
Basic properties of good scan conversion:
Straight lines should appear as straight lines Lines should pass through the endpoints (if possible) All lines should be drawn with constant brightness (regardless of length and orientation) As rapid as possible Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
6
To visualize the geometry, most people represent a pixel as a circular dot centered at the pixel’s (x,y) location on the integer grid. This is a convenient approximation to the cross-section of the CRT’s electron beam. The exact spacing between spots on an actual display can vary greatly among systems. Some overlap while others leave spaces, but almost all have a tighter spacing in the horizontal than in the vertical direction. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
7
This figure shows a highly magnified view of a 1-pixel-thick line and the ideal line that it approximates. The intensified pixels are shown as filled circles, and the nonintensified pixels are shown as unfilled circles. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
8
2.1 The Basic Incremental Algorithm
The simplest strategy for scan converting lines is to use the slope-intercept formula y = mx+B Given two points on a line (x0,y0) and (x1,y1) compute the slope, m, of the line remember, m = Dy/Dx = (y1-y0)/(x1-x0) B = y0-mx0 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
9
The basic idea is to increment x by 1 from the starting point (x0,y0) to the ending point (x1,y1) calculate y at each point, yi=mxi+B turn on pixel (xi,Round(yi)) where Round(yi)=Floor(0.5+yi) This brute force strategy is inefficient because each iteration requires: floating point multiplication floating point addition Call to function Floor( ) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
10
We can eliminate the multiplication if we note:
yi+1 = mxi+1 + B = m(xi + Dx) + B =yi + mDx and if Dx == 1 yi+1 = yi+m Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
11
Other Cases: Drawbacks: If the slope is greater than 1,
you can swap the values of x and y If the slope is negative, you start from the right endpoint and go to the left. Drawbacks: Floating point addition Round ( ) Accumulation of floating point errors Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
12
2.2 Midpoint Line Algorithm
In 1965 Bresenham developed what is now the classic line drawing algorithm that removed the rounding and used only integer arithmetic. He showed that his algorithm provided the best-fit approximation to the true-line by minimizing the error. We are going to look at a generalization called the midpoint technique. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
13
Goal: Calculate which side of M the line is on.
Main Idea: Assumption: 0 <= m <= 1 We have selected P, We must choose E or NE Define Q as the intersection of the line with x=xp+1 If Q is above M, select NE, else select E Goal: Calculate which side of M the line is on. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
14
Line Representation Review
Implicit representation: F(x,y) = ax + by + c = 0 Slope-intercept representation: y = mx + B = (y1-y0)/(x1-x0) x + B = dy/dx x + B How are they related? xdy - ydx + Bdx = 0 (a=dy, b=-dx, C=Bdx) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
15
The sign of F(x,y) for any point (x,y)
F(x,y) = 0 => point is on the line F(x,y) > 0 => point is below the line F(x,y) < 0 => point is above the line Check the sign of d=F(xp+1, yp+1/2) if d > 0, choose pixel NE if d < 0, choose pixel E if d==0, choose either (we pick E) How should d be computed? Incrementally! Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
16
Remember by definition, If E was chosen, then we shift M over 1
d = a(xp+1)+b(yp+1/2)+c If E was chosen, then we shift M over 1 dnew=F(M1)= F(xp+2, yp+1/2) = a(xp+2)+b(yp+1/2)+c but, remember dold = F(xp+1, yp+1/2) therefore: dnew=dold+a OR DE = a If NE was chosen, then we shift M over & up 1 dnew=F(M2)= F(xp+2, yp+3/2) = a(xp+2)+b(yp+3/2)+c also, dold = F(xp+1, yp+1/2) therefore: dnew=dold+a + b OR DNE=a+b Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
17
Short Summary of the midpoint technique:
At each step we choose between two pixels based upon the sign of the decision variable (d) Then we update the d by adding DE or DNE depending on the choice of the pixel Now, how do we get started? We just need the first d value. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
18
The first midpoint, dstart, is
F(x0+1,y0+1/2) = a(x0+1)+b(y0+1/2)+c = ax0+by0+c+a+b/2 = F(x0,y0)+a+b/2 BUT (x0,y0) is on the line, so F(x0,y0)=0 therefore dstart = a+b/2 = dy-dx/2 Can we get rid of the division? Change the function F( ) to multiply everything by 2 This changes the scalars (including DE and DNE) but does not change the sign (which is what we are interested in) So, dstart = 2dy - dx So, now we have an algorithm based on integer arithmetic only. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
19
Example: (x0,y0)= (5,8) (x1,y1)=(9,11) dy = dx = DE = 2dy =
DNE = (dy-dx)*2 = dstart = 2dy - dx = choose: E or NE dnew = Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
20
2.3 Additional Issues Endpoint order:
A line from P0 to P1 should contain the same set of pixels as the line from P1 to P0. The only place where the choice of the pixel is dependent on the direction of the line is where the line passes directly through the midpoint. Going from left to right we picked E Therefore going from right to left we pick SW, not W We can’t just swap endpoints, since line styles are not swappable. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
21
Starting at the edge of a clip rectangle
Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
22
Varying the intensity of a line as a function of slope
Line B has slope of 1 and is times as long as A, yet each line has 10 pixels. If intensity per pixel is constant, then the intensity per unit length is easily discernable by the user. Solution: make intensity a function of the slope. Antialiasing (3.14) will cover another method. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
23
Outline primitives composed of lines
Polylines can be scan-converted one line segment at a time. Rectangles and Polygons can be done a line segment at a time, but this would result in some pixels being drawn outside. Sect 3.4 and 3.5 cover these. Shared vertices care must be taken to draw shared vertices of polylines only once. Drawing twice can change the color, shut it off, or if writing to film make the intensity too bright. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
24
3. Scan Converting Circles
Review of basic circle formulas: x2+y2 = R2 circle centered at the origin (0,0) with radius R. (x-x0)2 + (y-y0)2 = R2 Circle centered at (x0,y0) with radius R. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
25
A simple algorithm Overview: Outline:
Draw only one quarter of the circle, use symmetry for the other three quarters. Outline: Step 1: x1 = 0, Dx = 1 Step 2: Step 3: round(yi) Step 4: draw (xi,round(yi)) Step 5:xi=xiold+Dx Step 6: if xi <= R goto Step 2 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
26
Computational cost: Problems:
floating point multiplications, subtractions, square-root( ), round( ) Problems: large gaps for x values close to R Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
27
3.1 Eight-Way Symmetry Draw one octant of the circle
Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
28
3.2 Midpoint Circle Algorithm
Bresenham in 1977 developed an incremental circle generator (for pen plotters) that is the foundation for the algorithm we will discuss. We will begin at x=0, and continue through the first octant. ( ) We will use the symmetry from the previous subsection to plot the rest of the circle. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
29
Overview: Assume that P has been chosen as closest to the circle.
The next choice is pixel E and SE. if M is inside the circle, choose E if M is outside, choose SE Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
30
The sign of F(x,y) for any point (x,y)
F(x,y) = x2 + y2 - R2 = 0 The sign of F(x,y) for any point (x,y) F(x,y) = 0 => point is on the circle F(x,y) > 0 => point is outside the circle F(x,y) < 0 => point is inside the circle Check the sign of d=F(xp+1, yp-1/2) if d > 0, choose pixel SE if d < 0, choose pixel E if d==0, choose either (we pick SE) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
31
Remember by definition: If E was chosen, then we shift M over 1
d = (xp+1)2+(yp-1/2)2-R2 If E was chosen, then we shift M over 1 dnew=F(M1)= F(xp+2, yp-1/2) = (xp+2)2+(yp-1/2)2-R2 a bunch of algebra later we get dnew=dold+(2xp+3) OR DE = (2xp+3) If SE was chosen, then M goes over & down 1 dnew=F(M2)= F(xp+2, yp-3/2) = (xp+2)2+(yp-3/2)2-R2 more algebra... dnew=dold+(2xp-2yp+5) OR DSE= +(2xp-2yp+5) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
32
Did you notice that DE and DSE were not constants?
Short Summary: choose between E and SE, based on the sign of d Update d by adding either DE or DSE All that remains is to compute the initial condition, dstart. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
33
How should we compute dstart.
The first midpoint will be at (1,R-1/2) F(1, R - 1/2) = 1 + (R - 1/2)2 - R2 = 5/4 - R Moving the algorithm from floats to ints Define h = d - 1/4 then replace d by h+1/4 hstart = 1 - R Now, d<0 becomes h<-1/4 but since these are all ints, it is equivalent to h<0 Now replace h with d, and you have Program 3.4 pg 83. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
34
Second-order differences
If E is chosen: (xp, yp) --> (xp+1, yp) DEold = 2xp+3 DEnew = 2(xp+1)+3 DEnew - DEold = 2 or DEnew = DEold + 2 DSEold = 2xp-2yp+5 DSEnew = 2(xp+1)+3 DSEnew - DSEold = 2 or DSEnew = DSEold + 2 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
35
If SE is chosen: (xp, yp) --> (xp+1, yp-1)
DEold = 2xp+3 DEnew = 2(xp+1)+3 DEnew - DEold = 2 or DEnew = DEold + 2 DSEold = 2xp-2yp+5 DSEnew = 2(xp+1) - 2(yp - 1) + 5 DSEnew - DSEold = 4 or DSEnew = DSEold + 4 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
36
The revised algorithm has the following steps:
Choose the pixel based on the sign of d computed during the previous iteration. Update d based with either DE or DSE. Update the D’s to take into account the move to the new pixel using the constant differences computed previously. Do the move. How should DE and DSE be initialized? Use the starting point (0,R) DEstart = 2xp+3 = 3 DSEstart = 2xp - 2yp + 5 = 5 - 2R Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
37
4. Filling Rectangles The task of filling primitives can be broken down into two parts: the decision on which pixels to fill and the easier decision of what to fill them with We will first discuss filling unclipped primitives with a solid color We will see patterns in Section 6 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
38
In general, determining which pixels to fill consists of taking successive scan lines that intersect the primitive and filling in spans of adjacent pixels from left to right. Basically we look for those pixels at which changes occur. By doing this, we take advantage of the coherence of the object -- the fact that is does not change very rapidly. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
39
5. Filling Polygons The general polygon scan-conversion algorithm we will study next handles both convex and concave polygons, even those that are self-intersecting or have interior holes. It operates by computing spans that lie between left and right edges of the polygon. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
40
In this figure we can see the basic polygon scan-conversion process.
Note: for line 8 a and d are at integer values, whereas b and c are not. We must determine which pixels on each scan line are within the polygon, and we must set the corresponding pixels (x=2 through 4, and 9 through 13) to their appropriate value. We then repeat this process for each scan line that intersects the polygon. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
41
This figure shows one problem
If we use the midpoint line scan-conversion algorithm to find the end of spans, we will select pixels outside the polygon Not good for polygons that share edges. (a) uses midpoint (b) uses interior only Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
42
In scan line 8 of this figure, there are two spans of pixels
As with the original midpoint algorithm, we will use an incremental algorithm to calculate the span extrema on one scan line from those on the previous scan line. In scan line 8 of this figure, there are two spans of pixels These spans can be filled in a three step process Find the intersections of the scan line with all edges of the polygon. Sort the intersections by increasing x coordinate Fill pixels between pairs that lie interior to the polygon Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
43
In this figure the sorted coordinates are:
The first two steps will be covered later, so let us look at the span filling strategy. In this figure the sorted coordinates are: 2, 4.5, 8.5, and 13 When dealing with the span filling there are four elaborations we need to make. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
44
These elaborations are:
Given an intersection with an arbitrary, fractional x value, how do we determine which pixel on either side of that intersection is interior? How do we deal with the special case of intersections at integer pixel coordinates? How do we deal with the special case in the second elaboration for shared vertices? How do we deal with the special case in the second elaboration in which the vertices define a horizontal edge. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
45
To handle the first elaboration, we say that
if we are approaching a fractional intersection to the right and inside the polygon, we round down the x coordinate of the intersection to define the interior pixel; if we are outside the polygon, we round up to be inside, We handle the second elaboration by applying the criterion we used to avoid conflicts at shared edges of rectangles If the leftmost pixel in a span has integer x coordinate, we define it to be interior; If the rightmost pixel has integer x coordinate, we define it to be exterior. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
46
For the third case, we count the ymin vertex of an edge in the parity calculation, but not the ymax vertex; therefore a ymax vertex is drawn only if it is the ymin vertex for the adjacent edge. Consider vertex A in this example. It is counted in the parity calculation since it is the ymin for FA, and ignored as the ymax for AB Thus edges are treated as intervals (closed at ymin, and open at ymax) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
47
For the fourth case (horizontal edges) the desired effect is that, as with rectangles, bottom edges are drawn but top edges are not. We will see int he next subsection that this happens if we follow the ymax ymin rules just outlined. Examples: scan line 8 note pixel at d is not inside polygon by second clarification, therefor it is not drawn. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
48
Examples (cont.): scan line 3 A counts since it is a ymin for AF.
hits vertex B AB and BC have their ymins at B therefore both change the parity therefore B is drawn. scan line 9 hits vertex F FA and EF have their ymaxs at F (therefore neither change the parity) therefore nothing is drawn Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
49
5.1 Horizontal Edges We deal properly with horizontal edges by not counting their vertices Here are the rules: Don’t consider the vertex contribution for the horizontal edge Invert the parity if the vertex is a ymin of some edge. Do not invert if the vertex is a ymax of some edge Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
50
Examples: Consider the bottom edge AB.
Vertex A is a ymin for edge JA, and AB does not contribute. Therefore the parity is odd and the span AB is drawn. Vertical edge BC has its ymin at B, but again AB does not contribute. The parity becomes even, and the span is terminated. At vertex J, edge IJ has a ymin vertex but edge JA does not, so the parity becomes odd and the span is drawn to edge BC. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
51
Examples (cont.): The span that starts at edge IJ (and hits C)
sees no change at C because C is a ymax vertex for BC, so the span continues along bottom edge CD However at D, the edge DE has a ymin, so the parity s reset to even and the span ends. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
52
Examples (cont.): The span that starts at I
the edge IJ has a ymax and edge HI does not contribute, so parity stays even and the top edge IH is not drawn. At H, however, edge GH has a ymin, so the parity becomes odd and the span is drawn from H to the pixel to the left of the intersection with edge EF Finally, there is no ymin vertex at G, nor is there one at F, so the top edge GF is not drawn. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
53
Issues: Good: Bad: Deals with shared vertices in a polygon
Deals with edges shared by two adjacent polygons Deals with horizontal edges It also allows self-intersecting polygons. Bad: It omits pixels it cannot totally avoid writing shared pixels multiple times without a history. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
54
5.2 Slivers There is another problem with our scan-conversion algorithm, one that is not resolved as satisfactorily as is that of horizontal edges: polygons with slivers – an area so thin that its interior does not contain a distinct span for each scan line. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
55
This problem of having missing pixels is another example of the aliasing problem – representing a continuous signal with a discrete approximation. If we had multiple bits per pixel, we could use antialiasing techniques as introduced for lines in 3.14 Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
56
5.3 Edge Coherence and the Scan-Line Algorithm
Step 1 in our procedure – calculating intersections – must be done cleverly. Brute Force Method: (very slow) test every polygon edge for intersection with every scan line. Efficient method: take advantage of edge coherence Note: Calculate the x intercept based upon the previous one. And this can be done completely with integers. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
57
Now we can develop a scan-line algorithm that takes advantage of this edge coherence.
We will use a data structure called the active-edge table (AET) the edges in the AET are sorted on their x intersection values, so we can fill the spans defined by pairs of intersection values (span extremas) As we move to the next scan line, y+1, the AET is updated first those edges in the table whose ymax = y are deleted second those edges not in the table whose ymin=y+1 are added finally, new x intersections are calculated using the incremental edge algorithm Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
58
To make the addition of edges to the AET efficient, we initially create a global edge table (ET)
this contains all edges sorted by their smaller y coordinate the ET is typically built using a bucket sort, with as many buckets as there are scan lines. Inside the bucket they are sorted by the x coordinate at the lower endpoint. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
59
Once the ET has been filled, here is our algorithm:
set y to the smallest y coordinate with an entry in the ET initialize AET to empty. Repeat until AET and ET are empty. move from ET to AET bucket y remove from AET those entries for which y=ymax, then sort AET on x fill in the desired pixel values on scan y using pairs of x coordinates from AET y++ for each nonvertical edge in AET, update x for the new y (use slope,…) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
60
6. Pattern Filling Introduction:
So far we have filled the interiors of a primitive with a solid color. Now we want to consider filling with a pattern We do this by adding extra control to the part of the scan conversion algorithm that actually writes each pixel. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
61
6.1 Pattern Filling Using Scan Conversion
The main issue for filling with pattern is the relation of the area of the pattern to that of the primitive Where is the pattern anchored? Choice 1 – Anchor to the leftmost pixel in the first row. some primitives (circles) cause you problems… Choice 2 – Have the programmer specify the anchor. Choice 3 – consider the entire screen as tiled with the primitive (anchor is lower left corner of the screen) computationally efficient and easy. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
62
6.2 Pattern Filling without Repeated Scan Conversion
Another technique is to scan convert the primitive first into a rectangular work area, and then to write each pixel from the bitmap to the appropriate place in the canvas. This two-step process is twice as much work as filling during scan conversion, and Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
63
This two-step process is twice as much work as filling during scan conversion,
and therefore is not worthwhile for primitives that are encountered and scan-converted only once. It pays off however for primitives that would otherwise be scan-converted repeatedly. Fixed size fonts, ... Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
64
7. Thick Primitives Conceptually, you produce thick primitives by tracing the scan-converted single pixel outline primitive. But there are a lot of questions: What shape is the brush, If non-circular, what is the orientation, … Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
65
There are four basic methods for drawing thick primitives
The first method uses more than 1 pixel for each column or row during scan conversion. The second traces the pen’s cross section along the single pixel outline of the primitive. The third draws two copies of the primitive a thickness t apart and fills in the space. The fourth approximates all primitives by polylines and then uses a thick line for each polyline segment. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
66
7.1 Replacing Primitives A quick extension to the scan-conversion inner loop to write multiple pixels at each computed pixel works reasonably well for lines; Here pixels are duplicated in columns for lines with -1<slope<1 and in rows for all other lines. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
67
Problems: The line ends are always horizontal or vertical, which is not pleasing for rather thick lines. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
68
Problems (cont.) The pixel replication algorithm also produces noticeable gaps in places where line segments meet at an angle, and misses pixels where there is a shift from horizontal to vertical replication Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
69
Problems (cont.) The thickness of lines varies by slope (another aliasing problem) if the thickness is horizontal or vertically determined. Even Thicknesses is also a problem Altogether pixel replication is an efficient but crude approximation that works best for primitives that are not very thick. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
70
7.2 The Moving Pen Choosing a rectangular pen whose center or corner travels along the line works reasonably well for lines. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
71
Problems: The problems here are different. The line is now thicker at the angle than it is on the horizontal. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
72
8. Clipping in a Raster World
As noted earlier, clipping and scan-conversion need to be as fast as possible. Clipping can be done: Analytically, On the fly during scan-conversion, or as part of CopyPixel Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
73
9. Clipping Lines This section treats analytic clipping of lines against rectangles; algorithms for clipping other primitives are handled in future sections Although there are specialized algorithms for rectangle and polygon clipping, it is important to note that primitives built out of lines can be clipped by repeated application of the line clipper. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
74
Lines intersecting a rectangular clip region are always clipped into a single line segment
Here are several examples of clipped lines: Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
75
9.1 Clipping Endpoints Before we discuss clipping lines, let us look at the simpler problem of clipping points. For a point to be inside, four inequalities must be satisfied: Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
76
9.2 Clipping Lines by Solving Simultaneous Equations
To clip a line, we need only consider its endpoints. If both endpoints lie inside the clip rectangle, the line can be trivially accepted (AB) If one is inside and one outside (CD), the line intersects, and we must compute the intersection point. If both are outside, it may or may not intersect the clip rectangle. We have further work to do. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
77
The brute force method:
Find the intersection points for the line with each of the four clip-rectangle edges. For each line and edge, we take the two lines and intersect them then we test whether the intersection is interior G’ and H’ are, I’ and J’ are not. Altogether, the brute-force approach involves considerable calculation and testing; it is thus inefficient. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
78
9.3 The Cohen-Sutherland Line-Clipping Algorithm
The more efficient Cohen-Sutherland algorithm performs initial tests on a line to determine whether intersection calculations can be avoided. First, endpoints pairs are checked for trivial acceptance. If they cannot be trivially accepted, region checks are then done. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
79
For instance, two simple comparisons on x show that both endpoints of line EF have an x coordinate less than xmin and thus lie outside of the clip rectangle therefore EF is trivially rejected. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
80
If a line segment cannot be trivially accepted or trivially rejected, it is divided into two segments at a clip edge so one segment can be trivially dealt with. See edge IJ Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
81
To perform the tests, we extend the edges of the clip rectangle to divide the plane into 9 regions.
These segments help the processing of trivial rejects or accepts. If triviality is not possible, the line is segmented. And the highest bit tells which rectangle edge to intersect the line with with. (edge EI) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
82
9.4 A Parametric Line-Clipping Algorithm
Intro: The Cohen-Sutherland algorithm is probably still the most commonly used line-clipping algorithm because it has been around the longest and has been widely published. In 1978, Cyrus and Beck published an algorithm that takes a fundamentally different and generally more efficient approach to line clipping. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
83
Parametric Representation of a line
P(t) = P0+t(P1-P0), t has values in [0,1] t = 0, P(t) == P0 t = 1, P(t) == P1 This is a directed line segment Parametric vs Slope Intercept equations Slope Intercept describe infinite lines Parametric describe segments (finite lines) Slope Intercept cannot deal with vertical lines. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
84
Cohen-Sutherland is efficient when
testing is cheap (bit testing in assembler) trivial acceptance or rejection applies to the majority of the lines. Parametric line clipping wins when: many line segments need to be clipped, since the actual calculation can be postponed. Improvements to both these algorithms are discussed in the literature Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
85
10. Clipping Circles First you can do a test on the circle’s extent (the square containing the circle) If scan conversion is fast, or the circle is not too large, it is probably more efficient to scissor on a pixel-by-pixel basis. If the circle is filled, spans can be clipped individually and then filled. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
86
11. Clipping Polygons An algorithm that clips polygons must deal with many different cases. Note: (a) yields two polygons from one. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
87
11.1 The Sutherland-Hodgman Polygon-Clipping Algorithm
Sutherland and Hodgman’s polygon clipping algorithm uses a divide and conquer strategy. It solves a series of simple and identical problems that, when combined, solve the overall problem. The simple problem is to clip a polygon against a single infinite edge. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
88
The four clip edges, each defining one boundary of the clip rectangle, successively clip a polygon against the clip rectangle. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
89
Notice the differences between this algorithm and the Cohen-Sutherland line clipping algorithm.
In this algorithm, the polygon is clipped against the four lines in succession. In the Line Algorithm, the clipper tested to see which edge was crossed and clipped only when necessary. This algorithm is actually generalizable to allow a polygon to be clipped any convex polyhedral volume. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
90
12. Generating Characters
There are two basic techniques for defining characters. Define each character as a curved or polygonal outline and scan convert it as needed. Most general, but computationally expensive. Define each font as a small rectangular bitmap. Generating simply entails copying the pixels from a font cache into the frame buffer. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
91
14. Antialiasing The primitives drawn so far have a common problem:
They have jagged edges. This undesirable effect known as the jaggies or staircasing, is the result of an all-or-nothing approach to scan conversion in which each pixel is either replaced with the primitives color or is left unchanged. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
92
Jaggies are an instance of a phenomenon known as aliasing.
The application of techniques that reduce or eliminate aliasing is referred to as antialiasing. Chapter 14 discusses basic ideas from signal processing that explain how aliasing got its name, why it occurs, and how to reduce or eliminate it when creating pictures. Here, we content ourselves with a more intuitive explanation of why the primitives we have produced exhibit aliasing and how to generate antialiased lines. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
93
14.1 Increasing Resolution
Consider the midpoint algorithm to draw a 1-pixel thick line. In each column the algorithm sets the color of the pixel closest to the line to black. Each time the line moves between columns in which the pixels closest to the line are not in the same row, there is a sharp jag in the line. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
94
Suppose we now use a display with twice the horizontal and vertical resolution.
The line passes through twice as many columns and therefore has twice as many jags, But each jag is half as large in x and in y. Although the resulting picture looks better, the improvement comes at a cost of quadrupling the memory cost, memory bandwidth, and scan-conversion time. Increasing the resolution only diminishes the problem, it does not eliminate it. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
95
14.2 Unweighted Area Sampling
The first approach to improving picture quality can be developed by recognizing that, although an ideal primitive such as a line has zero width, the primitive we drawing has nonzero width. A scan-converted area occupies a finite area on the screen. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
96
Thus, we think of any line as a rectangle of a desired thickness covering a portion of the grid of pixels. It follows that a line should not set the intensity of only a single pixel in a column to black, but rather should contribute some amount of intensity to each pixel in the columns whose area it intersects. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
97
But what is the geometry of a pixel? How large is it?….
It is computationally simple to assume that pixels form an array of non-overlapping square tiles covering the screen, centered on grid points, rather than disjoint circles as discussed earlier. We also assume that a line contributes to each pixel’s intensity an amount proportional to the percentage of the pixel’s tile it covers. Fully covered is black,… Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
98
This technique, as applied to the line shown previously is shown below:
This blurring makes the line look better at a distance, despite the fact that it spreads the on-off transitions over multiple pixels in a column or row. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
99
The properties of unweighted area sampling are:
We call this technique of setting the intensity proportional to the amount of the area covered unweighted area sampling. The properties of unweighted area sampling are: First, the intensity of the pixel intersected by a line edge decreases as the distance between the pixel center and the edge increases. Second, the primitive cannot influence the pixel at all if it does not intersect the pixel. Third, equal areas contribute equal intensity Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
100
14.3 Weighted Area Sampling
In weighted area sampling, we keep the first and second properties of unweighted area sampling, but we alter the third. We let equal areas contribute unequally. Chapter 14 discusses Weighted Area Sampling in the context of filtering theory. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
101
To explain the difference between weighted and unweighted sampling, let us look at one simple example: Traditional unweighted sampling Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
102
Traditional weighted sampling
Note the change from squares to circles (that overlap) Note also that the weight is centered at the center of the pixel. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
103
15. Advanced Topics We have only touched on the concepts of clipping and scan conversion in this chapter. In practice, many complex situations arise that require the applications of advanced approaches. These are fully discussed in Computer Graphics: Principles and Practice by Foley, vanDam, Feiner and Hughes (1990) Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
104
Summary In this chapter, we have taken our first look at the fundamental clipping and scan-conversion algorithms that are the meat and potatoes of raster graphics packages. The most important idea of this chapter is that, since speed is essential in interactive raster graphics, incremental scan-conversion algorithms using only integer operations in their inner loops are the best. Chapter 3 -- Basic Raster Graphics Algorithms for Drawing 2D Primitives
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.