Download presentation

Presentation is loading. Please wait.

Published bySusana Diver Modified about 1 year ago

1
1 COMPUTER GRAPHICS CHAPTER 3 2D GRAPHICS ALGORITHMS

2
2 2D Graphics Algorithms Output Primitives Output Primitives Line Drawing Algorithms Line Drawing Algorithms DDA Algorithm DDA Algorithm Midpoint Algorithm Midpoint Algorithm Bersenhem’s Algorithm Bersenhem’s Algorithm Circle Drawing Algorithms Circle Drawing Algorithms Midpoint Circle Algorithm Midpoint Circle Algorithm Antialising Antialising Fill-Area Algorithms Fill-Area Algorithms

3
3 Output Primitives

4
4 The basic objects out of which a graphics display is created are called. The basic objects out of which a graphics display is created are called. Describes the geometry of objects and – typically referred to as geometric primitives. Describes the geometry of objects and – typically referred to as geometric primitives. Examples: point, line, text, filled region, images, quadric surfaces, spline curves Examples: point, line, text, filled region, images, quadric surfaces, spline curves Each of the output primitives has its own set of attributes. Each of the output primitives has its own set of attributes.

5
Output Primitives Points Points Attributes: Size, Color. Attributes: Size, Color. glPointSize(p); glBegin(GL_POINTS); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glEnd()

6
Output Primitives Lines Lines Attributes: Color, Thickness, Type Attributes: Color, Thickness, Type glLineWidth(p); glBegin(GL_LINES); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()

7
Output Primitives Polylines (open) Polylines (open) A set of line segments joined end to end. A set of line segments joined end to end. Attributes: Color, Thickness, Type Attributes: Color, Thickness, Type glLineWidth(p); glBegin(GL_LINE_STRIP); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()

8
Output Primitives Polylines (closed) Polylines (closed) A polyline with the last point connected to the first point. A polyline with the last point connected to the first point. Attributes: Color, Thickness, Type Attributes: Color, Thickness, Type Note: A closed polyline cannot be filled. glBegin(GL_LINE_LOOP); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()

9
Output Primitives Polygons Polygons A set of line segments joined end to end. A set of line segments joined end to end. Attributes: Fill color, Thickness, Fill pattern Attributes: Fill color, Thickness, Fill pattern Note: Polygons can be filled. glBegin(GL_POLYGON); glVertex2d(x1, y1); glVertex2d(x2, y2); glVertex2d(x3, y3); glVertex2d(x4, y4); glEnd()

10
10 Output Primitives Text Text Attributes: Font, Color, Size, Spacing, Orientation. Attributes: Font, Color, Size, Spacing, Orientation. Font: Font: Type (Helvetica, Times, Courier etc.) Type (Helvetica, Times, Courier etc.) Size (10 pt, 14 pt etc.) Size (10 pt, 14 pt etc.) Style (Bold, Italic, Underlined) Style (Bold, Italic, Underlined)

11
11 Output Primitives Images Images Attributes: Image Size, Image Type, Color Depth. Attributes: Image Size, Image Type, Color Depth. Image Type: Image Type: Binary (only two levels) Binary (only two levels) Monochrome Monochrome Color. Color. Color Depth: Color Depth: Number of bits used to represent color.

12
TCS Output Primitives Output PrimitiveAttributes PointSize Color Line Thickness (1pt, 2pt … ) Type (Dashed, Dotted, Solid) Color Text Font (Arial, Courier, Times Roman … ) Size (12pt, 16pt..) Spacing Orientation (Slant angle) Style (Bold, Underlined, Double lined) Color Filled RegionFill Pattern Fill Type (Solid Fill, Gradient Fill) Fill Color ImagesColor Depth (Number of bits/pixel)

13
13 Line Drawing Algorithms

14
Line Drawing Line drawing is fundamental to computer graphics. We must have fast and efficient line drawing functions. Rasterization Problem: Given only the two end points, how to compute the intermediate pixels, so that the set of pixels closely approximate the ideal line.

15
Line Drawing - Analytical Method y x y=mx+c axax bxbx A(a x,a y ) B(b x,b y )

16
Directly based on the analytical equation of a line. Involves floating point multiplication and addition Requires round-off function. double m = (double)(by-ay)/(bx-ax); double c = ay - m*ax; double y; int iy; for (int x=ax ; x <=bx ; x++) { y = m*x + c; iy = round(y); setPixel (x, iy); } Line Drawing - Analytical Method

