CSC Computer Graphics

Similar documents
GRAPHICS OUTPUT PRIMITIVES

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

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

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

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

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

Rendering. A simple X program to illustrate rendering

Computer Graphics. Modelling in 2D. 2D primitives. Lines and Polylines. OpenGL polygon primitives. Special polygons

Computer Graphics. Lecture 3 Graphics Output Primitives. Somsak Walairacht, Computer Engineering, KMITL

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

Prof. Feng Liu. Fall /25/2018

Rendering. A simple X program to illustrate rendering

Computer Graphics. Computer Graphics. Lecture 3 Line & Circle Drawing

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

CPSC / Scan Conversion

Raster Displays and Scan Conversion. Computer Graphics, CSCD18 Fall 2008 Instructor: Leonid Sigal

Rasterization: Geometric Primitives

(Refer Slide Time: 00:03:51)

Rendering. Basic Math Review. Rasterizing Lines and Polygons Hidden Surface Remove Multi-pass Rendering with Accumulation Buffers.

Overview of Computer Graphics

Scan Conversion. Lines and Circles

Line Drawing Week 6, Lecture 9

12.4 The Ellipse. Standard Form of an Ellipse Centered at (0, 0) (0, b) (0, -b) center

Computer Graphics: Line Drawing Algorithms

Module 2, Section 2 Graphs of Trigonometric Functions

