Computer Graphics: Line Drawing Algorithms

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

Assignment 1. Simple Graphics program using OpenGL

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

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

Digital Differential Analyzer Bresenhams Line Drawing Algorithm

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

COMPUTER GRAPHICS LAB # 3

Drawing Primitives. OpenGL basics

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

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

Overview of Graphics Systems Hearn & Baker Chapter 2. Some slides are taken from Robert Thomsons notes.

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Graphics Programming

Lecture 3. Understanding of OPenGL programming

PART-I: Lab for MCS-051 (Advanced Internet Technologies)

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

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

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

6. Make use of glviewport() to display two sine curves on the same screen, one on the

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Computer Graphics (Basic OpenGL)

Overview of Computer Graphics

Announcements OpenGL. Computer Graphics. Spring CS4815

Rasterization: Geometric Primitives

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

COS340A Assignment 1 I Pillemer Student# March 25 th 2007 p1/15

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Computer Graphics

Programming with OpenGL Part 1: Background

Computer Graphics. Bing-Yu Chen National Taiwan University

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

Display Technologies: CRTs Raster Displays

Programming using OpenGL: A first Introduction

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

From 3D World to 2D Screen. Hendrik Speleers

Andrew Yenalavitch Homework 1 CSE Fall 2014

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

CS380: Computer Graphics Basic OpenGL Structure. Sung-Eui Yoon ( 윤성의 ) Course URL:

Bob s Concise Introduction to Doxygen

Renderer Implementation: Basics and Clipping. Overview. Preliminaries. David Carr Virtual Environments, Fundamentals Spring 2005

3D computer graphics: geometric modeling of objects in the computer and rendering them

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

CSC Computer Graphics

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

CSE4030 Introduction to Computer Graphics

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

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Programming with OpenGL Part 1: Background

Computer Graphics, Chapt 08

Computer Graphics Lecture Notes

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

Using OpenGL with CUDA

Chapter - 2: Geometry and Line Generations

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

OpenGL refresher. Advanced Computer Graphics 2012

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

CS Rasterization. Junqiao Zhao 赵君峤

An Improved Algorithm for Scan-converting a Line

Computer Graphics Anatomy of GUI. Computer Graphics CSC470 1

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

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

A Mathematical Overview of Bresenham Algorithms in the Determination of Active Pixel Positions

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

Draw the basic Geometry Objects. Hanyang University

Introduction to MS Visual C/C++

Basic Graphics Programming

CS 4204 Computer Graphics

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

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

From Ver(ces to Fragments: Rasteriza(on

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

Line Drawing Week 6, Lecture 9

Computer Graphics. Chapter 3 Computer Graphics Software

1.2 Basic Graphics Programming

Reading on the Accumulation Buffer: Motion Blur, Anti-Aliasing, and Depth of Field

Programming with OpenGL Part 3: Three Dimensions

MODULE - 4. e-pg Pathshala

C OMPUTER G RAPHICS Thursday

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:


VR-programming tools (procedural) More VRML later in this course! (declarative)

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Chapter 3: Graphics Output Primitives. OpenGL Line Functions. OpenGL Point Functions. Line Drawing Algorithms

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

Basic Graphics Programming

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

GLUT Tutorial. John Tsiombikas (Nuclear / The Lab Demos) May 6, Introduction to GLUT (OpenGL Utility Toolkit)

LECTURE 02 OPENGL API

Computer Graphics. OpenGL

Lectures OpenGL Introduction

Transcription:

Computer Graphics: Line Drawing Algorithms 1 Graphics hardware The problem scan conversion Considerations Line equations Scan converting algorithms A very simple solution The DDA algorithm, Bresenham algorithm Open GL Conclusion

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) 2 Graphics Hardware It s worth taking a little look at how graphics hardware works before we go any further How do things end up on the screen?

3 Architecture Of A Graphics System Display Processor Memory CPU Display Processor System Bus Frame Buffer System Memory Video Controller Monitor

4 Output Devices There are a range output devices currently available: Printers/plotters Cathode ray tube displays Plasma displays LCD displays 3 dimensional viewers Virtual/augmented reality headsets We will look briefly at some the more common display devices

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) Basic Cathode Ray Tube (CRT) Fire an electron beam at a phosphor coated screen

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) 6 Raster Scan Systems Draw one line at a time

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) 7 Colour CRT An electron gun for each colour red, green and blue

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) 8 Plasma-Panel Displays Applying voltages to crossing pairs conductors causes the gas (usually a mixture including neon) to break down into a glowing plasma electrons and ions

