CHAPTER 3 2D GRAPHICS ALGORITHMS

Slides:



Advertisements
Similar presentations
OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop –Set.
Advertisements

COMP 175 | COMPUTER GRAPHICS Remco Chang1/6103b – Shapes Lecture 03b: Shapes COMP 175: Computer Graphics February 3, 2015.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
Line and Curve Drawing Algorithms. Line Drawing x0x0 y0y0 x end y end.
CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3.
O a polygon is a plane figure specified by a set of three or more coordinate positions, called vertices, that are connected in sequence by straight-Line.
Computer Graphics Tz-Huan Huang National Taiwan University (Slides are based on Prof. Chen’s)
OUTPUT PRIMITIVES Screen vs. World coordinate systems ● Objects positions are specified in a Cartesian coordinate system called World Coordinate.
CMPE 466 COMPUTER GRAPHICS
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
CP411 polygon Lecture 6 1. Scan conversion algorithm for circle 2. Draw polygons 3. Antialiasing.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
2IV60 Computer graphics Graphics primitives and attributes Jack van Wijk TU/e.
© 2001 By Default! A Free sample background from Slide 1 Attributes of Output Primitives Definition Parameter that affects.
CMPE 466 COMPUTER GRAPHICS Chapter 5 Attributes of Graphics Primitives Instructor: D. Arifler Material based on - Computer Graphics with OpenGL ®, Fourth.
Output Primitives Computer Graphics.
1 Lecture 4 Graphical primitives Rasterization: algorithmic approach Rasterization: geometric approach 2D discrete lines, triangles Discrete planes 3D.
CS 454 Computer graphics Polygon Filling
CS 4731: Computer Graphics Lecture 22: Raster Graphics Part 3 Emmanuel Agu.
ENDS 375 Foundations of Visualization Geometric Representation 9/30/04.
CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes:
CSE554Binary PicturesSlide 1 CSE 554 Lecture 1: Binary Pictures Fall 2014.
TOPIC 4 PART III FILL AREA PRIMITIVES POLYGON FILL AREAS CGMB214: Introduction to Computer Graphics.
IT- 601: Computer Graphics Lecture-04 Scan Conversion Jesmin Akhter Lecturer,IIT Jahangirnagar University, Savar, Dhaka,Bangladesh 9/3/2015.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
1/24/20061 Fill-Area Algorithms and Functions. 1/24/20062 Learning Objectives OpenGL state variables Color and gray scale Color functions Point attributes.
CGMB214: Introduction to Computer Graphics
Dr. S.M. Malaek Assistant: M. Younesi
Jehee Lee Seoul National University
1 CPE 333 : Computer Graphics มหาวิทยาลัยเทคโนโลยีพระจอม เกล้าธนบุรี Dr. Natasha Dejdumrong.
Tools for Raster Displays CVGLab Goals of the Chapter To describe pixmaps and useful operations on them. To develop tools for copying, scaling, and rotating.
October 14, 2014Computer Vision Lecture 11: Image Segmentation I 1Contours How should we represent contours? A good contour representation should meet.
Part 6: Graphics Output Primitives (4) 1.  Another useful construct,besides points, straight line segments, and curves for describing components of a.
1 Introduction Line attribute Color and gray scale Area filled attribute Anti-aliasing.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
CGMB214: Introduction to Computer Graphics
INT 840E Computer graphics Introduction & Graphic’s Architecture.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
Computer Graphics Chapter 6 Andreas Savva. 2 Interactive Graphics Graphics provides one of the most natural means of communicating with a computer. Interactive.
Digital Media Dr. Jim Rowan ITEC So far… We have compared bitmapped graphics and vector graphics We have discussed bitmapped images, some file formats.
Creating Vectors – Part One 2.02 Understand Digital Vector Graphics.
2D Output Primitives Points Lines Circles Ellipses Other curves Filling areas Text Patterns Polymarkers.
1 CSCE 441: Computer Graphics Scan Conversion of Polygons Jinxiang Chai.
Scan Conversion.
Lecture 13: Raster Graphics and Scan Conversion
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Attributes of Graphics Primitives Chapter 4
Mohammed AM Dwikat CIS Department Digital Image.
Graphics Programming. Graphics Functions We can think of the graphics system as a black box whose inputs are function calls from an application program;
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
ATTRIBUTE OF OUTPUT PRIMITIVES. Attribute of Output Primitives 30/9/2008 A.Aruna/Assistant professor/IT/SNSCE 2 Definition Line Attribute Curve Attribute.
OUTPUT PRIMITIVES CEng 477 Computer Graphics METU, 2004.
CS552: Computer Graphics Lecture 16: Polygon Filling.
Graphics PRIMITIVES Chapter- 3 & Unit-2.
CSE 554 Lecture 1: Binary Pictures
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Computer Graphics Filled Area Primitives II Lecture 09 Taqdees A
Agenda Polygon Terminology Types of polygons Inside Test
Rasterizing Lines 1 Lecture 32 Mon, Nov 12, 2007.
Computer Graphics, KKU. Lecture 7
Agenda Polygon Terminology Types of polygons Inside Test
ویژگیهای مبناهای گرافیکی
Lecture 13 Clipping & Scan Conversion
Chapter 3 Graphics Output Primitives
Creating Vectors – Part One
Creating Vectors – Part One
Polygons.
Presentation transcript:

