Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms.

Slides:



Advertisements
Similar presentations
Circle Drawing Asst. Prof. Dr. Ahmet Sayar Kocaeli University
Advertisements

Graphics Primitives: line
5.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 5 – Drawing A Line.
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.
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann.
Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
1 King ABDUL AZIZ University Faculty Of Computing and Information Technology CS 454 Computer graphics Drawing Elementary Figures Dr. Eng. Farag Elnagahy.
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.
Scan Conversion Algorithms
Scan conversion of Line , circle & ellipse
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
The lines of this object appear continuous However, they are made of pixels 2April 13, 2015.
CS 376 Introduction to Computer Graphics 01 / 29 / 2007 Instructor: Michael Eckmann.
OUTPUT PRIMITIVES Screen vs. World coordinate systems ● Objects positions are specified in a Cartesian coordinate system called World Coordinate.
Math 140 Quiz 2 - Summer 2004 Solution Review (Small white numbers next to problem number represent its difficulty as per cent getting it wrong.)
2D Output Primitives Graphics packages provide basic operations (called primitive operations) to describe a scene in terms of geometric structures. The.
Raster conversion algorithms for line and circle
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics May 3, 2007.
University of Missouri at Columbia 2D Scan-line Conversion University of Missouri at Columbia.
Line Drawing by Algorithm. Line Drawing Algorithms Line drawn as pixels Graphics system –Projects the endpoints to their pixel locations in the frame.
Section 1.1 The Distance and Midpoint Formulas. x axis y axis origin Rectangular or Cartesian Coordinate System.
©Zachary Wartell, Larry F. Hodges Scan Conversion I Revision 1.6 8/9/2007 Copyright 2006, Dr. Zachary Wartell, UNCC, All Rights Reserved.
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Circle Drawing algo..
CGMB214: Introduction to Computer Graphics
Dr. S.M. Malaek Assistant: M. Younesi
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
coordinates, lines and increment
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
Equations of Lines Chapter 8 Sections
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
Larry F. Hodges 1 Design of Line and Circle Algorithms.
College of Computer and Information Science, Northeastern UniversityOctober 12, CS G140 Graduate Computer Graphics Prof. Harriet Fell Spring 2006.
1 Circle Drawing Algorithms Pictures snagged from
Introduction Computer Graphics & Its application Types of computer graphics Graphic display : random Scan & Raster Scan display Frame buffer and video.
Graphics Output Primitives
Line Drawing and Generalization. Outline  overview  line drawing  circle drawing  curve drawing.
CGMB214: Introduction to Computer Graphics
 A line segment in a scene is defined by the coordinate positions of the line end-points x y (2, 2) (7, 5)
Image Synthesis Rabie A. Ramadan, PhD 7. 2 Image Rasterization.
MIDPOINT CIRCLE & ELLIPSE GENERARTING ALGORITHMS
November 18, We could solve for y in terms of x ( x 0, y 0 ) are the origin points A Simple Circle Drawing Algorithm The equation for a circle.
10/19/04© University of Wisconsin, CS559 Fall 2004 Last Time Clipping –Why we care –Sutherland-Hodgman –Cohen-Sutherland –Intuition for Liang-Barsky Homework.
Midpoint Circle Algorithm
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
1 Bresenham’s Circle Algorithm Define:D(s i ) = distance of p 3 from circle D(t i ) = distance of p 2 from circle i.e.D(s i ) = (x i + 1) 2 + y i 2 – r.
Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing
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.
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
Scan Conversion of Line Segments. What is Scan conversion? Final step of rasterization (the process of taking geometric shapes and converting them into.
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.
Parametric Equations Until now, we’ve been using x and y as variables. With parametric equations, they now become FUNCTIONS of a variable t.
Primitive graphic objects
Chapter Three Part I Output Primitives CS 380.
Introduction to Graphing
Scan Conversion of Circles
2D Scan-line Conversion
Chapter 1 Graphs, Functions, and Models.
Introduction to Graphing
Chapter 3 Graphics Output Primitives
OUTPUT PRIMITIVES / DISPLAY TECHNIQUES
Presentation transcript:

Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms

Larry F. Hodges (modified by Amos Johnson) 2 y 2 -y 1 SLOPE = RISE RUN = x 2 -x 1 Basic Math Review Slope-Intercept Formula For A Line Given a third point on the line: P = (x,y) Slope = (y - y 1 )/(x - x 1 ) = (y 2 - y 1 )/(x 2 - x 1 ) Solving For y y = [(y 2 -y 1 )/(x 2 -x 1 )]x + [-(y 2 -y 1 )/(x 2 -x 1 )]x 1 + y 1 therefore y = Mx + B where M = [(y 2 -y 1 )/(x 2 -x 1 )] B = [-(y 2 -y 1 )/(x 2 -x 1 )]y 1 + y 1 Cartesian Coordinate System P1 = (x 1,y 1 ) P2 = (x 2,y 2 ) P = (x,y)

Larry F. Hodges (modified by Amos Johnson) 3 Other Helpful Formulas Length of line segment between P 1 and P 2 : L = sqrt [ (x 2 -x 1 ) 2 + (y 2 -y 1 ) 2 ] Midpoint of a line segment between P 1 and P 3 : P 2 = ( (x 1 +x 3 )/2, (y 1 +y 3 )/2 ) Two lines are perpendicular iff 1) M 1 = -1/M 2 or 2) Cosine of the angle between them is 0.

