A New Line Drawing Algorithm Based on Sample Rate Conversion

Similar documents
OpenGL Graphics System. 2D Graphics Primitives. Drawing 2D Graphics Primitives. 2D Graphics Primitives. Mathematical 2D Primitives.

Line Drawing Week 6, Lecture 9

UNIT -8 IMPLEMENTATION

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

An Improved Algorithm for Scan-converting a Line

CPSC / Scan Conversion

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

Line Drawing. Foundations of Computer Graphics Torsten Möller

Display Technologies: CRTs Raster Displays

Line Drawing. Introduction to Computer Graphics Torsten Möller / Mike Phillips. Machiraju/Zhang/Möller

(Refer Slide Time: 00:03:51)

Output Primitives. Dr. S.M. Malaek. Assistant: M. Younesi

Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling

Scan Conversion. CMP 477 Computer Graphics S. A. Arekete

MODULE - 4. e-pg Pathshala

CS 450: COMPUTER GRAPHICS RASTERIZING LINES SPRING 2016 DR. MICHAEL J. REALE

Tópicos de Computação Gráfica Topics in Computer Graphics 10509: Doutoramento em Engenharia Informática. Chap. 2 Rasterization.

Rasterization: Geometric Primitives

EFFICIENT INTEGER ALGORITHMS FOR THE GENERATION OF CONIC SECTIONS

Chapter - 2: Geometry and Line Generations

Rendering. A simple X program to illustrate rendering

Computer Graphics D Graphics Algorithms

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Rasterization. CS4620 Lecture 13

CS 4731: Computer Graphics Lecture 21: Raster Graphics: Drawing Lines. Emmanuel Agu

Computer Graphics. Lecture 2. Doç. Dr. Mehmet Gokturk

Digital Differential Analyzer Bresenhams Line Drawing Algorithm

The graphics pipeline. Pipeline and Rasterization. Primitives. Pipeline

Rasterization. CS4620/5620: Lecture 12. Announcements. Turn in HW 1. PPA 1 out. Friday lecture. History of graphics PPA 1 in 4621.

Efficient Plotting Algorithm

Rasterization. CS 4620 Lecture Kavita Bala w/ prior instructor Steve Marschner. Cornell CS4620 Fall 2015 Lecture 16

Pipeline and Rasterization. COMP770 Fall 2011

GRAPHICS OUTPUT PRIMITIVES

From Vertices to Fragments: Rasterization. Reading Assignment: Chapter 7. Special memory where pixel colors are stored.

Scan Converting Circles

Graphics (Output) Primitives. Chapters 3 & 4

Chapter 8: Implementation- Clipping and Rasterization