CHAPTER 3 2D GRAPHICS ALGORITHMS COMPUTER GRAPHICS CHAPTER 3 2D GRAPHICS ALGORITHMS

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

Output Primitives

Output Primitives 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. Examples: point, line, text, filled region, images, quadric surfaces, spline curves Each of the output primitives has its own set of attributes.

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

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

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

Output Primitives Polylines (closed) A polyline with the last point connected to the first point . 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()

Output Primitives Polygons A set of line segments joined end to end. 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()

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

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

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

Line Drawing Algorithms

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.

Line Drawing - Analytical Method x y=mx+c ax bx A(ax,ay) B(bx,by)

Line Drawing - Analytical Method 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); } Directly based on the analytical equation of a line. Involves floating point multiplication and addition Requires round-off function.

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

Incrementing along x xk+1 = xk+1 Current Pixel (xk, yk) (6,3) To find (xk+1, yk+!): xk+1 = xk+1 yk+1 = ? (5,2) (6,2) (6,1) 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.

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

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; } Does not involve any floating point multiplication. Involves floating point addition. Requires round-off function

Midpoint Algorithm xk+1 = xk+1 yk+1 = Either yk or yk+1 Midpoint algorithm is an incremental algorithm Assumption: Slope < 1 Current Pixel xk+1 = xk+1 yk+1 = Either yk or yk+1

Midpoint Algorithm - Notations Candidate Pixels Current Pixel ( xk, yk) Midpoint Line Coordinates of Midpoint = ( xk+1, yk+(1/2) ) ( xk+1, yk) ( xk+1, yk+1)

Midpoint Algorithm: Choice of the next pixel Midpoint Below Line Midpoint Above Line If the midpoint is below the line, then the next pixel is (xk+1, yk+1). If the midpoint is above the line, then the next pixel is (xk+1, yk).

Equation of a line revisited. Equation of the line: Let w = bx  ax, and h = by  ay. Then, h (x  ax)  w (y  ay) = 0. (h, w , ax , ay 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  ax)  w (y  ay). A(ax,ay) B(bx,by)

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

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

Midpoint Algorithm Decision Criteria Decision Parameter F(MP) = F(xk+1, yk+ ½) = Fk (Notation) If Fk < 0 : The midpoint is above the line. So the next pixel is (xk+1, yk). If Fk  0 : The midpoint is below or on the line. So the next pixel is (xk+1, yk+1).

Midpoint Algorithm – Story so far. Midpoint Above Line Next pixel = (xk+1, yk) Fk < 0 yk+1 = yk Midpoint Below Line Fk > 0 yk+1 = yk+1 Next pixel = (xk+1, yk+1)

Midpoint Algorithm Update Equation Fk = F(xk+1, yk+ ½) = h (xk+1  ax)  w (yk+½  ay) Update Equation But, Fk+1 = Fk + h  w (yk+1­­­ yk). (Refer notes) So, Fk< 0 : yk+1 = yk. Hence, Fk+1 = Fk + h . Fk  0 : yk+1 = yk+1. Hence, Fk+1 = Fk + h  w. F0 = h  w/2.

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++; }

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++; }

Circle Drawing Algorithms

Midpoint Circle Drawing Algorithm To determine the closest pixel position to the specified circle path at each step. For given radius r and screen center position (xc, yc), 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 xc to x and yc to y. 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 8 segments of octants for a circle:

Midpoint Circle Drawing Algorithm Circle function: fcircle (x,y) = x2 + y2 –r2 > 0, (x,y) outside the circle < 0, (x,y) inside the circle = 0, (x,y) is on the circle boundary { fcircle (x,y) =

Midpoint Circle Drawing Algorithm yk yk-1 midpoint yk yk-1 midpoint Next pixel = (xk+1, yk) Fk < 0 yk+1 = yk Next pixel = (xk+1, yk-1) Fk >= 0 yk+1 = yk - 1

Midpoint Circle Drawing Algorithm We know xk+1 = xk+1, Fk = F(xk+1, yk- ½) Fk = (xk +1)2 + (yk - ½)2 - r2 -------- (1) Fk+1 = F(xk+1, yk- ½) Fk+1 = (xk +2)2 + (yk+1 - ½)2 - r2 -------- (2) (2) – (1) Fk+1 = Fk + 2(xk+1) + (y2k+1 – y2k) - (yk+1 – yk) + 1 If Fk < 0, Fk+1 = Fk + 2xk+1+1 If Fk >= 0, Fk+1 = Fk + 2xk+1+1 – 2yk+1

Midpoint Circle Drawing Algorithm For the initial point, (x0 , y0) = (0, r) f0 = fcircle (1, r-½ ) = 1 + (r-½ )2 – r2 = 5 – r 4 ≈ 1 – r

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: f0 = 5 – r 4 = 5 – 10 = -8.75 ≈ –9

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

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; } }

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); }

Antialiasing

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:

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

Fill Area Algorithms

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.

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

Basic Filling Algorithm [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 (Code) 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 Requires an interior point. Involves considerable amount of stack operations. The boundary has to be closed. Not suitable for self-intersecting polygons

Types of Basic Filling Algorithms 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. Color is same as the old interior color

Boundary Fill Algorithm (Code) 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); }

Flood Fill Algorithm (Code) 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); }

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