17
Compute one point based on the previous point: (x 0, y 0 )…….…………..(x k, y k ) (x k+1, y k+1 ) ……. I have got a pixel on the line (Current Pixel). How do I get the next pixel on the line? Next pixel on next column (when slope is small) Next pixel on next row (when slope is large) Incremental Algorithms

18
Current Pixel (x k, y k ) To find (x k+1, y k+! ): x k+1 = x k +1 y k+1 = ? (5,2) (6,1) (6,2) (6,3) Assumes that the next pixel to be set is on the next column of pixels (Incrementing the value of x !) Not valid if slope of the line is large. Incrementing along x

19
Digital Differential Analyzer Algorithm is an incremental algorithm. Assumption: Slope is less than 1 (Increment along x). Current Pixel = (x k, y k ). (x k, y k ) lies on the given line. y k = m.x k + c Next pixel is on next column.x k+1 = x k +1 Next point (x k+1, y k+1 ) on the line y k+1 = m.x k+1 + c = m (x k +1) +c = y k + m Given a point (x k, y k ) on a line, the next point is given by x k+1 = x k +1 y k+1 = y k + m Line Drawing - DDA

20
Does not involve any floating point multiplication. Involves floating point addition. Requires round-off function Line Drawing - DDA double m = (double) (by-ay)/(bx-ax); double y = ay; int iy; for (int x=ax ; x <=bx ; x++) { iy = round(y); setPixel (x, iy); y+ = m; }

21
x k+1 = x k +1 y k+1 = Either y k or y k +1 Midpoint algorithm is an incremental algorithm Midpoint Algorithm Assumption: Slope < 1 Current Pixel

22
Candidate Pixels Current Pixel ( x k, y k ) Midpoint Line Coordinates of Midpoint = ( x k +1, y k +(1/2) ) ( x k +1, y k ) ( x k +1, y k +1) Midpoint Algorithm - Notations

23
Midpoint Below LineMidpoint Above Line Midpoint Algorithm: Choice of the next pixel If the midpoint is below the line, then the next pixel is (x k +1, y k +1). If the midpoint is above the line, then the next pixel is (x k +1, y k ).

24
A(a x,a y ) B(b x,b y ) Equation of a line revisited. Let w = b x a x, and h = b y a y. Then,h (x a x ) w (y a y ) = 0. (h, w, a x, a y are all integers). In other words, every point (x, y) on the line satisfies the equation F(x, y) =0, where F(x, y) = h (x a x ) w (y a y ). Equation of the line:

25
Midpoint Algorithm: Regions below and above the line. F (x,y) > 0 (for any point below line) F(x,y) < 0 (for any point above line) F(x,y) = 0

26
F(MP) > 0 Midpoint below line F(MP) < 0 Midpoint above line Midpoint Algorithm Decision Criteria

27
F(MP) = F(x k +1, y k + ½) = F k (Notation) If F k < 0 : The midpoint is above the line. So the next pixel is (x k +1, y k ). If F k 0 : The midpoint is below or on the line. So the next pixel is (x k +1, y k +1). Decision Parameter

28
Midpoint Algorithm – Story so far. Midpoint Below Line Next pixel = (x k +1, y k +1) F k > 0 y k+1 = y k +1 Midpoint Above Line Next pixel = (x k +1, y k ) F k < 0 y k+1 = y k

29
Midpoint Algorithm Update Equation F k = F(x k +1, y k + ½) = h (x k +1 a x ) w (y k +½ a y ) But, F k+1 = F k + h w (y k+1 y k ). (Refer notes) So, F k < 0 : y k+1 = y k.Hence, F k+1 = F k + h. F k 0 : y k+1 = y k +1.Hence, F k+1 = F k + h w. F 0 = h w/2. Update Equation

