CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.

Slides:



Advertisements
Similar presentations
Circle Drawing Asst. Prof. Dr. Ahmet Sayar Kocaeli University
Advertisements

Contents In today’s lecture we’ll have a look at:
Graphics Primitives: line
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.
Section 3-1 to 3-2, 3-5 Drawing Lines Some of the material in these slides may have been adapted from university of Virginia, MIT, and Åbo Akademi University.
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann.
Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
1 King ABDUL AZIZ University Faculty Of Computing and Information Technology CS 454 Computer graphics Drawing Elementary Figures Dr. Eng. Farag Elnagahy.
+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3.
Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier.
Scan Conversion Algorithms
Scan conversion of Line , circle & ellipse
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms.
CS 376 Introduction to Computer Graphics 01 / 29 / 2007 Instructor: Michael Eckmann.
OUTPUT PRIMITIVES Screen vs. World coordinate systems ● Objects positions are specified in a Cartesian coordinate system called World Coordinate.
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
Raster conversion algorithms for line and circle
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics May 3, 2007.
Line Drawing by Algorithm. Line Drawing Algorithms Line drawn as pixels Graphics system –Projects the endpoints to their pixel locations in the frame.
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Circle Drawing algo..
1/1/20001 Topic >>>> Scan Conversion CSE Computer Graphics.
Dr. S.M. Malaek Assistant: M. Younesi
Graphics Primitives: line. Pixel Position
WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition.
Scan Conversion Line and Circle
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
1Computer Graphics Implementation III Lecture 17 John Shearer Culture Lab – space 2
Introduction Computer Graphics & Its application Types of computer graphics Graphic display : random Scan & Raster Scan display Frame buffer and video.
Graphics Output Primitives
Line Drawing and Generalization. Outline  overview  line drawing  circle drawing  curve drawing.
CGMB214: Introduction to Computer Graphics
 A line segment in a scene is defined by the coordinate positions of the line end-points x y (2, 2) (7, 5)
Chapter 3 Scan Conversion Lecture-02. Bresenham’s Line Algorithm Bresenham’s line algorithm – is a highly efficient incremental method for scan- converting.
Image Synthesis Rabie A. Ramadan, PhD 7. 2 Image Rasterization.
CS-321 Dr. Mark L. Hornick 1 Line Drawing Algorithms.
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
Graphics Output Primitives Hearn & Baker Chapter 3
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
Scan Conversion.
Bresenham’s Line Algorithm
Lecture 2: 19/4/1435 Graphical algorithms Lecturer/ Kawther Abas CS- 375 Graphics and Human Computer Interaction.
Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Computer Graphics Inf4/MSc Computer Graphics Lecture 4 Line & Circle Drawing.
10/10/2006TCSS458A Isabelle Bichindaritz1 Line and Circle Drawing Algorithms.
Computer Graphics : output primitives.. 2 of 32 T1 – pp. 103–123, 137–145, 147–150, 164–171 Points and LinesPoints Line Drawing AlgorithmsLine Mid–Point.
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Line Drawing Algorithms 1. A line in Computer graphics is a portion of straight line that extends indefinitely in opposite direction. 2. It is defined.
Primitive graphic objects
CSCE 441 Lecture 2: Scan Conversion of Lines
Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Lecture 8 Shear and Line Drawing Algorithms
Chapter Three Part I Output Primitives CS 380.
© University of Wisconsin, CS559 Fall 2004
2D Scan-line Conversion
Rasterization and Antialiasing
Line and Curve Drawing Algorithms
Rasterization and Antialiasing
Scan Conversion (From geometry to pixels)
Chapter 3 Graphics Output Primitives
Line Drawing Algorithms
Presentation transcript:

CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE

DRAWING PRIMITIVES: LEGACY VS. NEW Legacy: specify primitive in glBegin() glBegin(GL_POINTS); glVertex3f(1,5,0); glVertex3f(2,3,1); … glEnd(); OpenGL 3.0+: specify primitive in glDrawArrays() or glDrawElementArrays() glDrawArrays(GL_POINTS, 0, pointCnt);

DRAWING POINTS AND LINES GL_POINTS = draws a point per vertex GL_LINES = draw individual lines with every 2 vertices Ignores unpaired vertices GL_LINE_STRIP = draw a polyline connecting all vertices in sequence GL_LINE_LOOP = draw a polyline, and then also connect the first and last vertices  closed polyline

THE PROBLEM WITH PIXELS Modern displays use grid of pixels  Discrete (digital) locations Problem: mathematical lines don’t fall perfectly into pixel locations I.e., have to round line coordinates to integers Lines end up having this stair-step effect (“jaggies”)  aliasing

