Chapter 3 Computer Graphics Software

Slides:



Advertisements
Similar presentations
Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Advertisements

COMPUTER GRAPHICS SOFTWARE.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
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.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
Chapter 2: Graphics Programming
OUTPUT PRIMITIVES Screen vs. World coordinate systems ● Objects positions are specified in a Cartesian coordinate system called World Coordinate.
In the name of God Computer Graphics Bastanfard.
Output Primitives Computer Graphics.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
CMPE 466 COMPUTER GRAPHICS
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Models and Architectures Ed Angel Professor of Computer Science, Electrical and Computer.
CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes:
CAP4730: Computational Structures in Computer Graphics Introduction to OpenGL.
Dr. S.M. Malaek Assistant: M. Younesi
Jehee Lee Seoul National University
Scan Conversion Line and Circle
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering API (Application Programmer’s Interface) that is.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
CSC 461: Lecture 41 CSC461: Lecture 4 Introduction to OpenGL Objectives: Development of the OpenGL API OpenGL Architecture -- OpenGL as a state machine.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
OpenGl Graphics Programming. Introduction OpenGL is a low-level graphics library specification. It makes available to the programmer a small set of geomteric.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
Programming with OpenGL Part 1: Background
CSC 461: Lecture 3 1 CSC461 Lecture 3: Models and Architectures  Objectives –Learn the basic design of a graphics system –Introduce pipeline architecture.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Introduction to OpenGL Programming Jian-Liang Lin 2002.
COMPUTER GRAPHICS CSCI 375. What do I need to know?  Familiarity with  Trigonometry  Analytic geometry  Linear algebra  Data structures  OOP.
CGMB214: Introduction to Computer Graphics
Computer Graphics Chapter 6 Andreas Savva. 2 Interactive Graphics Graphics provides one of the most natural means of communicating with a computer. Interactive.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
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.
OUTPUT PRIMITIVES A.Aruna/Faculty of Information technology/SNSCE13/19/2016.
Introduction to Graphics Programming. Graphics API.
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Write Bresenham’s algorithm for generation of line also indicate which raster locations would be chosen by Bresenham’s algorithm when scan converting.
10/10/2006TCSS458A Isabelle Bichindaritz1 Line and Circle Drawing Algorithms.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Rasterization CENG 477 Introduction to Computer Graphics Slides from Steve Marschner, CS4620, 2008 Cornell University.
Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified.
Primitive graphic objects
Introduction to OpenGL (IDE: Eclipse)
- Introduction - Graphics Pipeline
Programming with OpenGL Part 1: Background
Lecture 9 Line Drawing Algorithms (Bresenham’s Line Algorithm)
Coordinate Reference Frames
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Chapter Three Part I Output Primitives CS 380.
The Graphics Rendering Pipeline
CS451Real-time Rendering Pipeline
Models and Architectures
Models and Architectures
Models and Architectures
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Models and Architectures
Models and Architectures
Programming with OpenGL Part 1: Background
Chapter 3 Graphics Output Primitives
CS297 Graphics with Java and OpenGL
Presentation transcript:

Chapter 3 Computer Graphics Software COIS712/ECOM708/COIS800 Computer Graphics – Principles & Systems Advanced Computer Graphics and Multimedia Advanced Computer Graphics & Virtual Simulation Chapter 3 Computer Graphics Software

Outline Graphics Software Packages Introduction to OpenGL Example Program 2

Graphics Software Software packages General Programming Graphics Packages GL (Graphics Library, 1980s) [SGI], OpenGL(1992), GKS (Graphical Kernel System, 1984), PHIGS (Programmer’s Hierarchical Interactive Graphics System), PHIGS+,VRML(superseded by X3D), Direct3D, Java2D/Java3D Special - Purpose Application Packages CAD /CAM, Business, Medicine, Arts ( for Animation: 3ds Max, Maya [Autodesk, originally Alias (formerly Alias|Wavefront)] ) Computer-graphics application programming interface (CG API) A set of graphics functions to let programmers control hardware A software interface between a programming language and the hardware.  E.g.: GL, OpenGL, Direct3D