30
30 Midpoint Algorithm int h = by-ay; int w = bx-ax; float F=h-w/2; int x=ax, y=ay; for (x=ax; x<=bx; x++){ setPixel(x, y); if(F < 0) F+ = h; else{ F+ = h-w; y++; }

31
31 Bresenham’s Algorithm int h = by-ay; int w = bx-ax; int F=2*h-w; int x=ax, y=ay; for (x=ax; x<=bx; x++){ setPixel(x, y); if(F < 0) F+ = 2*h; else{ F+ = 2*(h-w); y++; }

32
32 Circle Drawing Algorithms

33
33 To determine the closest pixel position to the specified circle path at each step. 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 coodinate origin (0,0). For given radius r and screen center position (x c, y c ), calculate pixel positions around a circle path centered at the coodinate 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. Then, move each calculated position (x, y) to its proper screen position by adding x c to x and y c to y. Along the circle section from x=0 to x=y in the first quadrant, the gradient varies from 0 to -1. Along the circle section from x=0 to x=y in the first quadrant, the gradient varies from 0 to -1. Midpoint Circle Drawing Algorithm

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

35
TCS 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) =

36
TCS Midpoint Circle Drawing Algorithm ykyk y k -1 midpoint Next pixel = (x k +1, y k ) F k < 0 y k+1 = y k ykyk y k -1 midpoint Next pixel = (x k +1, y k -1) F k >= 0 y k+1 = y k - 1

37
TCS Midpoint Circle Drawing Algorithm We know x k+1 = x k +1, F k = F(x k +1, y k - ½) F k = (x k +1) 2 + (y k - ½) 2 - r (1) F k+1 = F(x k +1, y k - ½) F k+1 = (x k +2) 2 + (y k+1 - ½) 2 - r (2) (2) – (1) F k+1 = F k + 2(x k +1) + (y 2 k+1 – y 2 k ) - (y k+1 – y k ) + 1 If F k < 0, F k+1 = F k + 2x k+1 +1 If F k >= 0, F k+1 = F k + 2x k+1 +1 – 2y k+1

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

39
TCS 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: f 0 = 5 – r 4 = 5 – 10 4 = ≈ –9

40
TCS Midpoint Circle Drawing Algorithm Initial (x 0, y 0 ) = (1,10) Decision parameters are: 2x 0 = 2, 2y 0 = 20 kFkFk xy2x k+1 2y k = = = = = =57714

41
TCS Midpoint Circle Drawing Algorithm void circleMidpoint (int xCenter, int yCenter, int radius) { int x = 0; Int y = radius; int f = 1 – radius; circlePlotPoints(xCenter, yCenter, x, y); while (x < y) { x++; if (f < 0) f += 2*x+1; else { y--; f += 2*(x-y)+1; } } circlePlotPoints(xCenter, yCenter, x, y); }

42
TCS Midpoint Circle Drawing Algorithm void circlePlotPoints (int xCenter, int yCenter, int x, int y) { setPixel (xCenter + x, yCenter + y); setPixel (xCenter – x, yCenter + y); setPixel (xCenter + x, yCenter – y); setPixel (xCenter – x, yCenter – y); setPixel (xCenter + y, yCenter + x); setPixel (xCenter – y, yCenter + x); setPixel (xCenter + y, yCenter – x); setPixel (xCenter – y, yCenter – x); }

43
43 Antialiasing

44
44 Antialiasing Antialiasing is a technique used to diminish the jagged edges of an image or a line, so that the line appears to be smoother; by changing the pixels around the edges to intermediate colors or gray scales. Eg. Antialiasing disabled: Eg. Antialiasing enabled:

45
Antialiasing (OpenGL) Antialiasing disabledAntialiasing enabled Setting antialiasing option for lines: glEnable (GL_LINE_SMOOTH);

46
46 Fill Area Algorithms

47
47 Fill Area Algorithms Fill-Area algorithms are used to fill the interior of a polygonal shape. 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. Many algorithms perform fill operations by first identifying the interior points, given the polygon boundary.

48
48 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

49
49 [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

50
50 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)

51
51 Requires an interior point. Requires an interior point. Involves considerable amount of stack operations. Involves considerable amount of stack operations. The boundary has to be closed. The boundary has to be closed. Not suitable for self-intersecting polygons Not suitable for self-intersecting polygons Basic Filling Algorithm

52
52 Boundary Fill Algorithm Boundary Fill Algorithm For filling a region with a single boundary color. For filling a region with a single boundary color. Condition for setting pixels: Condition for setting pixels: Color is not the same as border color Color is not the same as border color Color is not the same as fill color Color is not the same as fill color Flood Fill Algorithm Flood Fill Algorithm For filling a region with multiple boundary colors. For filling a region with multiple boundary colors. Condition for setting pixels: Condition for setting pixels: Color is same as the old interior color Color is same as the old interior color Types of Basic Filling Algorithms

53
TCS 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)

54
TCS void floodFill(int x, int y, int fillColor, int oldColor) { getPixel(x, y, color); if (color != oldColor) { setPixel(x,y); floodFill(x+1, y, fillColor, oldColor); floodFill(x-1, y, fillColor, oldColor); floodFill(x, y+1, fillColor, oldColor); floodFill(x, y-1, fillColor, oldColor); } Flood Fill Algorithm (Code)

55
Filling Polygons (OpenGL) Enabling polygon fill (Default): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); Disabling polygon fill: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google