Shape Drawing Algorithms Shmuel Wimer Bar Ilan Univ
Shape Drawing Algorithms Shmuel Wimer Bar Ilan Univ. , Engineering Faculty March 2010 1
Display Window World –coordinate limits 5 4 Screen –coordinates 3 2 1 (4, 2) pixel coordinate 0 0 1 March 2010 2 3 4 5 2
Line Drawing Algorithms We’d like to display straight line on the screen. In analog displays, such as vector-scan, a smooth line can be displayed, as line equation is translated into deflection voltages. On raster systems, lines are plotted with pixels which have vertical and horizontal finite resolution. The outcome is a jaggy line. March 2010 3
In raster systems lines are plotted with pixels and step sizes in the horizontal and vertical directions must be sampled at nearest pixel positions. March 2010 4
March 2010 5
March 2010 6
March 2010 7
March 2010 8
March 2010 9
Circle Drawing Algorithms We could use above equation to step along the x axis from xc- r to xc+ r and calculate y positions of pixels. March 2010 10
This requires considerable amount of computation (square root) The drawing appearance is poor since the vertical spacing between drawn pixels is uneven. Appearance can be improved by changing the role of x and y whenever the absolute value of the slope of circle is crossing 1. But this requires more computation. Polar coordinates can be used with uniform stepping of θ. This spaces pixels evenly along circumference. Time consuming due to trigonometric computations. March 2010 11
We can use large angular steps and connect the points by straight lines. For smoother boundary we can use angular steps of 1/r which plots pixels approximately one pixel apart of each other (r∙ 1/r =1). Any of the methods can use symmetry for efficiency. All methods involve expensive computations. March 2010 12
March 2010 13
Successive decision parameters are obtained by incremental calculations. March 2010 14
March 2010 15
k pk (xk+1, yk+1) 2 xk+1 2 yk+1 0 -9 (1, 10) 2 20 1 -6 (2, 10) 4 20 2 -1 (3, 10) 6 20 3 6 (4, 9) 8 18 4 -3 (5, 9) 10 18 5 8 (6, 8) 12 16 6 5 (7, 7) 14 14 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 March 2010 16
Ellipse Drawing Algorithms March 2010 17
Unlike circles, symmetry exists only in quadrants. Midpoint Ellipse Algorithm is working similar to circle with a few adaptations. March 2010 18
March 2010 19
March 2010 20
Polygon Fill Areas Convex polygons are easy to fill Concave polygons are more difficult to fill Some graphics packages support convex polygon filling only. How should polygon split into convex components? We’d like to avoid any trigonometric or division operations. We’ll use vector product a x b for convex / concave test. March 2010 21
Polygon vertices are counterclockwise oriented. The cross product of successive edges is a perpendicular vector. Z component is positive for convex angle and negative for concave. Only multiplications are involved. March 2010 22
March 2010 23
Found concave followed by convex Tear off triangle The problem turns to filling of triangles March 2010 24
Inside-Outside Tests ? ? Odd-even rule: Draw “infinite” ray and count the number of crossings. Odd is inside, even outside. Avoid passing through vertices. Nonzero winding-number rule: Draw “infinite” directed ray. Traverse edges and check for crossing direction (e. g. by cross product). Add ± 1 accordingly. If it sums to zero point is outside, otherwise it is inside. March 2010 25
Polygon Tables Objects are described by sets of polygon surfacets. March 2010 26
• In hardware implementation these tables translate into physical RW memories. • For hardware implementations triangles are preferred, since it makes SURFACE-FACET MEMORY homogeneous. • Data in memories needs to be organized to maximize pipeline processing of facets: – Vertices are stored such that vertex progression in VERTEX MEMORY completes new triangle for every vertex. – Vertex indices are their memory addresses. • It is the SW application responsibility to triangulate objects and load HW memories in the right order. March 2010 27
Plane Equations • Graphics system processes input data through several steps: – Transformation of modeling through viewing pipeline. – Identification of visible surfaces. – Rendering of surfacets. • Information about the spatial orientation of surfaces is needed – Plane equations are in order. – How to calculate those efficiently w/o divisions? March 2010 28
March 2010 29
Front and Back Polygon Faces • Polygon surfaces enclose objects. – Need to know the outward an inward sides of polygon. • Need to find the side of a polygon visible by an observer. March 2010 30
Scan-Line Polygon-Fill Algorithms • This is most time consuming operation in computer graphics. – Use only integer arithmetic with minimum operations • Polygons are filled with scan-line – The sweep is vertical – A scan-line is the pixels of same y coordinates Pixels between odd-even intersections with border 1 2 3 are inside. 4 x March 2010 31
A problem may occur at vertices. Scan-line y’ is okay, but y is not. The remedy is to traverse edges prior to filling and detect the values of three consecutive of two successive edges. If they are monotonic then one of the edges is shortened by one pixel at corner Scan-line y’ 1 1 March 2010 2 1 1 Scan-line y 32
Polygon filling takes advantage of y increments by one unit. How to calculate efficiently the intersection of an edge with scan line? How to avoid division? March 2010 33
March 2010 34
March 2010 35
Antialiasing • Graphics primitives generated by raster algorithms have jagged (stair-step) appearance, called aliasing. – This is due to under sampling. – Antialiasing compensates for this. • Sampling rate could increase by higher raster resolution. – Expensive, increase of frame buffer (memory). – More processing (time, hardware). – Jagging phenomena is not canceled, but only improved. • Raster systems capable of gradual intensity of pixels can support antialiasing by modifying pixel intensity near boundary of primitives. March 2010 36
Antialiasing by Supersampling • Treat the screen as if it has finer grid than actual. • Modify real pixel intensity according to the number of covered sub-pixels There are 4 possible intensities, depending on the count of covered pixels at every 3 x 3 superpixel (1, 1) is covered by 3 subpixels (2, 2) and ((3, 2) are covered by 2 subpixels (2, 1) and ((3, 3) are covered by 1 subpixels March 2010 37
• Line was treated as zero width. • In case of finite width we count the number of subpixels inside the polygon representing the line. Useful also for mixing foreground and background colors in a pixel. For instance, if foreground is red and background is blue: March 2010 38
Antialiasing by subpixel Weighting Masks 1 2 4 2 1 Central pixel is most important and has a weight of 1/4. Others has a weight of 1/8 and 1/16. Antialiasing compensates for line-intensity differences Diagonal lines are less instance than horizontal and vertical ones. Supersampling with higher weights for subpixels close to diagonal will intensify diagonal lines March 2010 39
Antialiasing Area Boundaries Pixel intensities are adjusted along the boundary of area. Subdivide area of pixels and double scanline. Count how many subpixels are inside. Scan-line 1 Scan-line 2 75% pixel intensity March 2010 40
Percentage of pixel area within fill area by midpoint method (Pitteway & Watkinson). March 2010 41
boundary line overlap area March 2010 42
- Slides: 42