Images taken from Hearn & Baker, Computer Graphics with OpenGL (2004) 9 Liquid Crystal Displays Light passing through the liquid crystal is twisted so it gets through the polarizer A voltage is applied using the crisscrossing conductors to stop the twisting and turn pixels f

10 Problem Of Scan Conversion: Line A line segment in a scene is defined by the coordinate positions the line end-points y (7, ) (2, 2) x

11 The Problem (cont ) But what happens when we try to draw this on a pixel based display? How do we choose which pixels to turn on?

12 Considerations Considerations to keep in mind: The line has to look good Avoid jaggies It has to be lightening fast! How many lines need to be drawn in a typical scene? This is going to come back to bite us again and again

13 Line Equations Let s quickly review the equations involved in drawing lines Slope-intercept line y equation: y m x b yend where: yend y0 m xend x0 y0 x0 xend x b y0 m x0

14 Lines & Slopes The slope a line (m) is defined by its start and end coordinates The diagram below shows some examples lines and their slopes m = -4 m = -2 m = -1 m = - 1/2 m= m=4 m=2 m=1 m = 1/2 m = -1/3 m = 1/3 m=0 m=0

1 A Very Simple Solution We could simply work out the corresponding y coordinate for each unit x coordinate Let s consider the following example: y (7, ) 2 (2, 2) 2 7 x

16 A Very Simple Solution (cont ) 4 3 2 1 0 0 1 2 3 4 6 7

17 y A Very Simple Solution (cont ) (7, ) First work out m and b: 2 3 m 7 2 2 (2, 2) 3 4 b 2 2 x 2 3 4 6 7 Now for each x value work out the y value: 3 4 3 y (3) 3 2 3 4 4 y () 3 3 4 1 y (4) 4 3 3 4 2 y (6) 6 4

18 A Very Simple Solution (cont ) Now just round f the results and turn on these pixels to draw our line 7 6 4 3 2 1 0 0 1 2 3 4 6 7 8 3 y (3) 2 3 1 y (4) 3 3 4 y () 3 4 2 y (6) 4 4

19 A Very Simple Solution (cont ) However, this approach is just way too slow In particular look out for: The equation y = mx + b requires the multiplication m by x Rounding f the resulting y coordinates We need a faster solution

20 A Quick Note About Slopes In the previous example we chose to solve the parametric line equation to give us the y coordinate for each unit x coordinate What if we had done it the other way around? y b So this gives us: x m yend y0 where: m and b y0 m x0 xend x0

21 A Quick Note About Slopes (cont ) Leaving out the details this gives us: 2 x(3) 3 4 3 1 x(4) 3 We can see easily that this line doesn t look very good! We choose which way to work out the line pixels based on the slope the line 7 6 4 3 2 1 0 0 1 2 3 4 6 7 8

22 A Quick Note About Slopes (cont ) If the slope a line is between -1 and 1 then we work out the y coordinates for a line based on it s unit x coordinates Otherwise we do the opposite x coordinates are computed based on unit y coordinates m = -4 m = -2 m = -1 m = - 1/2 m = - 1/3 m=0 m= m=4 m=2 m=1 m = 1/2 m = 1/3 m=0

23 A Quick Note About Slopes (cont ) 4 3 2 1 0 0 1 2 3 4 6 7

24 The DDA Algorithm: Incremental The digital differential analyzer (DDA) algorithm takes an incremental approach in order to speed up scan conversion Simply calculate yk+1 based on yk The original differential analyzer was a physical machine developed by Vannevar Bush at MIT in the 1930 s in order to solve ordinary differential equations. More information here.

2 The DDA Algorithm (cont ) Consider the list points that we determined for the line in our previous example: (2, 2), (3, 23/), (4, 31/), (, 34/), (6, 42/), (7, ) Notice that as the x coordinates go up by one, the y coordinates simply go up by the slope the line This is the key insight in the DDA algorithm

