Computer graphics

nanhen1 14,853 views 34 slides Mar 30, 2013
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

No description available for this slideshow.


Slide Content

Seminar On Computer Graphics TOPICS: Mid-point Circle Algorithm, Filling Algorithms & Polygons. 1/2 - 1

Mid-point Circle Algorithm

To determine the closest pixel position to the specified circle path at each step. For given radius r and screen center position (x c , y c ), calculate pixel positions around a circle path centered at the coordinate origin (0,0). Then, move each calculated position (x, y) to its proper screen position by adding x c to x and y c to y . 3 Midpoint Circle Drawing Algorithm

4 Midpoint Circle Drawing Algorithm 8 segments of octants for a circle:

5 Midpoint Circle Drawing Algorithm Circle function: f circle ( x,y ) = x 2 + y 2 –r 2 > 0, ( x,y ) outside the circle < 0, ( x,y ) inside the circle = 0, ( x,y ) is on the circle boundary { f circle (x,y) =

y k 6 Midpoint Circle Drawing Algorithm Next pixel = (x k +1, y k ) P k < 0 y k+1 = y k Next pixel = (x k +1, y k -1) P k >= 0 y k+1 = y k - 1 x k X k+1 X k+2 y k X k X k+1 X k+2 y k-1 y k-1 midpoint midpoint

Midpoint Circle Drawing Algorithm We know x k+1 = x k +1, P k = f circle (x k +1 , y k - ½) P k = ( x k +1) 2 + ( y k - ½) 2 - r 2 -------- (1) P k+1 = f circle (x k+1 +1 , y k+1 - ½) P k+1 = [( x k +1)+1] 2 + (y k+1 - ½) 2 - r 2 -------- (2) 6

8 By subtracting eq . 1 from eq.2, we get P k+1 – P k = 2(x k +1 ) + (y 2 k+1 – y 2 k ) - (y k+1 – y k ) + 1 If P k < 0, P k+1 = P k + 2x k+1 +1 If P k >= 0, P k+1 = P k + 2x k+1 +1 – 2y k+1 P k+1 = P k + 2(x k +1) + (y 2 k+1 – y 2 k ) - (y k+1 – y k ) + 1

9 Midpoint Circle Drawing Algorithm For the initial point, (x , y ) = (0, r) p = f circle (1, r- ½ ) = 1 + (r- ½ ) 2 – r 2 = 5 – r 4 ≈ 1 – r

Taking input radius ‘r’ and circle center ( x c , y c ), we obtain the first point on the circumference of a circle centered on the origin as: ( x , y ) = (0,r ) Next we will calculate the initial value of the decision parameter as p = 5/4-r At each x position, starting at k=0 , perform the following test: if p<0 the next point along the circle centered on (0,0) is ( x k+1 , y k ) and p k+1 = p k + 2 x k+1 +1 10 Algorithm:

Otherwise , the next point along the circle is (x k +1, y k -1) and p k+1 = p k +2 x k+1 +1+ 2 y k+1 where, 2 x k+1 =2 x k +2 and 2 y k+1 = 2 y k -2. After this we will determine the symmetric points in the other 7 octants . At Move each calculated pixel position ( x,y ) on to the circle path centered on ( x c , y c ) and plot the coordinate values : x= x+ x c y= y+ y c Repeat step 3 through 5 until x>=y 11

12 Midpoint Circle Drawing Algorithm Example: Given a circle radius = 10, determine the circle octant in the first quadrant from x=0 to x=y. Solution: p = 5 – r 4 = 5 – 10 4 = -8.75 ≈ –9

13 Midpoint Circle Drawing Algorithm Initial (x , y ) = (0,10 ) Decision parameters are: 2x = 0, 2y = 20 k P k x y 2x k+1 2y k+1 -9 1 10 2 20 1 -9+2+1=-6 2 10 4 20 2 -6+4+1=-1 3 10 6 20 3 -1+6+1=6 4 9 8 18 4 6+8+1-18=-3 5 9 10 18 5 -3+10+1=8 6 8 12 16 6 8+12+1-16=5 7 7 14 14

Filling Algorithms

15 Fill Area Algorithms Fill-Area algorithms are used to fill the interior of a polygonal shape. Many algorithms perform fill operations by first identifying the interior points, given the polygon boundary.

16 The basic filling algorithm is commonly used in interactive graphics packages, where the user specifies an interior point of the region to be filled. Basic Filling Algorithm 4-connected pixels

17 [1] Set the user specified point. [2] Store the four neighboring pixels in a stack. [3] Remove a pixel from the stack. [4] If the pixel is not set, Set the pixel Push its four neighboring pixels into the stack [5] Go to step 3 [6] Repeat till the stack is empty. Basic Filling Algorithm

18 void fill( int x, int y) { if( getPixel ( x,y )==0){ setPixel ( x,y ); fill(x+1,y); fill(x-1,y); fill(x,y+1); fill(x,y-1); } } Basic Filling Algorithm (Code )

19 Boundary Fill Algorithm For filling a region with a single boundary color. Condition for setting pixels: Color is not the same as border color Color is not the same as fill color Flood Fill Algorithm For filling a region with multiple boundary colors. Here we don’t have to deal with boundary color Condition for setting pixels: Coloring of the pixels of the polygon is done with the fill color until we keep getting the old interior color. Types of Basic Filling Algorithms

20 void boundaryFill ( int x, int y, int fillColor , int borderColor ) { getPixel (x, y, color); if ((color != borderColor ) && (color != fillColor )) { setPixel ( x,y ); boundaryFill (x+1,y,fillColor,borderColor); boundaryFill (x-1,y,fillColor,borderColor); boundaryFill (x,y+1,fillColor,borderColor); boundaryFill (x,y-1,fillColor,borderColor); } } Boundary Fill Algorithm (Code)

Boundary-Fill Algorithm (cont.) The 4-connected method. The 8-connected method.

Boundary-Fill Algorithm (cont.)

CS 380 Pixel Span Method

Pixel Span Method (cont.)

Polygon

Definition Polygon is a closed figure with many vertices and edges (line segments), and at each vertex exactly two edges meet and no edge crosses the other . Types of Polygon: Regular polygons No. of egdes equal to no. of angles. Convex polygons Line generated by taking two points in the polygon must lie within the polygon . Concave polygons Line generated by taking two points in the polygon may lie outside the polygon .

This test is required to identify whether a point is inside or outside the polygon. This test is mostly used for identify the points in hollow polgons . Two types of methods are there for this test: Even-Odd Method, Winding Number Method. 1/2 - 27 Polygon Inside/Outside Test

Even-Odd method: Draw a line from any position P to a distant point outside the coordinate extents of the object and counting the number of edge crossings along the scan line . If the number of polygon edges crossed by this line is odd then P is an interior point. Else P is an exterior point

29 In some situations the logic described above doesn’t hold, when a scan line passes through a vertex having two intersecting edges then if both the edges lies on the same side of the scan line the intersection is considered to be even. Otherwise the intersection is considered to be odd i.e. if the two edges are on the opposite side of the scan line.

Nonzero Winding Number Rule : Another method of finding whether a point is inside or outside of the polygon. In this every point has a winding number, and the interior points of a two-dimensional object are defined to be those that have a nonzero value for the winding number. Initializing the winding number to 0. Imagine a line drawn from any position P to a distant point beyond the coordinate extents of the object. Count the number of edges that cross the line in each direction. We add 1 to the winding number every time we intersect a polygon edge that crosses the line from right to left, and we subtract 1 every time we intersect an edge that crosses from left to right.

4 . If the winding number is nonzero , then P is defined to be an interior point Else P is taken to be an exterior point.

Scan Line Polygon Fill Algorithm Interior pixels along a scan line passing through a polygon area For each scan line crossing a polygon ,the area filling algorithm locates the intersection points of the scan line with the polygon edges. These intersection points are then sorted from left to right , and the corresponding frame buffer positions between each intersection pair are set to specified fill color 10 14 18 24

Scan Line Polygon Fill Algorithm Adjusting endpoint values for a polygon, as we process edges in order around the polygon perimeter. The edge currently being processed is indicated as a solid like. In (a), the y coordinate of the upper endpoint of the current edge id decreased by 1. In (b), the y coordinate of the upper end point of the next edge is decreased by 1 (a) (b)

34 Thank You