Computer Graphics Drawing Line.

Slides:



Advertisements
Similar presentations
COMP 175 | COMPUTER GRAPHICS Remco Chang1/6103b – Shapes Lecture 03b: Shapes COMP 175: Computer Graphics February 3, 2015.
Advertisements

CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1 Emmanuel Agu.
Section 3-1 to 3-2, 3-5 Drawing Lines Some of the material in these slides may have been adapted from university of Virginia, MIT, and Åbo Akademi University.
Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
+ CPCS 391 Computer Graphics 1 Instructor: Dr. Sahar Shabanah Lecture 3.
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.
Computer Graphics Lecture 3 Line & Circle Drawing.
30/9/2008Lecture 21 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester ITGD3107 University of Palestine.
Lecture 2 Line & Circle Drawing
The lines of this object appear continuous However, they are made of pixels 2April 13, 2015.
Komputer Grafik 2 (AK045206) Scan Conversion 1/31 Scan Conversion.
CPCS 391 Computer Graphics 1 Lecture 5: Polygon Filling
Bresenham’s Midpoint Algorithm CS5600 Computer Graphics Rich Riesenfeld Spring 2006 Lecture Set 1.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 50 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 45 Computer Graphics Implementation I.
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Scan Conversion CS123 1 of 44Scan Conversion - 10/14/2014.
Rasterization Foley, Ch: 3. Pixels are represented as disjoint circles centered on uniform grid Each (x,y) of the grid has a pixel Y X (1,1) (1,2) (0,0)
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Dr. Scott Schaefer Scan Conversion of Lines. 2/78 Displays – Cathode Ray Tube.
1/1/20001 Topic >>>> Scan Conversion CSE Computer Graphics.
Dr. S.M. Malaek Assistant: M. Younesi
Jehee Lee Seoul National University
Graphics Primitives: line. Pixel Position
WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition.
Scan Conversion Line and Circle
C O M P U T E R G R A P H I C S Stuff Jian Chen January 15, 2010 Transformations 1/10 Describing Shape By Andries van Dam.
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
Graphics Graphics & Graphical Programming Lecture 14 - Lines & Circles.
Introduction Computer Graphics & Its application Types of computer graphics Graphic display : random Scan & Raster Scan display Frame buffer and video.
CGMB214: Introduction to Computer Graphics
Computer Graphics Drawing Line. Lines and Polylines Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon.
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2013 Tamara Munzner Rasterization.
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
1 CSCE 441 Lecture 2: Scan Conversion of Lines Jinxiang Chai.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
Scan Conversion.
Lecture 13: Raster Graphics and Scan Conversion
OUTPUT PRIMITIVES A.Aruna/Faculty of Information technology/SNSCE13/19/2016.
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Computer Graphics Inf4/MSc Computer Graphics Lecture 4 Line & Circle Drawing.
10/10/2006TCSS458A Isabelle Bichindaritz1 Line and Circle Drawing Algorithms.
Computer Graphics Lecture 06 Circle Drawing Techniques Taqdees A. Siddiqi
Rasterization CENG 477 Introduction to Computer Graphics Slides from Steve Marschner, CS4620, 2008 Cornell University.
Rasterization, or “What is glBegin(GL_LINES) really doing?” Course web page: February 23, 2012  Lecture 4.
Line Drawing Algorithms 1. A line in Computer graphics is a portion of straight line that extends indefinitely in opposite direction. 2. It is defined.
Objectives Understand Bresenhams line drawing algorithm. Apply the algorithm to plot a line with the end points specified.
Primitive graphic objects
Scan Conversion or Rasterization
CSCE 441 Lecture 2: Scan Conversion of Lines
(c) 2002 University of Wisconsin, CS559
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Scan Conversion or Rasterization
Computer Graphics Implementation I.
Chapter Three Part I Output Primitives CS 380.
Constructing Objects in Computer Graphics By Andries van Dam©
Scan Conversion of Lines
Fitting Curve Models to Edges
CSCE 441 Lecture 2: Scan Conversion of Lines
© University of Wisconsin, CS559 Fall 2004
3D Rendering Pipeline Hidden Surface Removal 3D Primitives
The Coordinate Plane; Graphs of Equations; Circles
Rasterizing Lines 1 Lecture 32 Mon, Nov 12, 2007.
Introduction to Computer Graphics
Edited from The Rasterization Problem: Idealized Primitives Map to Discrete Display Space Edited from.
Scan Conversion (From geometry to pixels)
Chapter 3 Graphics Output Primitives
Where We Stand At this point we know how to: Next thing:
Presentation transcript:

