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.

Slides:



Advertisements
Similar presentations
Graphics Primitives: line
Advertisements

5.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 5 – Drawing A Line.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
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.
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.
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.
Computer Graphics Lecture 3 Line & Circle Drawing.
Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms.
30/9/2008Lecture 21 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester ITGD3107 University of Palestine.
Lecture 2 Line & Circle Drawing
The lines of this object appear continuous However, they are made of pixels 2April 13, 2015.
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.
Raster conversion algorithms for line and circle
Output Primitives Computer Graphics.
Course Website: Computer Graphics 5: Line Drawing Algorithms.
Line Drawing by Algorithm. Line Drawing Algorithms Line drawn as pixels Graphics system –Projects the endpoints to their pixel locations in the frame.
CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes:
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Scan Conversion. Also known as rasterization In our programs objects are represented symbolically –3D coordinates representing an object’s position –Attributes.
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.
Introduction Computer Graphics & Its application Types of computer graphics Graphic display : random Scan & Raster Scan display Frame buffer and video.
Graphics Output Primitives
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)
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.
Midpoint Circle Algorithm
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.
In the name of God Computer Graphics. Today Introduction Sampling Graphic Output Primitives 1.Line 2.Circle 3.Curve 4.polygon.
CS 551 / 645: Introductory Computer Graphics
Rendering.
Scan Conversion.
Bresenham’s Line Algorithm
Example: (7,9) (12,0) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0) (0,9) What are the problems with this method? Slope>1.
Lecture 2: 19/4/1435 Graphical algorithms Lecturer/ Kawther Abas CS- 375 Graphics and Human Computer Interaction.
Computer Graphics CC416 Week 14 Filling Algorithms.
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.
Rasterization CENG 477 Introduction to Computer Graphics Slides from Steve Marschner, CS4620, 2008 Cornell University.
Scan Conversion of Line Segments. What is Scan conversion? Final step of rasterization (the process of taking geometric shapes and converting them into.
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Computer Graphics Lecture 05 Line Drawing Techniques Taqdees A. Siddiqi
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
Scan Conversion or Rasterization
CSCE 441 Lecture 2: Scan Conversion of Lines
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Scan Conversion or Rasterization
Lecture 8 Shear and Line Drawing Algorithms
Chapter Three Part I Output Primitives CS 380.
CSCE 441 Lecture 2: Scan Conversion of Lines
Chapter 3 Graphics Output Primitives
Line Drawing Algorithms
Presentation transcript:

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

2 2 Line Algorithms ● Drawing 2-D lines: a case study in optimization ■ We will use Professor Leonard McMillan’s web-based lecture notes (then at MIT, now at UNC) ○ ■ His examples are in Java, but the correspondences are obvious.

3 3 Our first adventure into scan conversion. ● Line drawing is our first adventure into the area of scan conversion. ■ The need for scan conversion, or rasterization, techniques is a direct result of scanning nature of raster displays (thus the names). ● Algorithms are fundamental to both 2-D and 3-D computer graphics ● Transforming the continuous into this discrete (sampling) ● Line drawing was easy for vector displays ● Most incremental line-drawing algorithms were first developed for pen-plotters ● We will gradually evolve from the basics of algebra to the famous Bresenham line-drawing algorithms ● We will look at others

4 4 Quest for the Ideal Line ● Important line qualities: ■ Continuous appearance ■ Uniform thickness and brightness ■ Turn on the pixels nearest the ideal line ■ How fast is the line generated m b ● Line usually specified by slope m and a y-axis intercept called b. ■ Generally in computer graphics, a line will be specified by two endpoints.

5 5 Simple Line ● Based on the simple slope-intercept algorithm from algebra y = m x + b public void lineSimple(int x0, int y0, int x1, int y1, Color color) { int pix = color.getRGB(); int dx = x1 - x0; int dy = y1 - y0; raster.setPixel(pix, x0, y0); if (dx != 0) { float m = (float) dy / (float) dx; float b = y0 - m*x0; dx = (x1 > x0) ? 1 : -1; while (x0 != x1) { x0 += dx; y0 = Math.round(m*x0 + b); raster.setPixel(pix, x0, y0); } } }

6 6 Does this algorithm work? ● This algorithm works well when the desired line's slope is less than 1, but the lines become more and more discontinuous as the slope increases beyond one. ● Since this algorithm iterates over values of x between x0 and x1 there is a pixel drawn in each column. When the slope is greater than one then often more than one pixel must be drawn in each column for the line to appear continuous. LineSimple Applet

7 7 Improved Line Drawing Algorithm ● Problem: lineSimple does not give satisfactory results for slopes > 1. ● Solution: symmetry: The assigning of one coordinate axis the name x and the other y was an arbitrary choice. Notice that line slopes of greater than one under one assignment result in slopes less than one when the names are reversed.

8 8 lineImproved(int x0, int y0, int x1, int y1) { int dx = x1 - x0; int dy = y1 - y0; raster.setPixel(pixel1, x0, y0); if (Math.abs(dx) > Math.abs(dy)) { // slope < 1 // compute slope: float m = (float) dy / (float) dx; float b = y0 - m*x0; dx = (dx < 0) ? -1 : 1; while (x0 != x1) { x0 += dx; raster.setPixel(pix, x0, Math.round(m*x0 + b)); } else if (dy != 0) { // slope >= 1 // compute slope: float m = (float) dx / (float) dy; float b = x0 - m*y0; dy = (dy < 0) ? -1 : 1; while (y0 != y1) { y0 += dy; raster.setPixel(pixel1, Math.round(m*y0 + b), y0); }

9 9 Algorithm Comment  Notice that the slope-intercept equation of the line is executed at each step of the inner loop. Line Improved Applet

10 Line Drawing Optimization – Optimize Inner Loop ● Optimize those code fragments where the algorithm spends most of its time ● Often these fragments are inside loops. ● Remove unnecessary method invocations: Replace Math.round(m*x0 + b) with (int)(m*y0 + b + 0.5). ● Use incremental calculations: Consider the expression : y = (int)(m*x + b + 0.5) The value of y is known at x0 (i.e. it is y ) Future values of y can be expressed in terms of previous values with a difference equation: ○ y i+1 = y i + m; or y i+1 = y i - m; Adding all these modifications to the lineModified() method, we obtain a new line drawing method called a Digital Differential Analyzer or DDA for short.

11 Low level Optimization ● Low-level optimizations are dubious, because they depend on specific machine details. ● However, a set of general rules that are more-or-less consistent across machines include: ■ Addition and Subtraction are generally faster than Multiplication. ■ Multiplication is generally faster than Division. ■ Using tables to evaluate discrete functions is faster than computing them ■ Integer calculations are faster than floating-point calculations. ■ Avoid unnecessary computation by testing for various special cases. ■ The intrinsic tests available to most machines are greater than, less than, greater than or equal, and less than or equal to zero (not an arbitrary value).

12 Applications of Low-level Optimizations ● Notice that the slope is always rational (a ratio of two integers). m = (y1 - y0) / (x1 - x0) ● Also notice that the incremental part of the algorithm never generates a new y that is more than one unit away from the old one (because the slope is always less than one) y i+1 = y i + m ● Thus, if we maintained the only fractional part of y we could still draw a line by noting when this fraction exceeded one. If we initialize fraction with 0.5, then we will also handle the rounding correctly as in our DDA routine: fraction += m; if (fraction >= 1) { y = y + 1; fraction -= 1; }

13 Bresenham's line drawing algorithm ● y is now an integer. Can we represent the fraction as an integer? ● After we draw the first pixel (which happens outside our main loop) the correct fraction is: fraction = 1/2 + dy/dx ● If we scale the fraction by 2*dx the following expression results: scaledFraction = dx + 2*dy ● So the incremental update becomes: scaledFraction += 2*dy // 2*dx*(dy/dx) ● & our test must be modified to reflect the new scaling: if (fraction >= 1) { y = y + 1; fraction -= 1; }  if (scaledFraction >= 2*dx)

14 Bresenham's line drawing algorithm ● This test can be made against a value of zero if the inital value of scaledFraction has 2*dx subtracted from it. Giving, outside the loop: OffsetScaledFraction = dx + 2*dy - 2*dx = 2*dy - dx ● So the inner loop becomes OffsetScaledFraction += 2*dy if (OffsetScaledFraction >= 0) { y = y + 1; fraction -= 2*dx; } ● We might as well double the values of dy and dx (this can be accomplished with either an add or a shift outside the loop). ● The resulting method is known as Bresenham's line drawing algorithm Bresenham's Line Drawing Applet

15 Bresenham's Line-Drawing Algorithm for |m| < 1 Drawing a line from (x0,y0) to (x1,y1) assuming x1>x0 1. Input the two line endpoints and store the left endpoint in (x0,y0) 2. Load (x0,y0) into the frame buffer; that is, plot the first point. 3. Calculate constants dx, dy=abs(y1-y0), 2dy, and 2dy–2dx, ystep (=1 for y1>y0 or = -1 for y1<y0) and obtain the starting value for the decision parameter as P 0 = 2dy – dx 4. At each x k along the line, starting at k = 0, perform the following test: 1. If P k < 0, the next point to plot is (x k + 1, y k ) and P k+1 =P k +2dy 2. Otherwise, the next point to plot is (x k +1, y k +ystep) and P k+1 = P k + 2dy – 2dx 5. Repeat step 4 dx times.