DDA ALGORITHM Digital differential analyzer (DDA) Scan-conversion line algorithm based on calculating either dy or dx Sample one coordinate at unit intervals  find nearest integer value for other coordinate If dx > dy  increment x, add slope to y, and round to nearest integer: If dy > dx  swap roles of x and y (increment y, add inverse slope to x, and round) If going in reverse  decrement x and/or y

DDA ALGORITHM: CODE // Get dx and dy int dx = x1 - x0; int dy = y1 - y0; int steps, k; float xIncrement, yIncrement; // Set starting point float x = x0; float y = y0;

DDA ALGORITHM: CODE // Determine which coordinate we should step in if (abs(dx) > abs(dy)) steps = abs(dx); else steps = abs(dy); // Compute increments xIncrement = float(dx) / float(steps); yIncrement = float(dy) / float(steps);

DDA ALGORITHM: CODE // Let’s assume we have a magic function called setPixel(x,y) that sets a pixel at (x,y) to the appropriate color. // Set value of pixel at starting point setPixel(round(x), round(y)); // For each step… for (k = 0; k < steps; k++) { // Increment both x and y x += xIncrement; y += yIncrement; // Set pixel to correct color // NOTE: we need to round off the values to integer locations setPixel(round(x), round(y)); }

DDA ALGORITHM: PROS AND CONS Advantage: Faster than using the slope-intercept form directly  no multiplication, only addition Caveat: initial division necessary Disadvantages: Accumulation of round-off error can cause the line to drift off true path Rounding procedure still time-consuming

BRESENHAM’S LINE ALGORITHM Uses only integers Slope m = fraction of integers Only uses addition/subtraction in main loop Basic idea: given (x+1), choose either y or (y+1) based on decision variable Based on distance from true line

BRESENHAM’S LINE ALGORITHM: SUMMARIZED 1. Input two line endpoints and store LEFT endpoint in (x 0, y 0 ) 2. Plot first point (x 0, y 0 ) 3. Compute constants Δx, Δy, 2Δy, and 2Δy - 2Δx. Also compute first value of decision variable: p 0 = 2Δy – Δx 4. At each x k, test p k : If p k < 0  plot (x k + 1, y k )  p k+1 = p k + 2Δy Otherwise  plot (x k + 1, y k +1)  p k+1 = p k + 2Δy - 2Δx 5. Perform step 4 (Δx – 1) times NOTE: This version ONLY works with 0 < |m| < 1.0!!! Ergo, Δx and Δy are positive here! NOTE: Effectively assuming line starts at (0,0) and thus b = 0

BRESENHAM’S LINE ALGORITHM: CODE // NOTE: dx and dy are ABSOLUTE VALUES in this code int dx = fabs(x1 - x0); int dy = fabs(y1 - x0); int p = 2*dy - dx; int twoDy = 2*dy; int twoDyMinusDx = 2*(dy - dx); int x,y;

BRESENHAM’S LINE ALGORITHM: CODE // Determine which endpoint to use as start position if(x0 > x1) { x = x1; y = y1; x1 = x0; } else { x = x0; y = y0; } // Plot first pixel setPixel(x,y);

BRESENHAM’S LINE ALGORITHM: CODE while(x < x1) { x++; if(p < 0) p += twoDy; else { y++; p += twoDyMinusDx; } setPixel(x,y); }

BRESENHAM’S LINE ALGORITHM: GENERALIZED What we’re talked about only works with 0 < |m| < 1.0 For slopes other than 0 < |m| < 1.0, use symmetry: If dy > dx  swap x and y After swapping endpoints and potentially swapping x and y, if “y0” > “y1”  decrement “y” rather than increment NOTE: “y” may actually be x if you swapped them Two more warnings: 1) In the sample code, dx and dy are ABSOLUTE VALUES 2) In the next image, when I say x and y, I mean the actual x and y

MIDPOINT ALGORITHM Bresenham = specific form of midpoint algorithm Uses the implicit representation  e.g., implicit line equation If a point is on the “inside”, F(x,y) < 0 If a point is on the “outside”, F(x,y) > 0 If a point is exactly on the boundary, F(x,y) = 0 Test whether the point F(x+1, y + ½) is inside or outside  choose closest point

MIDPOINT CIRCLE ALGORITHM SUMMARIZED 1. Input radius r and circle center (x c, y c ); first point = (x 0, y 0 ) = (0,r) 2. Calculate initial decision variable value*: 3. At each x k, test p k : If p k < 0  next point is (x k+1, y k ) and: Otherwise  next point is (x k+1, y k - 1) and: Where: 4. For each calculated position (x,y), plot (x + x c, y + y c ) 5. Plot corresponding symmetric points in other seven octants 6. Repeat steps 3 through 5 UNTIL x >= y *NOTE: If r = integer, then can use p 0 = 1 – r