26 The DDA Algorithm (cont ) When the slope the line is between -1 and 1 begin at the first point in the line and, by incrementing the x coordinate by 1, calculate the corresponding y coordinates as follows: yk 1 yk m When the slope is outside these limits, increment the y coordinate by 1 and calculate the corresponding x coordinates as follows: 1 xk 1 xk m

27 The DDA Algorithm (cont ) Again the values calculated by the equations used by the DDA algorithm must be rounded to match pixel values (xk+1, round(yk+m)) (xk, yk) (xk+1, yk+m) (xk, round(yk)) (round(xk+ 1/m), yk+1) (xk, yk) (xk+ 1/m, yk+1) (round(xk), yk)

28 DDA Algorithm Example Let s try out the following examples: y 7 (2, 7) (7, ) 2 y 2 (2, 2) 2 7 x (3, 2) 2 3 x

29 DDA Algorithm Example (cont ) 7 6 4 3 2 0 1 2 3 4 6 7

30 The DDA Algorithm Summary The DDA algorithm is much faster than our previous attempt In particular, there are no longer any multiplications involved However, there are still two big issues: Accumulation round-f errors can make the pixelated line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming

31 Open GL Open Graphics Library (OpenGL) is cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU) Explore: https://www.opengl.org/

Open GL: Installation OpenGL (GLUT) on Linux (Ubuntu) Installation Install the following packages from the ubuntu repository: 1. freeglut3-dev 2. mesa-common-dev sudo apt-get install freeglut3 freeglut3-dev mesacommon-dev Check your /usr/include/gl folder to verify the installation the opengl headers that you intend to use. Note: sudo add-apt-repository main universe restricted multiverse; sudo apt-get update

33 Open GL: Header Files // Header Files #include <math.h> #include <GL/gl.h> #include <GL/glut.h>

