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

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

Computer Graphics: Graphics Output Primitives Line Drawing Algorithms

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

Output Primitives Lecture: 4. Lecture 4

Overview of Computer Graphics

Chapter - 2: Geometry and Line Generations

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

Digital Differential Analyzer Bresenhams Line Drawing Algorithm

Rasterization: Geometric Primitives

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

From Ver(ces to Fragments: Rasteriza(on

1 Introduction to Graphics

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

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

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

CS Rasterization. Junqiao Zhao 赵君峤

Computer Graphics: Line Drawing Algorithms

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

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

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

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

Computer Graphics Lecture Notes

MET71 COMPUTER AIDED DESIGN

COMP30019 Graphics and Interaction Scan Converting Polygons and Lines

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

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

UNIT 2 GRAPHIC PRIMITIVES

CSCI 4620/8626. Coordinate Reference Frames

Scan Converting Lines

GRAPHICS OUTPUT PRIMITIVES

Scan Conversion. Drawing Lines Drawing Circles

Chapter 8: Implementation- Clipping and Rasterization

Line Drawing Week 6, Lecture 9

MODULE - 4. e-pg Pathshala

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

Raster Scan Displays. Framebuffer (Black and White)

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

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

Graphics (Output) Primitives. Chapters 3 & 4

Display Technologies: CRTs Raster Displays

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

CS2401 Computer Graphics

Topic #1: Rasterization (Scan Conversion)

Efficient Plotting Algorithm

Computer Graphics D Graphics Algorithms

CPSC / Scan Conversion

Rendering. A simple X program to illustrate rendering

Computer Graphics. - Rasterization - Philipp Slusallek

Painter s HSR Algorithm

Computer Graphics (CS 543) Lecture 10: Rasterization and Antialiasing

Einführung in Visual Computing

UNIT I INTRODUCTION. Survey of Computer Graphics

EF432. Introduction to spagetti and meatballs

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

Unit 2 Output Primitives and their Attributes

Computer Graphics. Chapter 4 Attributes of Graphics Primitives. Somsak Walairacht, Computer Engineering, KMITL 1

Rendering. A simple X program to illustrate rendering

CS 543: Computer Graphics. Rasterization

Chapter 3. Sukhwinder Singh

COMP371 COMPUTER GRAPHICS

CS6504 & Computer Graphics Unit I Page 1

Pipeline implementation II

Computer Graphics. Attributes of Graphics Primitives. Somsak Walairacht, Computer Engineering, KMITL 1

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

Line Drawing. Foundations of Computer Graphics Torsten Möller

(Refer Slide Time: 9:36)

Implementation III. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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

CS 130. Scan Conversion. Raster Graphics

Topics. From vertices to fragments

SNAP Centre Workshop. Graphing Lines

An Improved Algorithm for Scan-converting a Line

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4620/8626 Computer Graphics Spring 2014 Homework Set 1 Suggested Answers

RASTERIZING POLYGONS IN IMAGE SPACE

Scan Converting Circles

Realtime 3D Computer Graphics Virtual Reality

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

UNIT 2 Scan Conversion Techniques and Image Representation Unit-02/Lecture-01

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

1.1 Survey of Computer graphics Computer graphics is the pictorial representation of information using a computer program.

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

2D Image Synthesis. 2D image synthesis. Raster graphics systems. Modeling transformation. Vectorization. u x u y 0. o x o y 1

Computer Graphics. Chapter 3 Computer Graphics Software

FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Computer Graphics Lecture 5

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY CS2401 COMPUTER GRAPHICS QUESTION BANK

Shading Techniques Denbigh Starkey

(Refer Slide Time: 00:03:51)

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

Solution Notes. COMP 151: Terms Test

1. DDA Algorithm(Digital Differential Analyzer)

Rasterization. Rasterization (scan conversion) Digital Differential Analyzer (DDA) Rasterizing a line. Digital Differential Analyzer (DDA)

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

UNIT -8 IMPLEMENTATION

Revision Topic 11: Straight Line Graphs

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

Intro. To Graphing Linear Equations

CS 4300 Computer Graphics. Prof. Harriet Fell Fall 2012 Lecture 5 September 13, 2012

CS335 Fall 2007 Graphics and Multimedia. 2D Drawings: Lines

CS452/552; EE465/505. Clipping & Scan Conversion

Transcription:

Lecture 3 Output Primitives Assuming we have a raster display, a picture is completely specified by: - A set of intensities for the pixel positions in the display. - A set of complex objects, such as trees and terrain or furniture and walls, positioned at specified coordinate locations within the scene. Shapes and colors of the objects can be described internally with pixel arrays or with sets of basic geometric structures, such as straight line segments and polygon color areas figure (4-1). Figure (4-1): Image is set of intensities of pixels or a set of complex objects The scene is then displayed either by: - Loading the pixel arrays into the frame buffer. - Scan converting the basic geometric-structure specifications into pixel patterns. Typically, graphics programming packages provide functions to describe a scene in terms of these basic geometric structures, referred to as output primitives, and to group sets of output primitives into more complex structures. Each output primitive is specified with input coordinate data and other information about the way that objects is to be displayed. Points, straight line segments, circles, other conic sections, quadric surfaces, spline curves and surfaces, polygon color areas, and character strings are the most geometric components of pictures. We begin our discussion of picture-generation procedures by examining device-level algorithms for displaying two dimensional output primitives. Dr. Ayman Elshenawy Elsefy Page 1

Point Point plotting is accomplished by converting a single coordinate position furnished by an application program into appropriate operations for the output device in use. - In CRT monitor, the electron beam is turned on to illuminate the screen phosphor at the selected location. - How the electron beam is positioned depends on the display technology. o A random-scan (vector) system stores point-plotting instructions in the display list, and coordinate values in these instructions are converted to deflection voltages that position the electron beam at the screen locations to be plotted during each refresh cycle. o For a black and white raster system, a point is plotted by setting the bit value corresponding to a specified screen position within the frame buffer to 1. Then, as the electron beam sweeps across each horizontal scan line, it plots a point. o For RGB Raster system, the frame buffer is loaded with the color codes for the intensities that are to be displayed at the screen pixel positions. Figure (4-2) shows how a point is plotted in CRT Display devices. Figure (4-2): a point is plotted in CRT Display devices. Line Digital devices display a straight line segment by plotting discrete points between the start point and the end point. Discrete coordinate positions along the line path are calculated from the equation of the line. Dr. Ayman Elshenawy Elsefy Page 2

For a raster video display, the line color (intensity) is then loaded into the frame buffer at the corresponding pixel coordinates. After reading from the frame buffer, the video controller then "plots" the screen pixels. Screen locations (coordinates) are referenced with integer values, so plotted positions may only approximate actual line positions between two specified endpoints. A computed line position of (10.48, 20.51) [Mathematical Point], for example, would be converted to pixel position (10, 21). Thus rounding of coordinate values to integer s causes lines to be displayed with a stair-step appearance, as represented in Fig 4-3. The characteristic stair-step shape of raster lines is particularly noticeable on systems with low resolution, and we can improve their appearance somewhat by displaying them on high-resolution systems. More effective techniques for smoothing raster lines are based on adjusting pixel intensities along the line paths. The pixel positions are referenced according to scan-line number and column number (pixel position across a scan line). This addressing scheme is illustrated in Fig. 4-4. Scan lines are numbered consecutively from 0, starting at the top of the screen; and pixel columns are numbered from 0, left to right across each scan line. Figure (4-3): Stair-step appearance Figure (4-4): Pixel positions referenced by scan line number and column number One of 2 N intensities or colors is associated with each pixel, where N is the number of bits per pixel. Gray-scale typically has one byte per pixel, for 2 8 = 256 intensities. Dr. Ayman Elshenawy Elsefy Page 3

Color often requires one byte per channel, with three color channels per pixel: red, green, and blue. Color data is stored in a frame buffer. This is sometimes called an image map or bitmap. To load a specified color into the frame buffer at a position corresponding to column x along scan line y, we will assume we have available a low-level procedure of the form setpixel(x, y, color): Sets the pixel at position (x, y) to the given color. We sometimes will also want to be able to retrieve the current frame buffer intensity setting for a specified location. We accomplish this with the low-level function getpixel(x, y) : Gets the color at the pixel at position (x, y). Line drawing algorithms The equation of straight line can be computed using the following equations, y = m x + b (1) m = y x = y 2 y 1 x 2 x 1 (2) And b = y 1 m x 1 (3) Figure (4-4): Line path between endpoint positions (x 1, y 1 ) and (x 2, y 2 ) With m representing the slope of the line and b as the y intercept. Given that the two endpoints of a line segment are specified at positions (x 1, y 1 ) and (x 2, y 2 ) as shown in Fig. 4-4, we can determine values for the slope m and y intercept b with the following calculations: 1. Brute Force Algorithm The starting point is the equation of a straight line: Dr. Ayman Elshenawy Elsefy Page 4

y = mx + c where m is the gradient of the line: m = y x = y from y to x from x to and c is its intercept of the y-axis c = y from m x from Assume that the endpoints of the line are known, For any value of x we can compute y. A for loop can be used to iterate through all the values of x between xfrom and xto and for each of these, the corresponding y value calculated. figure 4-5 Enumerating the values of x Disadvantage of brute force method a. Gaps started to be appeared The results of running such an algorithm look like Figure 4-5. Unfortunately, if we pick coordinates for the end points such that the line is steep (in fact when its gradient m is >1 the results look something like Figure: Dr. Ayman Elshenawy Elsefy Page 5

Figure 4-6. Brute force algorithm with steep gradient The solution to the gaps problem is to always use the most rapidly changing variable (x or y) as the index to the loop (Figure 31). i.e. When the gradient (m) >1 - use y as the control variable in the loop and make x the subject of the equation: X = (y c)/m Figure 4-7-Comparison of x & y values for different gradients b. It requires floating point There is a second problem with the brute force approach: It requires floating point arithmetic which is slow when compared with using integer only arithmetic. An approach which used solely integers would result in a much quicker algorithm. It can be solved by removing the effect of the floating points 2. Bresenham s Algorithm One such integer only algorithm is Bresenham s. Start by considering the simple case where 0 < m <1 Consider, as before, iterating the x values from left to right. If the pixel at (x i, y i ) has been plotted, then the next one MUST be either (x i+1, y i ) or (x i+1, y i+1 ). Why? - Because of the Dr. Ayman Elshenawy Elsefy Page 6

gradient - it can t go up more than one step in the y direction for one step in the x (m<1) and it can t go down at all (0<=m). How can we work out which pixel to plot? Figure 4-8. The basis of Bresenham s algorithm - By calculating the difference between the true mathematical value of y at x i+1 (we ll call that y real ) and the y values represented by the pixels. Δy 1 and Δy 0 present these distances and the decision on which pixel to plot can be made by following the following pseudo code To illustrate Bresenham s approach, we first consider the scan conversion process for lines with positive slope less than 1. Pixel positions along a line path are then determined by sampling at unit x intervals. Starting from the left endpoint (x 0, y 0 ) of a given line, we step to each successive column (x position) and plot the pixel whose scan-line y value is closest to the line path. Figure 4-7(a) demonstrates the K th step in this process. Assuming we have determined that the pixel at (x i, y i ) is to be displayed, we next need to decide which pixel to plot in column x i + 1,. Our choices are the pixels at positions (x i+1, y i ) and (x i+1, y i+1 ). At sampling position x i+1, we label vertical pixel separations from the mathematical line path as d 1, and d 2. They coordinate on the mathematical line at pixel column position x k+1 is calculated as : y real = m(x i+1 ) + c Dr. Ayman Elshenawy Elsefy Page 7

d 1 = y real y i = m(x i+1 ) + c y i d 2 = y i+1 y real = y i+1 m(x i+1 ) + c The difference between these two separations is d 1 d 2 = 2m(x i+1 ) 2y i + 2c 1 A decision parameter p k for the i th step in the line algorithm can be obtained by rearranging the above Eq. so that it involves only integer calculations. We accomplish this by substituting m = y/ x, where y and x are the vertical and horizontal separations of the endpoint positions, and defining: p k = x(d 1 d 2 ) = 2 yx i + 2 y 2 x y i + 2 x b x p i = x(d 1 d 2 ) = 2 yx i 2 x y i R where R = 2 y + 2 x (b 1) The sign of p i, is the same as the sign of d 1 d 2, since x > 0 for our example. Parameter R is constant and has the value 2 y + 2 x (b 1), which is independent of pixel position and will be eliminated in the recursive calculations for p i. If the pixel at y i is closer to the line path than the pixel at y i +1 (that is, d 1 < d 2 ), then decision parameter p i is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixel. Coordinate changes along the line occur in unit steps in either the x or y directions. Therefore, we can obtain the values of successive decision parameters using incremental integer calculations. At step k + 1, the decision parameter is evaluated from Eq. as p i+1 = 2 yx i+1 2 x y i+1 R Subtracting p i+1 p i p i+1 p i = 2 y(x i+1 x i ) 2 x(y i+1 y i ) But x i+1 = x i + 1 p i+1 = p i + 2 y 2 x(y i+1 y i ) where the term y i+1 y i is either 0 or 1, depending on the sign of parameter p i. This recursive calculation of decision parameters is performed at each integer x position, starting at the left coordinate endpoint of the line. The first parameter, p i is evaluated from at the starting pixel position (x 0, y 0 ) and with m evaluated as y x : Dr. Ayman Elshenawy Elsefy Page 8

p 0 = 2 y x We can summarize Bresenham s line drawing for a line with a positive slope less than 1 in the following listed steps. The constants 2 y and 2 y 2 x are calculated once for each line to be scan converted, so the arithmetic involves only integer addition and subtraction of these two constants. Bresenham's Line-Drawing Algorithm for m < 1 1. Input the two line endpoints and store the left endpoint in (x 0, y 0 ) 2. Load (x 0, y 0 ) into the frame buffer; that is, plot the first point. 3. Calculate constants Δx, Δy, 2Δy, and 2Δy 2Δx, and obtain the starting value for the decision parameter as p 0 = 2 y x 4. At each x k along the line, starting at k = 0, perform the following test: If p k < 0, the next point to plot is (x k+1, y k ) and p k+1 = p k + 2 y Otherwise, the next point to plot is (x k+1, y k+1 ) and p k+1 = p k + 2 y 2 x 5. Repeat step 4 Δx times. An implementation of Bresenham s line drawing for slopes in the range m < 1 is given in the following procedure. Endpoint pixel positions for the line are passed to this procedure, and pixels are plotted from the left endpoint to the right endpoint. #Include <device.h> void linebresenham (int xa, int ya, int xb, int yb) { int dx = abs ( xa - xb ), dy = abs (ya - yb); int p = 2 * dy - d x ; int twody = 2 * dy, twodydx = 2 * (dy - Ax); int x, y, xend: /* Determine which point to use a s start, which as end * / if (xa > xb ) { x = xb; Y = yb; xend = xa; } else{ x = xa; Y = ya; xend = xb; } setpixel (x, y); while (x < xend) { x++; if (p < 0) { p += twody; } else { y++; p+= twodydx; } setpixel ( x, y); Dr. Ayman Elshenawy Elsefy Page 9

} } 3. Digital Differential Analyzer (DDA) algorithm This algorithm samples the line at unit interval in one coordinate and determines the corresponding integer values nearest the line path for other coordinates. The equation of the line is = mx + c, m = y2 y1, For any interval x, the corresponding interval is given by y = m x Case 1: m < 1 x2 x1 The sampling is done using x axis, x = 1 and y = m then: x i+1 = x i + 1 Then calculate y = m y i+1 = y i + y = y i + m Case 1: m > 1 The sampling is done using y axis, y = 1 and x = 1/m then y i+1 = y i + 1 Then calculate x = 1/m Dr. Ayman Elshenawy Elsefy Page 10

Algorithm x i+1 = x i + x = x i + ( 1 m ) 1. START 2. Get the values of the starting and ending co-ordinates i.e., (x 1, y 1 ) and (x 2, y 2 ). 3. Find the value of slope m m = y = y 2 y 1 x x 2 x 1 4. If m 1 then Δx = 1,Δy = mδx x k + 1=x k + 1, y k + 1 = y k + m 5. If m 1 then Δy = 1, Δx = Δy/m x k + 1 = x k + 1/m, y k + 1 = y k + 1 6. STOP /**** Program to Draw a Line using DDA Algorithm ****/ #include <stdio.h> #include <dos.h> #include <graphics.h> void linedda(int, int, int, int); void main() { int x1, y1, xn, yn; int gd = DETECT, gm; initgraph(&gd, &gm, ""); printf("enter the starting coordinates of line: "); scanf("%d %d", &x1, &y1); printf("enter the ending coordinates of line: "); scanf("%d %d", &xn, &yn); linedda(x1, y1, xn, yn); getch(); } void linedda(int x1, int y1, int xn, int yn) { int dx, dy, m, i; m = (yn-y1)/(xn-x1); for (i=x1; i<=xn; i++) { if (m <= 1) { dx = 1; dy = m * dx; } else { dy = 1; dx = dy / m; } x1 = x1 + dx; y1 = y1 + dy; putpixel(x1, y1, RED); delay(20); } } Dr. Ayman Elshenawy Elsefy Page 11

Figure 4-8 Comments: 1. It is faster to calculate pixel position. 2. Due to propagation of round off errors due to successive addition the calculated pixel may shift among from the true line path. Sheet 3 1. Describe how a point can be represented in display device? 2. Define a graphical line and how it can be displayed on a specific display device? 3. Explain the Basic concept of drawing a line using the brute force algorithm? 4. What are the main disadvantages of the brute force algorithm and how can we solve it? 5. For the Bresenham's line drawing algorithm : a. Explain the basic concept of the Presenham algorithm? b. Write the Algorithm c. Write a c++ implementation for this algorithm. 6. For the DDA line drawing algorithm : a. Explain the basic concept. b. Write the Algorithm c. Write a c++ implementation for this algorithm. 7. Write a Bresenham's line algorithm for line where m 1. Digitize a line with end points (20, 10) and (30, 18). Dr. Ayman Elshenawy Elsefy Page 12