Larry F. Hodges (modified by Amos Johnson) 4 Parametric Form Of The Equation Of A 2D Line Segment Given points P 1 = (x 1, y 1 ) and P 2 = (x 2, y 2 ) x = x 1 + t(x 2 -x 1 ) y = y 1 + t(y 2 -y 1 ) t is called the parameter. When t = 0 we get (x 1,y 1 ) t = 1 we get (x 2,y 2 ) As 0 < t < 1 we get all the other points on the line segment between (x 1,y 1 ) and (x 2,y 2 ).

Larry F. Hodges (modified by Amos Johnson) 5 Basic Line and Circle Algorithms 1. Must compute integer coordinates of pixels which lie on or near a line or circle. 2. Pixel level algorithms are invoked hundreds or thousands of times when an image is created or modified. 3. Lines must create visually satisfactory images. Lines should appear straight Lines should terminate accurately Lines should have constant density Line density should be independent of line length and angle. 4. Line algorithm should always be defined.

Larry F. Hodges (modified by Amos Johnson) 6 Simple DDA Line Algorithm {Based on the parametric equation of a line} Procedure DDA(X1,Y1,X2,Y2 :Integer); Var Length, I:Integer; X,Y,Xinc,Yinc:Real; Begin Length := ABS(X2 - X1); If ABS(Y2 - Y1) > Length Then Length := ABS(Y2-Y1); Xinc := (X2 - X1)/Length; Yinc := (Y2 - Y1)/Length; X := X1; Y := Y1; DDA (digital differential analyzer) creates good lines but it is too time consuming due to the round function and long operations on real values. For I := 0 To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End {For} End; {DDA}

Larry F. Hodges (modified by Amos Johnson) 7 DDA Example Compute which pixels should be turned on to represent the line from (6,9) to (11,12). Length := Max of (ABS(11-6), ABS(12-9)) = 5 Xinc := 1 Yinc := 0.6 Values computed are: (6,9), (7,9.6), (8,10.2), (9,10.8), (10,11.4), (11,12)

Larry F. Hodges (modified by Amos Johnson) 8 Simple Circle Algorithms Since the equation for a circle on radius r centered at (0,0) is x 2 + y 2 = r 2, an obvious choice is to plot y = ±sqrt(r 2 - x 2 ) for -r <= x <= r. This works, but is inefficient because of the multiplications and square root operations. It also creates large gaps in the circle for values of x close to R (and clumping for x near 0). A better approach, which is still inefficient but avoids the gaps is to plot x = r cosø y = r sinø as ø takes on values between 0 and 360 degrees.

Larry F. Hodges (modified by Amos Johnson) 9 Fast Lines Using The Midpoint Method Assumptions: Assume we wish to draw a line between points (0,0) and (a,b) with slope M between 0 and 1 (i.e. line lies in first octant). The general formula for a line is y = Mx + B where M is the slope of the line and B is the y-intercept. From our assumptions M = b/a and B = 0. Therefore y = (b/a)x + 0 is f(x,y) = bx – ay = 0 (an equation for the line). If (x 1,y 1 ) lie on the line with M = b/a and B = 0, then f(x 1,y 1 ) = 0. (a,b) (0,0)