Computer Graphics Drawing Line

2D Object Lines and Polylines Convex vs. Concave Polygons Polylines: lines drawn between ordered points Same first and last point make closed polyline or polygon If it does not intersect itself, called simple polygon Convex vs. Concave Polygons Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon. Concave: Not convex: some two points in the polygon are joined by a line not fully contained in the polygon.

2D Object Special polygons triangle square rectangle Circles P1 y r r Consist of all points equidistant from one predetermined point (the center) (radius) r = c, where c is a constant On a Cartesian grid with center of circle at origin equation is r2 = x2 + y2 P1 y r r P0 x

2D Object Circle as polygon (Aligned) Ellipses A circle can be approximated by a polygon with many sides (>15) (Aligned) Ellipses A circle scaled along the x or y axis Example: height, on y-axis, remains 3, while length, on x-axis, changes from 3 to 6 1 2 3 4 5 6 7 8 9 10

2D to 3D Object Vertices in motion (“Generative object description”) Line is drawn by tracing path of a point as it moves (one dimensional entity) Square drawn by tracing vertices of a line as it moves perpendicularly to itself (two dimensional entity) Cube drawn by tracing paths of vertices of a square as it moves perpendicularly to itself (three-dimensional entity) Circle drawn by swinging a point at a fixed length around a center point 5

Building 3D Primitives Triangles and tri-meshes Parametric polynomials, like the aforementioned splines used to define surface patches.

Points A point is a list of n numbers referring to a location in n-D The individual components of a point are often referred to as coordinates i.e. (2, 3, 4) is a point in 3-D space This point’s x-coordinate is 2, it’s y-coordinate is 3, and it’s z-coordinate is 4

Vectors A vector is a list of n numbers referring to a direction (and magnitude) in n-D

Rays A ray is just a vector with a starting point Ray = (Point, Vector)

Points and Vectors Can define a vector by 2 points Point - Point = Vector Can define a new point by a point and a vector Point + Vector = Point

Planes How can we define a plane? 3 non-linear points Use linear interpolation A perpendicular vector and an incident point n • (x-x0) = 0 ax + by + cz + d = 0 Hessian normal form: Normalize n first n • x = - p

Raster Graphics Image produced as an array (the raster) of picture elements (pixels) in the frame buffer

Rasterization In the rasterization step, geometry in device coordinates is converted into fragments in screen coordinates After this step, there are no longer any “polygons”

Line Drawing A classic part of the computer graphics curriculum Input: Line segment definition (x1, y1), (x2, y2) Output: List of pixels (x1, y1) (x2, y2)

How Do They Look? So now we know how to draw lines But they don’t look very good:

Towards the Ideal Line We can only do a discrete approximation Illuminate pixels as close to the true path as possible, consider bi-level display only Pixels are either lit or not lit

Simple Line Based on slope-intercept algorithm from algebra: y = mx + b Simple approach: increment x, solve for y Floating point arithmetic required

Line drawing algorithm Need algorithm to figure out which intermediate pixels are on line path Pixel (x,y) values constrained to integer values Rounding may be required. E.g. computed point (10.48, 20.51) rounded to (10, 21)

Line Drawing Algorithm 0 1 2 3 4 5 6 7 8 9 10 11 12 8 7 6 5 4 3 2 1 Line: (3,2) -> (9,6) ? Which intermediate pixels to turn on?

Line Drawing Algorithm Slope-intercept line equation y = mx + b Given two end points (x0,y0), (x1, y1), how to compute m and b? (x0,y0) (x1,y1) dx dy

Does it Work? It seems to work okay for lines with a slope of 1 or less, but doesn’t work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work. Solution? - use symmetry. increment along x-axis if dy<dx else increment along y-axis

Line Drawing Algorithm Numerical example of finding slope m: (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)

