Sei sulla pagina 1di 16

4.1.

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.3. Weightage: 10 Marks 4.4. Prerequisite:


Knowledge of mathematical reasoning and ability to solve problems in linear algebra and (primarily differential) calculus, knowledge of Data Structures, Ability to build software composed of several interacting modules, Knowledge of vector geometry is useful, Knowledge of C/Java Programming.

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

4.8. Key Definitions:


1. Polygon: In geometry a polygon is traditionally a plane figure that is bounded by a closed path or circuit, composed of a finite sequence of straight line segments. 2. Concave polygon: a concave polygon is a simple polygon having at least one interior angle greater than 180. 3. Convex polygon: A polygon is convex if, for any two points selected inside the polygon, the line segment between them is completely contained within the polygon. 4. Polygon filling: The process of generating solid filled area using edges & vertex information is referred to as polygon filling. 5. Inside- outside tests: The basic problem to decide whether a point lies inside or outside a polygon. 6. Transformation: is the process of introducing changes in the shape size and orientation of the object using scaling rotation reflection shearing & translation etc. 7. Translation: is a process of changing the position of an object in a straight line path from one coordinate location to another 8. Shearing: is a deformation of an object in which parallel planes remain parallel but are shifted in a direction parallel to them. Shearing refers to the change in the shape. 9. Rotation: is a transformation in which coordinates axes are rotated by a fixed angle about the origin.

4.9. Theory 4.9.1 Introduction:


A polygon, even though generally constructed from straight lines, is an important graphics primitive. So often we want to handle polygon as a single entity, as images of objects from the real world consist in large, part of polygons. A polygon is a closed area of image bounded by straight or curved lines and filled with one solid color. Since images are two dimensional, a polygon is a closed planar figure. Polygons are used in computer graphics to compose images that are three-dimensional in appearance. Usually (but not always) triangular, polygons arise when an object's surface is modeled, vertices are selected, and the object is rendered in a wire frame model. This is quicker to display than a shaded model; thus the polygons are a stage in computer animation.

4.9.2 Convex and Concave polygons


a. Convex polygons: A convex polygon is a simple polygon whose interior is a convex set. The following properties of a simple polygon are all equivalent to convexity: Every internal angle is less than 180 degrees. Every line segment between two vertices remains inside or on the boundary of the polygon.

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

4.9.3 Inside/Outside test:


We can determine whether a point is inside the polygon or outside.The following is a simple solution to the problem often encountered in computer graphics, determining whether or not a point (x,y) lies inside or outside a 2D polygonally bounded plane. This is necessary for example in applications such as polygon filling on raster devices. Hatching in drafting software, and determining the intersection of multiple polygons. Consider a polygon made up of N vertices (xi,yi) where i ranges from 0 to N-1. The last vertex (xN,yN) is assumed to be the same as the first vertex (x0,y0), that is, the polygon is closed. To determine the status of a point (xp,yp) consider a horizontal ray emanating from (xp,yp) and to the right. If the number of times this ray intersects the line segments making up the polygon is even then the point is outside the polygon. Whereas if the number of intersections is odd then the point (xp,yp) lies inside the polygon. The following shows the ray for some sample points and should make the technique clear.

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 )

4.9.4. Seed fill polygon filling algorithm


5

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

There is the following problem with four_fill:

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

Figure 2.4. 8-connected

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.

4.9.5. Scan line Polygon fill algorithm

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.

4.9.6. Pattern Filling of Polygons


Rather than filling a polygon with a solid color, we may want to fill it with a pattern or texture.

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]).

4.10 MULTIPLE CHOICE QUESTIONS


1. In the polygon inside test, if the winding number of a point is zero then the point lies ___________ the polygon a) Inside b) Outside c) On d) As vertex of 2. A polygon is called ------- if the line joining any two interior points of the polygon lies completely inside the polygon (a) Concave (b)Convex (c) Positively oriented polygon (d)Negatively oriented polygon 3. A rotation is a movement of an object in a ______ motion a) Circular b) rectangular c) Parallel d) None 4. _________ is a transformation that produces a mirror image of an object. a) Scaling b) Reflection c) Translation d) both B & C 5. Scaling is a/an ______ transformation a) Non-affine b) Affine c) Both A & B d) None

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.

When the scale factor is same in all directions, it is called ______.

Answers. 1) b 7) Scaling

2) b 8) planar

3) a 4) b 5) b 9) identity 10) uniform scaling

6) a

4.11. SHORT QUESTIONS


1. List the transformation types in two-dimensional graphics. Ans. Translation, scaling, rotation, reflection and shearing. 2. Define Reflection. Ans. Reflection is a transformation that produces a mirror image of an object. 3. What is scaling? Ans. The scaling transformations changes the shape of an object and can be carried out by multiplying each vertex (x,y) by scaling factor Sx,Sy where Sx is the scaling factor of x and Sy is the scaling factor of y. 4. What is the need of homogeneous coordinates? Ans. To perform more than one transformation at a time, use homogeneous coordinates or matrixes. They reduce unwanted calculations intermediate steps saves time and memory and produce a sequence of transformations. 5. What is fixed point scaling? Ans. The location of a scaled object can be controlled by a position called the fixedpoint that is to remain unchanged after the scaling transformation. 6. What is Transformation? Ans. Transformation is the process of introducing changes in the shape size and orientation of the object using scaling rotation reflection shearing & translation etc. 7. What is translation? Ans. Translation is the process of changing the position of an object in a straight-line path from one coordinate location to another. Every point (x , 14

y) in the object must undergo a displacement transformation is: x| = x + tx ; y| = y+ty

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.

4.12 University Questions:


Q.1. Explain Flood Fill Algorithm using 8-connected approach. What are its advantages over Boundary fill algorithm [10 Marks] [Dec 2009, Jan 2010]. Q.2. Explain the winding number method. Q.3. Describe Halftoning, Dithering & thresholding. [05 Marks] [Jan 2010]. [05 Marks] [Dec 2010].

4.13. Learning Resources:


1. S. Harrington, Computer Graphics, 2nd Edition, McGraw-Hill Publications, 1987 ISBN 0-07-100472-6. 2. J. Foley, Van Dam, S. Feiner, J. Hughes, Computer Graphics Principles and Practice, 2nd Edition , Pearson Education, 2003, ISBN 81-7808-038-9 3. Leen Ammeraal, KangZRang Computer Graphics for Java Programming,2nd Edition, Wiley India 4. D. Rogers, Procedural elements for computer Graphics, 2nd Edition, TATA McGraw-Hill Publications,2001, ISBN0-07-047371-4 5. D. Hearn, M. Baker , Computer Graphics-C Version, 2nd Edition, Pearson Education, 2002, ISBN 81-7808-794-4 6. F. Hill, Computer Graphics: Using OpenGL, 2nd Edition, Pearson Education, 2003, ISBN 81-297-0181-2 7. Xiang, Plastock,Computer Graphics, 2nd Edition, TATA McGraw-Hill Publications,2002, ISBN-0-07-049958-6 8. Malay K. Pakhira, Computer Graphics Multimedia and Animation, 2nd Edition, PHI Learning Private Limited, 2010, ISBN: 978-81-203-4127-2

16

16