Larry F. Hodges (modified by Amos Johnson) 10 Fast Lines (cont.) For lines in the first octant, the next pixel is to the right or to the right and up. Assume: Distance between pixels centers = 1 Having turned on pixel P at (x i, y i ), the next pixel is T at (x i +1, y i +1) or S at (x i +1, y i ). Choose the pixel closer to the line f(x, y) = bx - ay = 0. The midpoint between pixels S and T is (x i + 1,y i + 1/2). Let e be the difference between the midpoint and where the line actually crosses between S and T. If e is positive the line crosses above the midpoint and is closer to T. If e is negative, the line crosses below the midpoint and is closer to S. To pick the correct point we only need to know the sign of e. (x i +1, y i + 1/2 + e) e (x i +1,y i + 1/2) P = (x i,y i )S = (x i + 1, y i ) T = (x i + 1, y i + 1)

Larry F. Hodges (modified by Amos Johnson) 11 Fast Lines - The Decision Variable f(x i +1,y i + 1/2 + e) = b(x i +1) - a(y i + 1/2 + e) = b(x i + 1) - a(y i + 1/2) -ae = f(x i + 1, y i + 1/2) - ae = 0 Let d i = f(x i + 1, y i + 1/2) = ae; d i is known as the decision variable. Since a >= 0, d i has the same sign as e. Algorithm: If d i >= 0 Then Choose T = (x i + 1, y i + 1) as next point d i+1 = f(x i+1 + 1, y i+1 + 1/2) = f(x i +1+1,y i +1+1/2) = b(x i +1+1) - a(y i +1+1/2) = f(x i + 1, y i + 1/2) + b - a = d i + b - a Else Choose S = (x i + 1, y i ) as next point d i+1 = f(x i+1 + 1, y i+1 + 1/2) = f(x i +1+1,y i +1/2) = b(x i +1+1) - a(y i +1/2) = f(x i + 1, y i + 1/2) + b = d i + b

Larry F. Hodges (modified by Amos Johnson) 12 x := 0; y := 0; d := b - a/2; For i := 0 to a do Plot(x,y); If d >= 0 Then x := x + 1; y := y + 1; d := d + b – a Else x := x + 1; d := d + b End Fast Line Algorithm Note: The only non-integer value is a/2. If we then multiply by 2 to get d' = 2d, we can do all integer arithmetic using only the operations +, -, and left-shift. The algorithm still works since we only care about the sign, not the value of d. The initial value for the decision variable, d0, may be calculated directly from the formula at point (0,0). d0 = f(0 + 1, 0 + 1/2) = b(1) - a(1/2) = b - a/2 Therefore, the algorithm for a line from (0,0) to (a,b) in the first octant is:

Larry F. Hodges (modified by Amos Johnson) 13 Bresenham’s Line Algorithm We can also generalize the algorithm to work for lines beginning at points other than (0,0) by giving x and y the proper initial values. This results in Bresenham's Line Algorithm. Begin {Bresenham for lines with slope between 0 and 1} a := ABS(xend - xstart); b := ABS(yend - ystart); d := 2*b - a; Incr1 := 2*(b-a); Incr2 := 2*b; If xstart > xend Then x := xend; y := yend Else x := xstart; y := ystart End For I := 0 to a Do Plot(x,y); x := x + 1; If d >= 0 Then y := y + 1; d := d + incr1 Else d := d + incr2 End End {For Loop} End {Bresenham} Note: This algorithm only works for lines with Slopes between 0 and 1

Larry F. Hodges (modified by Amos Johnson) 14 Circle Drawing Algorithm We only need to calculate the values on the border of the circle in the first octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0). Procedure Circle_Points(x,y :Integer); Begin Plot(x,y); Plot(y,x); Plot(y,-x); Plot(x,-y); Plot(-x,-y); Plot(-y,-x); Plot(-y,x); Plot(-x,y) End;

Larry F. Hodges (modified by Amos Johnson) 15 Fast Circles Consider only the first octant of a circle of radius r centered on the origin. We begin by plotting point (r,0) and end when x < y. The decision at each step is whether to choose the pixel directly above the current pixel or the pixel which is above and to the left. Assume P i = (x i, y i ) is the current pixel. T i = (x i, y i +1) is the pixel directly above S i = (x i -1, y i +1) is the pixel above and to the left.