From Ver(ces to Fragments: Rasteriza(on

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

MODULE - 4. e-pg Pathshala

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

Graphics Output Primitives

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

Scan Converting Lines

Realtime 3D Computer Graphics Virtual Reality

Polar Functions Polar coordinates

UNIT 2 GRAPHIC PRIMITIVES

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

UNIT -8 IMPLEMENTATION

Efficient Plotting Algorithm

Computer Graphics Lecture Notes

Raster Graphics Algorithms

THE INVERSE GRAPH. Finding the equation of the inverse. What is a function? LESSON

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

Output Primitives Lecture: 4. Lecture 4

Section 4.2 Graphing Lines

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

Graphs, Linear Equations, and Functions

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

CS 130. Scan Conversion. Raster Graphics

Chapter - 2: Geometry and Line Generations

Lines and Their Slopes

1 Introduction to Graphics

20 Calculus and Structures

Computer Graphics D Graphics Algorithms

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

Implicit differentiation

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

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

Chapter 8: Implementation- Clipping and Rasterization

Two Dimensional Viewing

a 2 + 2a - 6 r r 2 To draw quadratic graphs, we shall be using the method we used for drawing the straight line graphs.

2.1 The ReCTAngUlAR COORdInATe SySTemS And graphs

Computer Graphics D Graphics Algorithms

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

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

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

of Straight Lines 1. The straight line with gradient 3 which passes through the point,2

CSCI 4620/8626. Coordinate Reference Frames

A New Line Drawing Algorithm Based on Sample Rate Conversion

(0, 4) Figure 12. x + 3. d = c. = b. Figure 13

Scan Conversion. Drawing Lines Drawing Circles

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

An Improved Algorithm for Scan-converting a Line

Functions: The domain and range

Functions Review Packet from November Questions. 1. The diagrams below show the graphs of two functions, y = f(x), and y = g(x). y y

8.5 Quadratic Functions and Their Graphs

PARAMETRIC EQUATIONS AND POLAR COORDINATES

LESSON 3.1 INTRODUCTION TO GRAPHING

Line Drawing. Foundations of Computer Graphics Torsten Möller

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

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

Graphics (Output) Primitives. Chapters 3 & 4

Unit 2 Output Primitives and their Attributes

Digital Differential Analyzer Bresenhams Line Drawing Algorithm

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

STRAND G: Relations, Functions and Graphs

4.7 INVERSE TRIGONOMETRIC FUNCTIONS

Getting a New Perspective

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

Double Integrals in Polar Coordinates

Chapter 1 Notes, Calculus I with Precalculus 3e Larson/Edwards

Name: Thus, y-intercept is (0,40) (d) y-intercept: Set x = 0: Cover the x term with your finger: 2x + 6y = 240 Solve that equation: 6y = 24 y = 4

CS 543: Computer Graphics. Rasterization

Equations and Inequalities

Raster Scan Displays. Framebuffer (Black and White)

Integrating ICT into mathematics at KS4&5

Transformations of Functions. 1. Shifting, reflecting, and stretching graphs Symmetry of functions and equations

Using Characteristics of a Quadratic Function to Describe Its Graph. The graphs of quadratic functions can be described using key characteristics:

Introduction to Trigonometric Functions. Peggy Adamson and Jackie Nicholas

Geometry. Origin of Analytic Geometry. Slide 1 / 202 Slide 2 / 202. Slide 4 / 202. Slide 3 / 202. Slide 5 / 202. Slide 6 / 202.

Parametric Equations: Motion in a Plane Notes for Section 6.3. are parametric equations for the curve.

Transcription:

7//7 CSC. Computer Graphics Lecture Kasun@dscs.sjp.ac.l Department of Computer Science Universit of Sri Jaewardanepura Line drawing algorithms DDA Midpoint (Bresenham s) Algorithm Circle drawing algorithms Simple Circle Algorithm Mid-Point Circle Algorithm Ellipse drawing algorithms Simple Ellipse Algorithm Midpoint Ellipse Algorithm 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP A line segment in a scene is defined b the coordinate positions of the line end-points (7, ) (, ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP But what happens when we tr to draw this on a piel based displa? Considerations to eep in mind: The line has to loo good Avoid jaggies It has to be lightening fast! Millions of lines need to be drawn in a tpical scene. How do we choose which piels to turn on? 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6

7//7 Lines must create visuall satisfactor images. Lines should appear straight Lines should terminate accuratel Lines should have constant densit Line densit should be independent of line length and angle. Ideall, the following properties should be considered Smooth Continuous Pass through specified points Uniform brightness Efficient 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 There are three possible choices. Eplicit: = f() = m ( - ) + where m = d/d Parametric: = f(t), = f(t) = + t( - ), t in [,] = + t( - ) Implicit: f(, ) = F(,) = (-)d - (-)d if F(,) = then (,) is on line F(,) > then (,) is below line F(,) < then (,) is above line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 DrawLine(int, int, int, int, int color) float ; int ; for (=; <=; ++) = + (-)*(-)/(-) WritePiel(, Round(), color ); 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP DrawLine(int,int,int,int, int color) float m,; int d,d,; d = - ; d = - ; m = d/d; = +.; for (=; <=; ++) WritePiel(, Floor(), color ); = + m; A Better Implementation 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Advantages over Algorithm Eliminates multiplication thus improves speed Disadvantages Round-off error builds up Get piel drift Rounding and floating point arithmetic still time consuming Wors well onl for m < Need to loop in for m > Need to handle special cases 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP

7//7 Let s quicl review the equations involved in drawing lines end end Slope-intercept line equation: m c where: m end end c m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The slope of a line (m) is defined b its start and end coordinates The diagram below shows some eamples of lines and their slopes m = - / m = - / m = m = - m = - m = - m = m = m = m = m = / m = / m = 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP We could simpl wor out the corresponding coordinate for each unit coordinate Let s consider the following eample: (7, ) (, ) 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 (, ) 6 7 (7, ) First wor out m and b: m 7 c Now for each value wor out the value: ( ) ( ) ( ) ( 6) 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 Now just round off the results and turn on these piels to draw our line 7 6 6 7 8 ( ) ( ) ( ) ( 6) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8

7//7 However, this approach is just wa too slow In particular loo out for: The equation = m + c requires the multiplication of m b Rounding off the resulting coordinates We need a faster solution In the previous eample we chose to solve the parametric line equation to give us the coordinate for each unit coordinate What if we had done it the other wa around? Where: c m and m end end c m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Leaving out the details this gives us: ( ) ( ) We can see easil that 7 this line doesn t loo 6 ver good! We choose which wa to wor out the line piels based on the slope of the line 6 7 8 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP If the slope of a line is between - and then we wor out the coordinates for a line based on it s unit coordinates Otherwise we do the opposite coordinates are computed based on unit coordinates m = - / m = - / m = m = - m = - m = - m = m = m = m = m = / m = / m = 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Given points P = (, ) and P = (, ) = + t( - ) = + t( - ) t is called the parameter. When t = we get (, ) t = we get (, ) As < t < we get all the other points on the line segment between (, ) and (, ). 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP

7//7 The digital differential analzer (DDA) algorithm taes an incremental approach in order to speed up scan conversion Simpl calculate + based on The original differential analzer w a s a p h s i c a l m a c h i n e developed b Vannevar Bush at MIT in the 9 s in order to solve ordina r differen tia l e q u a t i o n s. M o r e i n f o r m a t i o n h e r e. 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Digital differential analser Y=m+c For m< =m For m> = /m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 Procedure DDA(X,Y,X,Y :Integer); Var Length, I :Integer; X,Y,Xinc,Yinc :Real; Begin Length := ABS(X - X); If ABS(Y -Y) > Length Then Length := ABS(Y-Y); Xinc := (X - X)/Length; Yinc := (Y -Y)/Length; X := X; Y := Y; For I := To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End For End; DDA 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 Compute which piels should be turned on to represent the line from (6,9) to (,). Length := Ma of (ABS(-6), ABS(-9)) = Xinc := Yinc :=.6 Values computed are: (6,9), (7,9.6), (8,.), (9,.8), (,.), (,) 9 6 7 8 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 Consider the list of points that we determined for the line in our previous eample: (, ), (, / ), (, / ), (, / ), (6, / ), (7, ) Notice that as the coordinates go up b one, the coordinates simpl go up b the slope of the line This is the e insight in the DDA algorithm 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP

7//7 When the slope of the line is between - and begin at the first point in the line and, b incrementing the coordinate b, calculate the corresponding coordinates as follows: m When the slope is outside these limits, increment the coordinate b and calculate the corresponding coordinates as follows: m 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Again the values calculated b the equations used b the DDA algorithm must be rounded to match piel values ( +, round( +m)) (, ) ( +, +m) (, round( )) (round( + / m ), +) (, ) ( + / m, +) (round( ), ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP Let s tr out the following eamples: (, 7) 7 (7, ) (, ) (, ) 7 It is much faster than our previous attempt. No an multiplications involved. However, there are still two big issues: Accumulation of round-off errors can mae the pielated line drift awa from what was intended The rounding operations and floating point arithmetic involved are time consuming 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP It is another incremental scan conversion algorithm. The big advantage of this algorithm is that it uses onl integer calculations J a c B r e s e n h a m wored for 7 ears at IBM before entering academia. Bresenham developed his famous algorithms at IBM in t h e e a r l 96s 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 6

7//7 The midpoint algorithm is even better than the above algorithm in that it uses onl integer calculations. It treats line drawing as a sequence of decisions. For each piel that is drawn the net piel will be either N or NE, as shown below. It uses the implicit definition of the line, F(,) =. The N/NE decisions are made as follows. d = F( p +, p +.) if d < line below midpoint choose E if d > line above midpoint choose NE if E is chosen d new = F( p +, p +.) d new - d old = F( p +, p +.) - F( p +, p +.) Delta = d new -d old = d 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 If NE is chosen d new = F( p +, p +.) Delta = d-d Initialization d start = F( +, +.) = ( +- )d - ( +.- )d = d-d/ Integer onl algorithm F (,) = F(,) ; d = d d start = d - d Delta = Delta DrawLine(int, int, int, int, int color) int d, d, d, ince, incne,, ; d = - ; d = - ; d = *d - d; ince = *d; incne = *(d - d); = ; for (=; <=; ++) WritePiel(,, color); if (d>) d = d + incne; = + ; else d = d + ince; 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The initial value for the decision variable, d, ma be calculated directl from the formula at point (,). d = f( +, + /) = b() - a(/) = b - a/ Therefore, the algorithm for a line from (,) to (a,b) in the first octant is: Note: The onl non-integer value is a/. If we then multipl b to get d' = d, we can do all integer arithmetic using onl the operations +, -, and left-shift. The algorithm still wors since we onl care about the sign, not the value of d. := ; := ; d := b - a/; For i := to a do Plot(,); If d >= Then := + ; := + ; d := d + b a Else := + ; d := d + b End End 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP To generalize lines with arbitrar slopes Consider smmetr between various octants and quadrants For m >, interchange roles of and, that is step in direction, and decide whether the value is above or below the line. If m >, and right endpoint is the first point, both and decrease. To ensure uniqueness, independent of direction, alwas choose upper (or lower) point if the line go through the mid-point. Handle special cases without invoing the algorithm: horizontal, vertical and diagonal lines 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7

7//7 Generalize the algorithm to wor for lines beginning at points other than (,) b giving and the proper initial values. Note: This algorithm onl wors for lines with slopes between and Begin Bresenham for lines with slope between and a := ABS(end - start); b := ABS(end - start); d := *b - a; Incr := *(b-a); Incr := *b; If start > end Then := end; := end Else := start; := start End For I := to a Do Plot(,); := + ; If d >= Then := + ; d := d + incr Else d := d + incr End End For Loop End Bresenham Move across the ais in unit intervals and at each step choose between two different coordinates (, ) ( +, +) ( +, ) For eample, from position (, ) we have to choose between (, ) and (, ) We would lie the point that is closer to the original line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP At sample position + the vertical separations from the mathematical line are labelled d upper and d lower + The coordinate on the mathematical line at + is: m( ) b d upper + d lower So, d upper and d lower are given as follows: and: d d lower upper m( ) b ( ) m( ) b We can use these to mae a simple decision about which piel is closer to the mathematical line 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Eplicit: = f() R Usuall, we draw a quarter circle b incrementing from to R in unit steps and solving for + for each step. Parametric: Rcos Rsin - b stepping the angle from to 9 - avoids large gaps but still insufficient. Implicit: f() = + -R If f(,) = then it is on the circle. f(,) > then it is outside the circle. f(,) < then it is inside the circle. 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 8

7//7 The first thing we can notice to mae our circle drawing algorithm more efficient is that circles centred at (, ) have eight-wa smmetr (-, ) (, ) (-, ) (, ) (-, -) R (, -) (-, -) (, -) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP The equation for a circle is: r where r is the radius of the circle So, we can write a simple circle drawing algorithm b solving the equation for at unit intervals using: r 9 9 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP This is not a brilliant solution! It has large gaps where the slope approaches the vertical. The calculations are not ver efficient The square (multipl) operations The square root operation tr reall hard to avoid these! We need a more efficient, more accurate solution X=r*cosθ+ c Y=r*sinθ+ c º θ 6º Or θ 6.8(*π) Problem: Deciding the increment in θ Cos, sin calculations 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9

7//7 Similarl to the case with lines, there is an incremental algorithm for drawing circles the mid-point circle algorithm In the mid-point circle algorithm we use eight-wa smmetr so onl ever calculate the points for the top right eighth of a circle, and then use smmetr to get the rest of the points The mid-point circle a l g o r i t h m w a s developed b Jac Bresenham, who we heard about earlier. Bresenham s patent for the algorithm can USJP be v i e w e d here. 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of d old = F( p +, p +.) If d old <, E is chosen d new = F( p +, p -.) = d old +( p +) Delta E = p + If d old >=, SE is chosen d new = F( p +, p -.) = d old +( p - p +) Delta SE = p - p + Initialization d init = / R = - R 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 7 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 8 6 M 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 9 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6

7//7 M 6 Assume that we have (, ) just plotted point (, ) The net point is a choice between ( +, ) and ( +, -) We would lie to choose the point that is nearest to the actual circle So how do we mae this choice? ( +, ) ( +, -) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 Let s re-jig the equation of the circle slightl to give us: (, ) r f circ The equation evaluates as follows: f circ, if (, ) is inside the circle boundar (, ), if (, ) is on thecircle boundar, if (, ) is outsidethecircle boundar B evaluating this function at the midpoint between the candidate piels we can mae our decision Assuming we have just plotted the piel at (, ) so we need to choose between ( +, ) and ( +, -) Our decision variable can be defined as: p f circ ( (, ) ) ( ) If p < the midpoint is inside the circle and and the piel at is closer to the circle Otherwise the midpoint is outside and - is closer r 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 To ensure things are as efficient as possible we can do all of our calculations incrementall First consider: or: p f circ [(, r where + is either or - depending on the sign of p ) ] p p ( ) ( ) ( ) 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 6 The first decision variable is given as: p fcirc r (, ) r ( ) r r Then if p < then the net decision variable is given as: p p If p > then the decision variable is: p p 7//7 Kasun@dscs.sjp.ac.l - Facult of Applied Sciences of USJP 66