34 Open GL: main() int main(int argc, char ** argv) { glutinit(&argc,argv); // Initialize the toolkit glutinitdisplaymode(glut_single GLUT_RGB); // set Display mode glutinitwindowsize(640, 480); // set window size glutinitwindowposition(100, 10); // set window position on screen glutcreatewindow("dot Plot a Line");// open the screen window glutdisplayfunc(mydisplay); // register redrtaw function myinit(); glutmainloop(); // go into a perpetual loop return 0; }

3 Open GL: Initialization void myinit(void) { glclearcolor(1.0, 1.0, 1.0, 0.0); // set whitebackground color glcolor3f(0.0f, 0.0f, 0.0f); // set the drawing color(black) glpointsize(4.0); // a 'dot' is 4 by 4 pixels glmatrixmode(gl_projection); // set camera shape glloadidentity(); gluortho2d(0.0, GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); }

36 Open GL: mydisplay void mydisplay(void) { glclear(gl_color_buffer_bit); the screen linedda(10, 10, 400, 400); linedda(300, 100, 300, 400); glflush(); // send all output to display } // clear

37 Open GL: Plotting Point void drawdot(glint x, GLint y) { glbegin(gl_points); glvertex2i(x,y); glend(); }

38 Open GL: DDA Line Drawing(1) void linedda(float x1, float y1, float x2, float y2) { float x, y, dx, dy, pixel; int i; dx = fabs(x2 - x1); dy = fabs(y2 - y1); if(dx >= dy) pixel = dx; else pixel = dy; dx = dx / pixel; dy = dy / pixel;

39 Open GL: DDA Line Drawing(2) x = x1; y = y1; i=1; while(i <= pixel) { drawdot((glint)(x+0.), (GLint)(y+0.)); x = x + dx; y = y + dy; i = i + 1; } }

40 Open GL: Compiling &Linking Compiling and Linking You will have to use the -lglut linker option with gcc/g++ to compile a program with glut library. For example, to compile the program test.cpp that uses GLUT type, use gcc -o test test.cpp -lglut -lgl lglu -lm to get the binary executable.

41 Open GL: execution

42 The Bresenham Line Algorithm The Bresenham algorithm is another incremental scan conversion algorithm The big advantage this algorithm is that it uses only integer calculations Jack Bresenham worked for 27 years at IBM before entering academia. Bresenham developed his famous algorithms at IBM in the early 1960s

43 The Big Idea Move across the x axis in unit intervals and at each step choose between two different y coordinates For example, from position (2, 3) we (x +1, y +1) have to choose 4 between (3, 3) and (x, y ) 3 (3, 4) (x +1, y ) We would like the 2 point that is closer to 2 3 4 the original line k k k k k k

44 Deriving The Bresenham Line Algorithm At sample position xk+1 yk+1 the vertical y separations from the mathematical line are yk labelled dupper and dlower dupper dlower xk+1 The y coordinate on the mathematical line at xk+1 is: y m( xk 1) b

4 Deriving The Bresenham Line Algorithm (cont ) So, dupper and dlower are given as follows: d lower y yk m( xk 1) b yk and: d upper ( yk 1) y yk 1 m( xk 1) b We can use these to make a simple decision about which pixel is closer to the mathematical line

46 Deriving The Bresenham Line Algorithm (cont ) This simple decision is based on the difference between the two pixel positions: d lower d upper 2m( xk 1) 2 yk 2b 1 Let s substitute m with y/ x where x and y are the differences between the endpoints: y x(d lower d upper ) x(2 x ( xk 1) 2 yk 2b 1) 2 y xk 2 x yk 2 y x(2b 1) 2 y xk 2 x yk c

47 Deriving The Bresenham Line Algorithm (cont ) So, a decision parameter pk for the kth step along a line is given by: pk x(d lower d upper ) 2 y xk 2 x yk c The sign the decision parameter pk is the same as that dlower dupper If pk is negative, then we choose the lower pixel, otherwise we choose the upper pixel

48 Deriving The Bresenham Line Algorithm (cont ) Remember coordinate changes occur along the x axis in unit steps so we can do everything with integer calculations At step k+1 the decision parameter is given as: pk 1 2 y xk 1 2 x yk 1 c Subtracting pk from this we get: pk 1 pk 2 y ( xk 1 xk ) 2 x( yk 1 yk )

49 Deriving The Bresenham Line Algorithm (cont ) But, xk+1 is the same as xk+1 so: pk 1 pk 2 y 2 x( yk 1 yk ) where yk+1 - yk is either 0 or 1 depending on the sign pk The first decision parameter p0 is evaluated at (x0, y0) is given as: p0 2 y x

0 The Bresenham Line Algorithm 1. BRESENHAM S LINE DRAWING ALGORITHM (for m < 1.0) Input the two line end-points, storing the left end-point in (x0, y0) 2. Plot the point (x0, y0) 3. Calculate the constants Δx, Δy, 2Δy, and (2Δy - 2Δx) and get the first value for the decision parameter as: p0 2 y x 4. At each xk along the line, starting at k = 0, perform the following test. If pk < 0, the next point to plot is (xk+1, yk) and: pk 1 pk 2 y

1 The Bresenham Line Algorithm (cont ) Otherwise, the next point to plot is (xk+1, yk+1) and: pk 1 pk 2 y 2 x. Repeat step 4 (Δx 1) times ACHTUNG! The algorithm and derivation above assumes slopes are less than 1. for other slopes we need to adjust the algorithm slightly

2 Bresenham Example Let s have a go at this Let s plot the line from (20, 10) to (30, 18) First f calculate all the constants: Δx: 10 Δy: 8 2Δy: 16 2Δy - 2Δx: -4 Calculate the initial decision parameter p0: p0 = 2Δy Δx = 6

3 Bresenham Example (cont ) 18 k 17 0 16 1 2 1 3 14 4 13 12 6 11 7 10 8 20 21 22 23 24 2 26 27 28 29 30 9 pk (xk+1,yk+1)

4 Bresenham Exercise Go through the steps the Bresenham line drawing algorithm for a line going from (21,12) to (29,16)

Bresenham Exercise (cont ) 18 k 17 0 16 1 1 2 3 14 4 13 12 6 11 7 10 8 20 21 22 23 24 2 26 27 28 29 30 pk (xk+1,yk+1)

6 Bresenham Line Algorithm Summary The Bresenham line algorithm has the following advantages: An fast incremental algorithm Uses only integer calculations Comparing this to the DDA algorithm, DDA has the following problems: Accumulation round-f errors can make the pixelated line drift away from what was intended The rounding operations and floating point arithmetic involved are time consuming

7 Conclusion In this lecture we took a very brief look at how graphics hardware works Drawing lines to pixel based displays is time consuming so we need good ways to do it The DDA algorithm is pretty good but we can do better Next time we ll like at the Bresenham line algorithm