Larry F. Hodges (modified by Amos Johnson) 16 Fast Circles - The Decision Variable f(x,y) = x 2 + y 2 - r 2 = 0 f(x i - 1/2 + e, y i + 1) = (x i - 1/2 + e) 2 + (y i + 1) 2 - r 2 = (x i - 1/2) 2 + (y i +1) 2 - r 2 + 2(x i -1/2)e + e 2 = f(x i - 1/2, y i + 1) + 2(x i - 1/2)e + e 2 = 0 Let d i = f(x i - 1/2, y i +1) = -2(x i - 1/2)e - e 2 Thus, If e 0 so choose point S = (x i - 1, y i + 1). d i+1 = f(x i /2, y i ) = ((x i - 1/2) - 1) 2 + ((y i + 1) + 1) 2 - r 2 = d i - 2(x i -1) + 2(y i + 1) + 1 = d i + 2(y i+1 - x i+1 ) + 1 If e >= 0 then d i <= 0 so choose point T = (x i, y i + 1). d i+1 = f(x i - 1/2, y i ) = d i + 2y i P = (x i,y i ) T = (x i,y i +1) S = (x i -1,y i +1) e (x i -1/2, y i + 1)

Larry F. Hodges (modified by Amos Johnson) 17 Fast Circles - Decision Variable (cont.) The initial value of d i is d 0 = f(r - 1/2, 0 + 1) = (r - 1/2) r 2 = 5/4 - r {1-r can be used if r is an integer} When point S = (x i - 1, y i + 1) is chosen then d i+1 = d i + -2x i+1 + 2y i When point T = ( x i, y i + 1) is chosen then d i+1 = d i + 2y i+1 + 1

Larry F. Hodges (modified by Amos Johnson) 18 Fast Circle Algorithm Begin {Circle} x := r; y := 0; d := 1 - r; Repeat Circle_Points(x,y); y := y + 1; If d <= 0 Then d := d + 2*y + 1 Else x := x - 1; d := d + 2*(y-x) + 1 End Until x < y End; {Circle} Procedure Circle_Points(x,y :Integer); Begin Plot(x,y); Plot(y,x); Plot(y,-x); Plot(x,-y); Plot(-x,-y); Plot(-y,-x); Plot(-y,x); Plot(-x,y) End;

Larry F. Hodges (modified by Amos Johnson) 19 Fast Ellipses The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b 2 x 2 + a 2 y 2 -a 2 b 2 = 0 (a,0) (-a,0) (0,b) (0,-b) (x, y) (x, -y) (-x, y) (-x, -y)

Larry F. Hodges (modified by Amos Johnson) 20 Fast Ellipses The circle algorithm can be generalized to work for an ellipse but only four way symmetry can be used. F(x,y) = b 2 x 2 + a 2 y 2 -a 2 b 2 = 0 All the points in one quadrant must be computed. Since Bresenham's algorithm is restricted to only one octant, the computation must occur in two stages. The changeover occurs when the point on the ellipse is reached where the tangent line has a slope of ±1. In the first quadrant, this is where the line y = x intersects the ellipses. (a,0) (-a,0) (0,b) (0,-b) (x, y) (x, -y) (-x, y) (-x, -y) y = x

Larry F. Hodges (modified by Amos Johnson) 21 Line and Circle References Bresenham, J.E., "Ambiguities In Incremental Line Rastering," IEEE Computer Graphics And Applications, Vol. 7, No. 5, May Eckland, Eric, "Improved Techniques For Optimising Iterative Decision- Variable Algorithms, Drawing Anti-Aliased Lines Quickly And Creating Easy To Use Color Charts," CSC 462 Project Report, Department of Computer Science, North Carolina State University (Spring 1987). Foley, J.D. and A. Van Dam, Fundamentals of Interactive Computer Graphics, Addison-Wesley Newman, W.M and R.F. Sproull, Principles Of Interactive Computer Graphics, McGraw-Hill, Van Aken J. and Mark Novak, "Curve Drawing Algorithms For Raster Display," ACM Transactions On Graphics, Vol. 4, No. 3, April 1985.