Functions of Graphics Packages Functions provided by general graphics packages are to create and manipulate pictures Graphics Output Primitives: lines, curves, spheres… Primitive Attributes: color, line styles… Geometric Transformations Viewing Transformations Input Functions: data flow from mouse, joystick, … Control Operations

Algorithms A number of basic algorithms are needed: Advanced: Transformation: convert models/primitives from one coordinate system to another Clipping/Hidden surface removal: remove primitives and part of primitives that are not visible on the display Rasterization: convert a projected screen space primitive to a set of pixels. Advanced: Shading and illumination: simulate the highly realistic lighting effect of a scene. Animation: simulate movement by rendering a sequence of frames. Parallel processing & real time rendering for graphics computation, in particular to large and/or natural environments. Physically based graphics modeling and rendering.

Graphics Rendering Process Rendering purpose: the conversion of a 3D scene into a 2D image 2D Image render Graphics Rendering Pipeline Fixed graphics Pipeline in hardware Programmable pipeline in hardware

Coordinate (坐標) Representations in Graphics Rendering Pipeline Modeling (local/master) coordinates (MC) A separate coordinates reference frame for object. Local coordinate system (2D or 3D) Define the coordinates for each object individually Scale and unit are varied from object to object World coordinates (WC) A scene reference frame where the objects are placed at appropriate locations. Global coordinate system (2D or 3D) Place all defined objects together in a scene within this reference system MC  WC (modeling transformation)

Coordinate Representations in Graphics Rendering Pipeline Viewing and projection coordinates World coordinates positions are transformed though the viewing pipeline to viewing and projection coordinates. (Viewing transformation) Normalized (device) coordinates (NC) Make coordinate independent to any specific output device (device-independent) The scene is stored in normalized coordinates, range from -1 to 1, or 0 to 1. Stage between WC and DC Device (screen) coordinate (DC) Display coordinate system on the output device (e.g, screen) 2D only Platform dependent

Graphics Rendering Pipeline Modeling Viewing Transformations Transformations M1 Model 3D World Scene 3D View Scene V MCS Model Model M2 M3 VCS WCS Rasterization 2D/3D Device Scene P Clip Normalize Projection NDCS DCS SCS 2D Image 9 (From CENG477 notes, 09)

