Beruflich Dokumente
Kultur Dokumente
Motivation:
The study of polygons is a large sub-field of computer graphics and geometric modeling. Different representations of polygons are used for different applications and goals. The varieties of operations performed on polygons are used to transmit polygons over a network and as an image manipulation tool.
4.2. Syllabus:
Module 1. 2. 3. 4. 5. 6. 7 8 Content Introduction, Representation of polygon Inside Outside test, Winding number method, Coherence Inside-outside test Polygon filling methods Boundary fill Flood fill Scan line polygon Fill Patterns filling Scan conversion of characters, Anti aliasing Types of anti aliasing, Haftoning, thresholding and dithering Duration 1 lecture 1 lecture 1 lecture 1 lecture 1 lecture 1 lecture 1 lecture Self Study Time 2 hours 2 hours 2 hours 2hours 4 hours 2 hours 2 hours 2 hours
4.5. Objective:
Understand the need of polygons. To know polygon filling method Understand scan conversion of characters.
4.6. Learning
At the end of this chapter, the students will be able to learn Polygons Filling polygons Visibility of points inside polygons Scan conversion of characters Anti-aliasing &, halftoning & dithering.
4.7. Abbreviations:
(1) 2D- two dimensional
Figure 2.1. An example of a convex polygon: a regular pentagon A simple polygon is strictly convex if every internal angle is strictly less than 180 degrees. Equivalently, a polygon is strictly convex if every line segment between two nonadjacent vertices of the polygon is strictly interior to the polygon except at its endpoints. All regular polygons are also convex polygons. Any line drawn between points that lie inside the polygon will also lie inside the polygon. b. Concave polygons:
A polygon that is not convex is called concave or reentrant A concave polygon will always have an interior angle with a measure that is greater than 180 degrees.
Figure 2.2. An example of a concave polygon. It is possible to cut a concave polygon into a set of convex polygons
The only trick is what happens in the special cases when an edge or vertex of the polygon lies on the ray from (xp,yp). The possible situations are illustrated below. 4
The thick lines above are not considered as valid intersections, the thin lines do count as intersections. Ignoring the case of an edge lying along the ray or an edge ending on the ray ensures that the endpoints are only counted once. To determine this we are generally using two methods. Odd-Even Rule Winding number method
a. Odd-Even Rule The odd-even rule classifies each point not on one of the edges as either interior or exterior. From any point shoot a straight line to infinity, not passing any vertices of the polygon Calculate the number of edges crossed, N. If N is odd then the point is interior. If N is even then the point is exterior. The scan-line algorithm works according to this rule. b. Winding number method Counts the number of times the object winds around a given point in the counter-clockwise direction From the given point again draw a straight line to infinity. Initialize winding number w = 0. For each edge crossing the line from right to left add one: w -> w + 1 For each edge crossing the line from left to right subtract 1: w -> w 1 If w! = 0 then the point is an interior point, otherwise, it is exterior. May give results different from the odd even rule! A practical way of calculating the winding number w is to examine the sign of the z component of the vector product between a vector u along the line and the edge EAB = VB VA: sign( u x EAB )
a. Boundary Fill Algorithm: A Boundary fill algorithm starts at a point inside a region (seed pixel) and point that interior portion towards the boundary. If the boundary is specified in a single column the algorithm proceeds outwards pixel by pixel until the boundary color is encountered. A boundary fill algorithm accepts 1. Coordinates of a interior point (X, Y) 2. A fill color 3. A boundary color Starting from point (X,Y) the algo tests the neighboring pixel to determine if they are of the boundary color if not they are painted with fill color. And their neighbors are tested. The processes continuous until all pixels up to the boundary color for areas are tested. Neighboring pixels are tested using either 4 or 8 connected method as shown below. 1. 4-connected region: From a given pixel, the region that you can get to by a series of 4 way moves (N, S, E and W).The neighbouring 4 pixel positions are tested. If the selected pixel is (x, y) the neighbouring pixels are (x+1, y) , (x-1, y) ,(x, y+1) , (x, y1). 4-connected fill is faster, but can have problems. T L B Figure 2.3. 4-connected Recursive method for 4-connected void BoundaryFill4(int x, int y, color newcolor, color edgecolor) { int current; current = ReadPixel(x, y); if(current != edgecolor && current != newcolor) { BoundaryFill4(x+1, y, newcolor, edgecolor); BoundaryFill4(x-1, y, newcolor, edgecolor); BoundaryFill4(x, y+1, newcolor, edgecolor); BoundaryFill4(x, y-1, newcolor, edgecolor); } } 6 R
This leads to the Eight-connected fill algorithm where we test all eight adjacent pixels. 2. 8-connected region From a given pixel, the region that you can get to by a series of 8 way moves (N, S, E, W, NE, NW, SE, and SW), the 4 diagonal pixels are also included. If the selected pixel is (x, y) the 8 neighbouring pixels are (x+1, y) ,(x-1, y), (x, y-1), (x, y+1) (x+1, y+1) ,(x-1, y+1) (x-1, y-1), (x+1, y1) In Some cases, an 8-connected region is more accurate than the 4connected region V-Vertical; D- Diagonal; H-Horizontal D H D V L V D H D
Limitations of Boundary fill algorithm. This algorithm may not fill regions correctly is some interior pixels are already displayed in the fill color. Because algorithm checks foe next pixel both boundary as well as fill color. If pixel with fill color encounter procedure terminates leaving together interior pixel unfilled. Boundary fill algorithm requires considerable amount of stack. Thats disadvantage. 7
b) Flood-Fill Algorithm The user specifies an interior color to be replaced by fill color (use 4 or 8 fill method). The process stops when no adjacent pixels are of interior color, e.g., replace white with blue.
But, even worse for overlapping polygons same result as before, but no cure. In general the Scan Conversion method is the most versatile.
A standard output primitive in general graphics package is a solid color or patterned polygon area: 1. There are two basic approaches to filling on raster systems. 2. Determine overlap Intervals for scan lines that cross that area. 3. Start from a given interior point and paint outward from this point until we encounter the boundary The first approach is mostly used in general graphics packages, however second approach is used in applications having complex boundaries and interactive painting systems Xk+1 , yk+1 Scan Line yk +1 Scan Line yk Xk , yk
For each scan lines crossing a polygon are then sorted from left to right, and the corresponding frame buffer positions between each intersection pair are set to the specified color. 8
Figure 2.5. Interior pixels along a scan line passing through a polygon area These intersection points are then sorted from left to right, and the corresponding frame buffer positions between each intersection pair are set to specified color. The basic scan-line algorithm is as follows: 1. Find the intersections of the scan line with all edges of the polygon 2. Sort the intersections by increasing x coordinate 3. Fill in all pixels between pairs of intersections that lie interior to the polygon i) Scan converting convex polygons A problem in scan converting ac general n-sided convex polygon is that a given scan line will not necessarily cross all sides of a polygon. For a triangle there was only one side not intersected so a simple test was sufficient. If there are many sides it becomes inefficient to test against all sides. So we keep a list of "active" edges, i.e., edges that are crossed by the current scan line. This list is kept by having a list of edges, sorted by Ytop value, and using two pointers, one to the first active edge and the second to the last active edge.
Here is an example of the changing active edge list as a 5-sided polygon is scan converted. Sorted list of edges (by Ytop) List 1 List 2 9
CB is the pointer to top of active edges CD is the pointer to bottom of active edges DE BA EA
CB CD DE is the pointer to top of active edges BA is the pointer to bottom of active edges EA
List 3 CB CD DE BA is the pointer to top of active edges EA is the pointer to bottom of active edges So a modified Top Level Algorithm: 1. 2. 3. 4. 5. Sort sides and create Edge List (Draw horizontal edges) Repeat Update active edge pointers (Top Bottom) Find x-intersections Draw-lines
ii) Scan Converting Concave Polygons 1. The scan line may intersect more than 2 times and therefore we must sort the x intersections.
10
2. Vertices: we want to fill from 1 to 2 and from 3 to 4, so have vertex count as 2 intersections then each pair of lines is in polygon interior.
But what about this case? An odd number of intersections and not all pairs are in interior, e.g., (3 - 4).
Solution: generate 2 intersections only when at a local min or max, else generate only 1 intersection. How to generate only 1 intersection at some vertices? Case 1: Check to see if the y coordinate is monotonically decreasing, if yes, then increase y by 1.
11
Case 2: Check to see if the y coordinate is monotonically increasing, if yes, then decrease y by 1.
In both of the above cases, the vertices will still be plotted but will only be counted once.
We store the pattern in an N x M array (e.g. 8 x 8), then we want: row 0 of the pattern to be in rows 0, 8, 16, etc. of the polygon. row 1 of the pattern to be in rows 1, 9, 17, etc. of the polygon.
12
Similarly, column 0 of the pattern will be in column 0, 8, 16, etc. of the polygon, column 1 of the pattern will be in column 1, 9, 17, etc. of the polygon. So, we will have a row pointer and a column pointer: for each scan line (Y): rowptr <-- y mod 8 for each column (X) colptr <-- x mod 8 Then to display the pixel, we use set_pixel(x, y, pattern[rowptr, colptr]).
6. ______ transformation do not change the shape of the object a) Shear b) Reflection c) Translation d) Scaling Fill in the blanks: 7. A ________ transformation alters the size of an object. 8. A 2D transformation system uses only ______ coordinates. 9. A transformation that maps each point onto itself is called an ___transformation. 13
10.
Answers. 1) b 7) Scaling
2) b 8) planar
6) a
to
(x|,y|).
The
8. What is rotation? Ans. A 2-D rotation is done by repositioning the coordinates along a circular path, in the x-y plane by making an angle with the axes. The transformation is given by: X| = r cos (q + f) and Y| = r sin (q + f). 9. What is shearing? Ans. The shearing transformation actually slants the object along the X direction or the Y direction as required.ie; this transformation slants the shape of an object along a required plane. 10. Define polygon? Distinguish between convex and concave polygons? Ans. A polygon is any closed continues sequence of line segments ie, a polyline whose last node point is same as that of its first node point. The line segments form the sides of the polygon and their intersecting points form the vertices of the polygon. If the line joining any two points in the polygon lies completely inside the polygon then, they are known as convex polygons. If the line joining any two points in the polygon lies outside the polygon then, they are known as concave polygons. 11. What is seed fill? Ans. One way to fill a polygon is to start from a given point (seed) known to be inside the polygon and highlight outward from this point i.e neighboring pixels until encounter the boundary pixels, this approach is called seed fill. 12. What is scan line algorithm? Ans. One way to fill the polygon is to apply the inside test. i.e to check whether the pixel is inside the polygon or outside the polygon and then highlight the pixel which lie inside the polygon. This approach is known as scan-line algorithm. 13. Define coherence properties? Ans. A coherence property of a scene is apart of a scene by which relate one part of the scene with the other parts of the scene. 14. What is a winding number? Ans.Winding number method is used to check whether a given point is inside or out side the polygon. In this method give a direction number to all the edges which cross the scan line. If the edge starts below the line and ends above scan line give direction as -1 . otherwise1. Fr polygons or 15
two dimensional objects, the point is said to be inside when the value of winding number is nonzero.
16
16