Incremental Algorithm Basic Algorithm Find equation of line that connects two points P and Q Starting with leftmost point P, increment xi by 1 to calculate yi = m*xi + B where m = slope, B = y intercept Draw pixel at (xi, Round(yi)) where Round (yi) = Floor (0.5 + yi) Incremental Algorithm: Each iteration requires a floating-point multiplication Modify algorithm to use deltas (yi+1 – yi) = m*(xi+1 - xi) + B – B yi+1 = yi + m*(xi+1 – xi) If x = 1, then yi+1 = yi + m At each step, we make incremental calculations based on preceding step to find next y value

Incremental Algorithm Start at t = 0 At each step, increment t by dt Choose appropriate value for dt Ensure no pixels are missed: Implies: and Set dt to maximum of dx and dy

Example Code // Incremental Line Algorithm // Assume x0 < x1 void Line(int x0, int y0, int x1, int y1) { int x, y; float dy = y1 – y0; float dx = x1 – x0; float m = dy / dx; y = y0; for (x = x0; x < x1; x++) { WritePixel(x, Round(y)); y = y + m; }

Line Drawing Algorithm Pseudocode compute m; if m < 1: { float y = y0; // initial value for(int x = x0;x <= x1; x++, y += m) setPixel(x, round(y)); } else // m > 1 float x = x0; // initial value for(int y = y0;y <= y1; y++, x += 1/m) setPixel(round(x), y); Note: setPixel(x, y) writes current color into pixel in column x and row y in frame buffer

Bresenham Mid-Point algorithm Observation on lines. while( n-- ) { draw(x,y); move right; if( below line ) move up; } Bresenham Mid-Point algorithm

Testing for the side of a line. Need a test to determine which side of a line a pixel lies. Write the line in implicit form: Easy to prove F<0 for points above the line, F>0 for points below.

Testing for the side of a line. Need to find coefficients a,b,c. Recall explicit, slope-intercept form : So:

Decision variable. Referred to as decision variable Evaluate F at point M Referred to as decision variable NE M Very important. E Previous Pixel (xp,yp) Choices for Current pixel Choices for Next pixel

Mid-Point Algorithm If E chosen : But recall : Evaluate d for next pixel, Depends on whether E or NE Is chosen : If E chosen : But recall : NE M E So : Previous Pixel (xp,yp) Choices for Next pixel Choices for Current pixel

Decision variable. If NE was chosen : So : M NE E Previous Pixel (xp,yp) Choices for Next pixel Choices for Current pixel

Summary of mid-point algorithm Choose between 2 pixels at each step based upon the sign of a decision variable. Update the decision variable based upon which pixel is chosen. Start point is simply first endpoint (x1,y1). Need to calculate the initial value for d

Initial value of d. Start point is (x1,y1) But (x1,y1) is a point on the line, so F(x1,y1) =0 Conventional to multiply by 2 to remove fraction  doesn’t effect sign.

algorithm void MidpointLine(int x1,y1,x2,y2) { int dx=x2-x1; int dy=y2-y1; int d=2*dy-dx; int increE=2*dy; int incrNE=2*(dy-dx); x=x1; y=y1; WritePixel(x,y); while (x < x2) { if (d<= 0) { d+=incrE; x++ } else { d+=incrNE; x++; y++; } WritePixel(x,y);

It was (not) the end! 2-step algorithm by Xiaolin Wu: Treat line drawing as an automaton , or finite state machine, ie. looking at next two pixels of a line, easy to see that only a finite set of possibilities exist. The 2-step algorithm exploits symmetry by simultaneously drawing from both ends towards the midpoint.

Two-step Algorithm Possible positions of next two pixels dependent on slope – current pixel in blue: Slope between 0 and ½ Slope between ½ and 1 Slope between 1 and 2 Slope greater than 2

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

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

OpenGL Glbegin ( GL_LINES); glVertex2iv (p1); glVertex2iv (p2); glEnd ( );

OpenGL Glbegin ( GL_LINE_STRIP); glVertex2iv (p1); glVertex2iv (p2); glEnd ( );

OpenGL Glbegin ( GL_LINE_LOOP); glVertex2iv (p1); glVertex2iv (p2); glEnd ( );