Introduction to OpenGL OpenGL (Open Graphics Library) [developed by Silicon Graphics Inc. (SGI) in 1992, and maintained by OpenGL Architectural Review Board (OpenGL ARB), the group of companies that would maintain and expand the OpenGL specification until 2006; by Khronos Group until now ] Khronos Group (http://www.khronos.org/) [a nonprofit industry consortium creating open standards for the authoring and acceleration of parallel computing, graphics, dynamic media, computer vision and sensor processing on a wide variety of platforms and devices. ]

Introduction to OpenGL OpenGL (Open Graphics Library) A common graphics library which provides functions for drawings and interactive input. A cross-language, cross-platform API No command for performing windowing tasks No command for handling input Accessible via C/C++, Java… http://www.opengl.org

Introduction to OpenGL OpenGL basic (core) library (opengl32.lib | opengl.lib) Over 250 functions, specifying objects and operations needed to produce interactive 3D graphics OpenGL geometric primitives include points, lines and polygons; specific support for triangle and quadrilateral polygons; quadric (defined by quadratic equation) and NURBES (spline) surface. Texture mapping support.  …… From OpenGL 2.0: Programmable shader support

Introduction to OpenGL Related libraries for specific windowing systems GLX: X-Window System (Linux, Unix, OS X) Prefix glx WGL: Microsoft Windows 16 functions Prefix wgl Appel GL (AGL): Apple OpenGL Auxiliary Lib. (glaux.lib) 31 functions, for Windows NT|95, 98, 2000 mainly. Prefix aux

Introduction to OpenGL Related libraries OpenGL Utility (GLU) (glu32.lib | glu.lib) 43 functions Setting up viewing and projection matrices Complex objects Line and polygon approximations Displaying quadrics, B-splines, surface rendering Prefix glu OpenGL Utility Toolkit (GLUT) (glut32.lib) A windowing application programming interface (API) for OpenGL About 30 functions, for interacting with any screen-windowing system, plus quadric curves and surfaces. Prefix glut

OpenGL Utility Toolkit (GLUT) - Written by Mark Kilgard formerly in SGI, now in NVIDIA A window-system independent toolkit NOT a part of OpenGL BUT a windowing API for OpenGL, so no need to know The details of different platform: GLX,WGL, and AGL The details of opening a window and an OpenGL context across operating systems. The details of various input devices, such as keyboard and mouse. Widely used in demonstration programs and literature.

GLUT – Hello World Open a window display function is defined as void (*func) (void) { }; A callback function which is registered by glutDisplayFunc as a routine to invoke when the window need to be redisplayed. Open a window

OpenGL Syntax Functions, symbols, and types Prefix gl glBegin, glClear, glCopyPixels, glPolygonMode GL_2D, GL_POLYGON, GL_BUFFER_BIT GLbyte, GLshort, GLint, GLfloat, GLdouble Prefix gl Each component word in the function name has its first letter capitalized Arguments are assigned symbolic constants specifying parameter names, values of parameters, or a mode. Constants defined with GL_, and underscores separate words. OpenGL defines its own types which correspond to C types GLbyte: signed char; GLshort:short; GLint: int; GLfloat: float…

OpenGL Syntax Functions are always named in the following manner glColor3f(0.0f, 0.0f, 0.0f) gl + actual function name + number of arguments + type of arguments Number of arguments: 2, 3, 4 Type of arguments: s, i, f, d, ub, v… (v indicates it’s a vector/array)

OpenGL Syntax Example This is: 0% Red, 40% Green, 80% Blue  glColor3f(0.0f, 0.4f, 0.8f); This is: 0% Red, 40% Green, 80% Blue  glColor4f(0.1, 0.3, 1.0, 0.5) ; This is: 10% Red, 30% Green, 100% Blue, 50% Opacity  GLfloat color[4] = {0.0, 0.2, 1.0, 0.5}; glColor4fv( color ); It is 0% Red, 20% Green, 100% Blue, 50% Opacity

Introduction to OpenGL Compiling: Header Files gl.h, glu.h, glut.h, glaux.h (windows.h forWGL routines) * If you use GLUT to handle the window-managing operations, only #include <glut.h> needed, gl.h and glu.h have been included. Linking: Dynamic Link Lib opengl32.dll glu32.dll glut32.dll 20

An Example of OpenGL Initialize OpenGL and GLUT Initialize a drawing window Draw a line segment

An Example of OpenGL

An Example of OpenGL glutInitWindowPosition (50, 100); //give the initial location for the top-left corner of the display window glutInitWindowPosition (50, 100); //set the initial pixel width and height of the display window glutCreateWindowSize (400, 300); Figure 3-2 A 400 by 300 display window at position (50, 100) relative to the top-left corner of the video display.

End of Chapter 3

Summary Basic display devices Input devices Graphics software packages Graphics rendering pipeline and coordinate representations OpenGL introduction

Chapter 4 Graphics Output Primitives (Part I) COIS712/ECOM708/COIS800 Computer Graphics – Principles & Systems Advanced Computer Graphics and Multimedia Advanced Computer Graphics & Virtual Simulation Chapter 4 Graphics Output Primitives (Part I)

Outline Definition of graphics output primitives Coordinate Reference Frames OpenGL Point Functions OpenGL Line Functions Implementation Algorithm: Line-Drawing Algorithms (Chapter 6) DDA Bresenham

Definitions One of the first things when creating a computer-generated picture is to describe various picture components of a virtual scene. To provide the shape or structure of the individual objects To provide their coordinate locations in the scene Graphics output primitive: functions in the CG API describe such picture components Geometric primitives: define the geometry of objects Lines,Triangles, Quadrics, Conic sections, Curved surfaces, ……

Coordinate Reference Frames World coordinate system Cartesian coordinates (笛卡爾坐標) A right hand coordinate system Label the axes as X (horizontal) Y (vertical) Z (in 3D) Origin is in the lower left +Y Y Axis (0,0) X Axis +X

Partition Space into Pixels Object information, such as the coordinates, colors, is passed to the viewing routines. Rasterization (光柵化) Map the 3D objects to positions on the 2D screen Scan-conversion methods Converts vector images (primitives such as lines, circles, etc.) into raster images (integer pixel values)  Geometric description  discrete pixel representation 30

Screen Coordinate System Screen: 2D coordinate system (W*H) Quick review: What’s the pixel? Two definitions for screen coordinate system: For hardware processes, such as screen +Y refreshing, the origin is at the top-left corner of the screen y - scan line number; x - column number. Y Axis For software, the origin is at the low-left corner (OpenGL convention) Correspond to the pixel positions in the (0,0) X Axis +X frame buffer 31

“pixel” is a screen spot. How to represent each pixel position by the screen coordinates? Is it a square or a point? From a geometry point of view, a pixel is a point. Q: Where is the pixel P(2,1) on the screen? 3 2 1 1 2 3 4 5

Q: Where is P(2,1) on the screen? A: the center of a pixel located. Therefore, when we think about images, a pixel is a rectangle or the inscribed circle. Q: Where is P(2,1) on the screen? A: the center of a pixel located. 2 1 1 2 3 4

Basic OpenGL Point Structure In OpenGL, to specify a point: glVertex*(); (*) indicates the suffix codes needed  glVertex2i(80, 100), glVertex2f(58.9, 90.3)  glVertex3i(20, 20, -5), glVertex3f(-2.2, 20.9, 20) [ gl + actual function name + number of arguments + type of arguments ] Must put within a ‘glBegin/glEnd’ pair The form of a point position in OpenGL spec.: glBegin(GL_POINTS); glVertex2i(50, 100); glVertex2i(75, 150); glVertex2i(100, 200); glEnd(); Symbolic constant glBegin (GL_POINTS); glVertex* (); glEnd ();

OpenGL Point Functions Some examples of specifying points in OpenGL //Specify the coordinates of points in arrays; then call the OpenGL functions int point1 [] = {50, 100}; int point2 [] = {75, 150}; int point3 [] = {100, 200}; glBegin (GL_POINTS); glVertex2iv (point1); glVertex2iv (point2); glVertex2iv (point3); glEnd (); //Specify the points in 3D world reference frame glBegin (GL_POINTS); glVertex3f (-78.05, 909.72, 14.60); glVertex3f (261.91, -5200.67, 188.33);

OpenGL Point Functions Some examples of specifying points in OpenGL (cont.) // In C++, define a C++ class or structure of a point class wcPt2D { public: GLfloat x, y; }; wcPt2D pointPos; pointPos.x = 120.75; pointPos.y = 45.30; glBegin (GL_POINTS); glVertex2f (pointPos.x, pointPos.y); glEnd ();

OpenGL Line Functions In OpenGL, to specify one or more straight-line segments: glBegin (GL_LINES); glVertex2iv (p1); glVertex2iv (p2); glVertex2iv (p3); glVertex2iv (p4); glVertex2iv (p5); glEnd (); p3 p1 p4 p2 glBegin (GL_LINE_STRIP); glVertex2iv (p1); glVertex2iv (p2); glVertex2iv (p3); glVertex2iv (p4); glVertex2iv (p5); glEnd (); p3 p1 p5 p4 p2

OpenGL Line Functions In OpenGL, to specify one or more straight-line segments (cont.): p3 glBegin (GL_LINE_LOOP); glVertex2iv (p1); glVertex2iv (p2); glVertex2iv (p3); glVertex2iv (p4); glVertex2iv (p5); glEnd (); p1 p5 p4 p2

Implementation Algorithm: Line drawing algorithms Digital Differential Analyzer (DDA) Bresenham’s Line Algorithm

Line-Drawing Algorithms The ideal line Continuous appearance Uniform thickness and brightness Line on a raster monitor Screen: discrete pixels Digitize the line into a set of discrete integer positions approximating the actual line path. 40

Line-Drawing Algorithms (17,8) (2,2) Discretization - converting a continuous signal into discrete elements. Scan conversion line-drawing algorithm: convert the line information into pixel data for display

Line-Drawing Algorithms How to calculate the pixel positions along a straight-line path The line equation (slope-intercept equation): any point (x, y) on the line must follow it. y = m • x + b, where m is the slope (斜率) of the line. b is the intercept on y-axis. If we have two endpoints, (x0, y0) and (xend, yend), then m and b can be calculated as: m = y / x = (yend – y0) / (xend – x0) b = y0 - m . x0

Line-Drawing Algorithms On raster system, the line is approximated by pixels. How good it is, decided by: how to sample a line at discrete positions (step sizes of the calculation in the horizontal (x) and vertical (y) directions) which is the nearest pixel to the line at each sampled position. Two line-drawing algorithms: DDA (Digital Differential Analyzer). (2) Bresenham’s Line Algorithm.

(1) Line drawing – DDA algorithm (DDA) is a scan-conversion line algorithm based on calculating either y or x. Each point is generated from the previous point Take a unit step with one coordinate and calculate the corresponding value for another Slope of a line m = Δy/Δx y = m • x or x = y / m y x Different cases based on the sign of the slope, value of the slope, and the direction of drawing. Slope sign: positive or negative. Slope value: <= 1 or >1. Direction: (left – right) or (right – left) II I III IV 44 Four Quadrants

DDA algorithm y = m • x x = y / m // if |m|<1, | ∆x | =1. // if |m|>1, | ∆y | =1. a low level procedure to store the current color into the frame buffer at (x, y)

Line drawing – DDA algorithm DDA summary Go to starting point Increment x and y values Step size: one unit (in x or y direction) calculating corresponding value for another Round to the closest raster position Merits Relative fast: replace the multiplication by using the increments of x or y directions to step from one pixel to another. Drawbacks Divisions needed to set increment values The use of floating-point arithmetic Rounding operations to an integer

(2) Bresenham’s Line Algorithm An efficient algorithm for line drawing Using only integer addition/subtraction We have a point (xk, yk), then at each sampling step Two possible pixel positions: A (xk+1, yk) and B (xk+1, yk+1) To decide which is closer to the line path B d B dA A The vertical axes show scan-line positions; The horizontal axes identify pixel columns.

Bresenham’s Line Algorithm for |m|<1.0 Input the two line endpoints and store the left one (x0, y0). Plot (x0, y0) to be the first point (set the color for frame buffer position (x0, y0)). Calculate the constants ∆x, ∆y, 2∆y, and 2∆y – 2∆x, and obtain the starting value for the decision parameter as p0 = 2∆ y – ∆x. At each xk along the line, starting at k = 0, perform the following test. If pk < 0, plot (xk+1, yk) and pk+1 = pk Otherwise, y +1 B + 2∆y k dupper y yk dlower A plot (xk+1, yk+1) and pk+1 = pk + 2∆y – 2∆x. 5. Perform step 4 ∆x – 1 times. xk+1

Bresenham’s Line Algorithm Example Note:This Bresenham’s algorithm is used when slope |m|< 1. Example 3-1: Using Bresenham’s Line-Drawing Algorithm, digitize the line with endpoints (20,10) and (30,18). -- y = 18 – 10 = 8 -- x = 30 – 20 = 10 -- m = y / x = 0.8 plot the first point (x0, y0) = (20, 10)  p0 = 2 y – x = 2 • 8 – 10 = 6 , so the next point is (21, 11) The next point is (xk+1, yk+1)

Bresenham’s Line Algorithm Example The successive pixel positions along the line path can be determined from the decision parameter as K Pk (xk +1, yk +1) 6 (21,11) 5 (26,15) 1 2 (22,12) (27,16) -2 (23,12) 7 (28,16) 3 14 (24,13) 8 (29,17) 4 10 (25,14) 9 (30,18) y = 8; x = 10 Pk > 0: Pk+1 = Pk + 2∆y – 2∆x Pk < 0: Pk+1 = Pk + 2∆y 50

Bresenham’s Line Algorithm Example Pixel positions along the line path between endpoints (20, 10) and (30, 18), plotted with Bresenham’s algorithm. (20, 10); (21,11); (22,12); (24,13); (25,14); …; (30,18).

Bresenham’s Line Algorithm for |m|<1.0 Pk < 0: Pk+1 = Pk + 2∆y Pk > 0: Pk+1 = Pk + 2∆y – 2∆x

Setting Frame Buffer Values The final stage of line segment implementation is to set the frame- buffer color values. Suppose: The frame buffer is stored in memory as an addressable array. The frame buffer array is stored row by row (row-major order). Pixel positions are labeled from (0,0) at the lower-left screen corner to (xmax, ymax) at the top-right corner. For a bilevel (one bit per pixel) system, the frame buffer bit address for the pixel at (x, y): addr(x, y) = addr(0, 0) + y•(xmax + 1) + x

Summary OpenGL output primitives functions Line drawing algorithm Point and line Line drawing algorithm DDA Bresenham

END

DDA – case 1 (1st quadrant) Positive slope; left to right: If 0 <= m <= 1 then: xk+1 = xk + 1 yk+1 = yk + m (rounded to the nearest integer corresponding to a screen pixel position in x coordinate) If m > 1 then: xk+1 = xk + 1/m yk+1 = yk + 1 y = m • x x = y / m I Four Quadrants

DDA – case 2 (3rd quadrant) Positive slope; right to left: If 0 < m <= 1 then: xk+1 = xk – 1 yk+1 = yk – m If m > 1 then: xk+1 = xk – 1/m yk+1 = yk – 1 y = m • x x = y / m III Four Quadrants

DDA – case 3 (4th quadrant) Negative slope; left to right: If |m | <= 1 then: xk+1 = xk + 1 yk+1 = yk – |m| If |m | > 1 then: xk+1 = xk + 1/|m| yk+1 = yk – 1 y = m • x x = y / m IV Four Quadrants

DDA – case 4 (2nd quadrant) Negative slope; right to left: If |m | <= 1 then: xk+1 = ? xk – 1 yk+1 =? yk + |m| If |m | > 1 then: xk+1 = ? xk – 1/|m| yk+1 = ? yk + 1 y = m • x x = y / m II Four Quadrants

Bresenham’s Line Algorithm Consider lines with positive slope less than 1.0: At sampling point xk+1, we label vertical pixel separations from the mathematical line path as dlower and dupper. After calculating dlower and dupper we will choose yk or yk+1. y = m (xk + 1) + b dlower = y – yk = m (xk + 1) + b – yk dupper = (yk+1) – y = yk+1 - m (xk + 1) – b B C A An efficient test: dlower – dupper = 2m • xk + 2m – 2yk + 2b – 1 = 2m (xk + 1) – 2yk + 2b – 1 y = m x + b

Bresenham’s Line Algorithm To derive the decision parameter for the kth step: pk dlower – dupper = 2m (xk + 1) – 2yk + 2b – 1  m = y / x ∆x (dlower – dupper ) = 2 ∆ y (xk + 1) – ∆ x (2yk – 2b +1) = 2 ∆ y • x k – 2 ∆ x • yk + c where c = 2 ∆ y + ∆ x (2b – 1) is a constant; and k represents the kth step. pk = ∆x (dlower – dupper ) = 2 ∆ y • x k – 2 ∆ x • yk + c

Bresenham’s Line Algorithm pk = ∆x (dlower – dupper ) = 2 ∆ y • x k – 2 ∆ x • yk + c If pk < 0 (i.e. dlower < dupper)  we plot the pixel A (xk+1, yk) Otherwise we plot the pixel B (xk+1, yk+1) yk+1 y yk B d upper C d lower A xk+1

Bresenham’s Line Algorithm Go on to the next step, we need to decide the pk+1: pk  pk+1 From pk = 2 ∆ y • x k – 2 ∆ x • yk + c, we can get pk+1 = 2 ∆ y • x k+1 – 2 ∆ x • yk+1 + c, where c = 2 ∆ y + ∆ x (2b – 1) is a constant Therefore, xk+1 = xk + 1 pk+1 – pk = 2 ∆ y (x k+1 – x k) – 2 ∆ x (yk+1 – yk ) 0 or 1, depending on the sign of pk  pk+1 = pk +2 ∆y – 2 ∆x (yk+1 – yk) The first parameter p0 = 2 ∆ y – ∆ x hint: from (x0, y0) and m= ∆y/ ∆ x, k = 0. y0+1 y y0 dupper d lower 63 x0+1