From Ver(ces to Fragments: Rasteriza(on

Department of Computer Sciences Graphics Fall 2003 (Lecture 2) Pixels

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

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

UNIT 2 GRAPHIC PRIMITIVES

CS 130. Scan Conversion. Raster Graphics

Number- Algebra. Problem solving Statistics Investigations

1 Introduction to Graphics

R asterisation. Part I: Simple Lines. Affine transformation. Transform Render. Rasterisation Line Rasterisation 2/16

Rasterization, or What is glbegin(gl_lines) really doing?

In today s lecture we ll have a look at: A simple technique The mid-point circle algorithm

Surface shading: lights and rasterization. Computer Graphics CSE 167 Lecture 6

MET71 COMPUTER AIDED DESIGN

Vertical and Horizontal Translations

RASTERIZING POLYGONS IN IMAGE SPACE

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

Announcements. Midterms graded back at the end of class Help session on Assignment 3 for last ~20 minutes of class. Computer Graphics

OUTPUT PRIMITIVES. CEng 477 Introduction to Computer Graphics METU, 2007

Computer Graphics. - Rasterization - Philipp Slusallek

Number Mulitplication and Number and Place Value Addition and Subtraction Division

An Intersecting Modification to the Bresenham Algorithm

CSCI 4620/8626. Coordinate Reference Frames

CSC Computer Graphics

Introduction to Bresenham s Line Algorithm Using. Instruction Series Graphics Note 5

Rendering. A simple X program to illustrate rendering

New Swannington Primary School 2014 Year 6

0. Introduction: What is Computer Graphics? 1. Basics of scan conversion (line drawing) 2. Representing 2D curves

Scheme of work Elementary 1, the maths pathway

The Traditional Graphics Pipeline

Circular Hough Transform

Computer Graphics D Graphics Algorithms

(Refer Slide Time: 9:36)

Maintaining Mathematical Proficiency

Oral and Mental calculation

An Assertion-Guided Derivation of a Circle Drawing Algorithm

Topic #1: Rasterization (Scan Conversion)

Year 6 Maths Long Term Plan

Points and lines. x x 1 + y 1. y = mx + b

Introduction to Computer Graphics (CS602) Lecture 05 Line Drawing Techniques

Chapter 3. Sukhwinder Singh

GRAPHING WORKSHOP. A graph of an equation is an illustration of a set of points whose coordinates satisfy the equation.

Grade 6 Math Curriculum Sequence School Year

Read, write compare and order numbers beyond 1000 in numerals and words Read Roman numerals to 100 and understand how they have changed through time

Output Primitives Lecture: 4. Lecture 4

CSAP Achievement Levels Mathematics Grade 6 March, 2006

CS Rasterization. Junqiao Zhao 赵君峤

NUMBER SCALING FOR THE LGP-27

CS602 Midterm Subjective Solved with Reference By WELL WISHER (Aqua Leo)

CS 543: Computer Graphics. Rasterization

(Refer Slide Time: 00:02:00)

Einführung in Visual Computing

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY CS2401 COMPUTER GRAPHICS QUESTION BANK

Year 6 Mathematics Overview

Y6 MATHEMATICS TERMLY PATHWAY NUMBER MEASURE GEOMETRY STATISTICS

Year 6.1- Number and Place Value 2 weeks- Autumn 1 Read, write, order and compare numbers up to and determine the value of each digit.

Decimals Outcomes. Represent Q Using Decomposition

The Traditional Graphics Pipeline

Level 3 will generally. Level 2 may demonstrate limited ability to: Same as Level 2 Same as Level 2 identify models or

Overview of Computer Graphics

Department of Computer Science Engineering, Mits - Jadan, Pali, Rajasthan, India

The Traditional Graphics Pipeline

Chapter 1. Linear Equations and Straight Lines. 2 of 71. Copyright 2014, 2010, 2007 Pearson Education, Inc.

MATHEMATICS Key Stage 2 Year 6

Transcription:

A New Line Drawing Algorithm Based on Sample Rate Conversion c 2002, C. Bond. All rights reserved. February 5, 2002 1 Overview In this paper, a new method for drawing straight lines suitable for use on raster scan displays and plotters is presented. Unlike previous methods which are based on DDA or midpoint algorithms, this technique is based on signal processing concepts related to resampling, multirate processing and sample rate conversion generally, and decimation in particular. An algorithm based on spatial decimation is developed which has attractive features including the elimination of test, compare and branch operations within the inner plotting loop. Furthermore, all multiplies, divides, shifts and other complex CPU operations are eliminated from the inner loop, as well. Lines can be drawn in any orientation with nearest pixel accuracy using only the primitive CPU operations of addition (subtraction) and incrementing (decrementing). With some CPU architectures, including current Intel and AMD processors, it is possible to hold all variables in CPU registers so that the only memory accesses required in the inner loop relate to the plotting function. 2 Background A variety of line drawing algorithms have been published in the literature. Among these are algorithms by Bresenham [1], Wu [5] and others. The central problem solved by these algorithms is to find a best fit to an ideal line, given the constraints imposed by a raster scan or integer grid limited display. See also Pitteway [3], and Foley [2]. These prior art solutions are typically derived from the differential quantities associated with a straight line and focus on correct pixel selection or 1

improvements in efficiency. The new method is based on signal processing concepts. For general information about signal processing see Ifeachor [6] and Oppenheim [7]. 3 The Algorithm The derivation of the new algorithm will focus on applying decimation or rate multiplying methods to the problem of drawing a line in the first octant. The other octants can be supported by changing the signs of x and y, or exchanging the roles of x and y, or both. 3.1 Decimation In signal processing, a significant and recurrent problem has to do with aligning data sets associated with different sample rates. For example, it may be necessary to convert a data set sampled at 48 kilosamples per second to data set with equivalent sampling at 44.1 kilosamples per second. The challenge is to align the ends of the source and target data sets, and devise a resampling strategy which produces the target set from the source set with minimum error. When the target set has a lower sample rate (fewer samples per second) this process is called decimation. The following figure illustrates decimation for a short signal fragment. 16 samples 11 samples In this figure, the upper trace represents a source data set consisting of 16 samples. The lower trace represents the same signal decimated (downsampled) to 11 samples. It is offset vertically for clarity. Decimation in a sampled digital environment is a dirty process, in the sense that sometimes dubious assumptions are made about the behavior of the signal between sample points. Nevertheless, most signals of interest are rea- 2

sonably smooth, so the errors are generally acceptable. Since we will be applying decimation techniques to straight lines, rather than arbitrary curves, there are no assumptions required and the process introduces no errors other those inherent in the scan conversion. How is decimation applied to the line drawing problem? Consider the line in the figure below. 20 15 4 3 (x 2, y 2 ) 10 5 0 2 1 0 (x 1, y 1 ) 0 1 2 3 4 5 0 5 10 15 20 25 The x coordinates of each pixel can be viewed as a uniform sample set. The y coordinates represent another sample set. Assuming, without loss of generality, that the slope of the line is within the first octant, the x set will be the larger of the two. Given the coordinates of the endpoints and using decimation, we can generate the y coordinates by resampling the set of x coordinates. In a sense, we have rotated the resampled sequence by 90 degrees. The decimator is the key to making this process efficient. Its purpose is to step along the same interval at two different rates. As applied here, it works by algorithmically matching the y coordinate set to the x set by taking unit steps along the x coordinate and stepping at a different rate along the y axis. Since the y steps typically involve a fractional quantity, fixed point math will be used to track the exact y value. The integer part of y, which is the only part usable on the raster grid, is found by updating a control variable which represents the fractional part of the y coordinate at each step. When the control variable passes through the next integer value, the y coordinate is incremented. The update value is simply the ratio the total y distance over the total x distance. Note that this process bears a superficial resemblance to line drawing by DDA methods, 3

but the emphasis on decimation suggests a new solution for the y coordinate update problem. 3.2 The New Solution The y coordinate is incremented when the fractional part of the control variable overflows, increasing the integer part by unity. Now identify the y coordinate with the integer part of the control variable and allocate a separate integer (register) for the fractional part. If the fractional part is scaled so that it occupies an entire unsigned integer, its overflow will generate a CPU carry. By simply adding the carry to the y coordinate, we can update it properly without any need for correcting the fraction or testing any of the other variable quantities. To understand how this is done, we must see how to scale the fractional portion of the update quantity to fit an integer. Let the control variable be cvar and the update quantity be incr. Assuming 16-bit integers, 1 the generating equation for incr is: y 2 y 1 x 2 x 1 = y x y x = incr 65536 incr (1) (2) = y 65536. (3) x The multiplication by 65536 is even simpler than a word shift, because 16- bit integer division on most processors leaves the fractional part in a specific register as a remainder. Just take that register as incr. cvar is initialized to 32768 (0x8000), which is the fractional equivalent of 0.5. The new algorithm is summarized in the following pseudo-code for the inner drawing loop: 1 16-bit integers are not required by the algorithm. 32-bit integers could also be used. 4

1. cvar 0x8000, incr y/ x, 2. x x 1 ; y y 1, 3. plot x,y, 4. add incr to cvar, 5. add carry to y, 6. increment x, 7. repeat 3-6 until done (x = x 2 ). Observe that in step 5, if no carry is generated, the y coordinate will be unchanged. 4 Conclusion It has been shown that it is possible to devise a line drawing algorithm such that the inner drawing loop requires no test, compare or branch instructions. In addition, there are no complex CPU instructions, such as multiplies divides or shifts. For the case shown, with the line in the first octant, we have identified the y coordinate with the integer portion of the control variable, and allocated a separate integer for the fractional part. By separating the integer and fractional parts, we have found a means to employ the CPU carry bit as a convenient, efficient device to transfer integer updates to the y coordinate without auxiliary tests, branches or correction factors. The following quantities are required to support the algorithm x: The x coordinate, y: The y coordinate, incr: The update quantity, cvar: The control variable. All these values can be held in processor registers during the plotting process. 5

4.1 Optimization For the basic algorithm operating in octant I, the x value and the loop control variable both increment in unit steps at each iteration. Hence, it may be possible to combine the two operations in such a way as to reduce the number of increments by one, per iteration. There are several ways to do this, and the performance advantage of any particular method is processor dependent. 4.2 Extension to other Octants Interchanging the roles of x and y will map the algorithm for octant I to octant II. Changing the signs the update quantities for x and/or y will map to other quadrants. These modifications in combination will extend the basic algorithm to work in any octant. There are several high level strategies possible for implementing a general line drawing algorithm. An obvious method is to implement 8 versions of the algorithm, one for each octant, and traverse a decision tree to determine the appropriate octant from the given line coordinates. Another is to derive signed update quantities based on the required octant and use them for incrementing (decrementing) and adding (subtracting) in the inner loop. These methods have been used successfully by other line drawing algorithms and are not detailed here. 5 Comments 5.1 Downsampling The line drawing algorithm presented above is based on an efficient linear downsampler (decimator) which has been used for many years by the author. An observation that there was an equivalence between the integer indices used in sample sequences, and the integer grid coordinates of the display inspired the adaptation. A brief description of the downsampler should clarify its functional equivalence with the line drawing routine. In operation, the signal decimator uses the integer x and y values as indices 6

into source and target sample vectors, z[n] and z[m]. incr is initialized to m/n, and x, y and cvar are initially set to zero. To start, z[0] z[0]. Operations may be performed in floating point or fixed point math, but recall that cvar is a fraction. In the inner decimation loop, x is incremented and cvar is updated by the addition of incr. When cvar overflows, y is incremented and the array entry for z[y] is set to z[x] + (z[x + 1] z[x]) cvar. Iteration is continued until x = n. 5.1.1 Decomposition of Control Variable There are several ways to interpret the new algorithm. Its basic strategy is to separate the integer and fractional parts of the control variable so the y coordinate is identified with the integer part and is immediately accessible to the plot routine without modification. The fractional part is cvar. This decomposition operates the same way for the two 16-bit integers as would a 32-bit fixed point control variable consisting of a 16-bit upper integer part and a lower 16-bit fractional part. For the 32-bit variable, updating the upper part occurs automatically with the addition of incr. However, the y coordinate cannot be directly accessed except by right shifting the variable by 16 bits. Furthermore, since the control variable must be maintained from one iteration to the next, the right shifting must be done on a temporary copy, involving another operation. Hence, the 32-bit solution requires a copy operation followed by a 16-bit right shift. For comparative purposes the new algorithm replaces the copy and right shift with a simple add instruction. For Intel and AMD processors the code sequence using a 32-bit control variable is: mov tmpreg,rega shr tmpreg,16 ; tmp cvar ; recover y coordinate For the new algorithm, with separated integer and fractional parts, this is replaced by: adc yreg,0 ; update y 7

References [1] J. E. Bresenham, Algorithm for Computer Control of a Digital Plotter, IBM Systems Journal, 4(1), 1965, pp.25-30. [2] James D. Foley, et al., Computer Graphics, principles and practices, Addison-Wesley Publishing Company, 1997, pp. 72-81. [3] M. L. V. Pitteway, Algorithm for Drawing Ellipses and Hyperbolae with a Digital Plotter, Computer J., 10(3), Nov. 1967, pp. 282-289. [4] J. C. Rokne, et al., Fast Line Scan Conversion, ACM Transactions on Graphics, 1990. [5] X. Wu and J. G. Rokne, Double-Step Incremental Generation of Lines and Circles, Computer Vision, Graphics and Image Processing, 37, pp. 331-334. [6] Emmanuel C. Ifeachor and Barrie W. Jervis, Digital Signal Processing, A Practical Approach, Addison-Wesley Publishing Company, 1993. [7] Alan V. Oppenheim and Ronald W. Schafer, Digital Signal Processing, Prentice-Hall, Inc., 1975. 8