Chapter 3 Graphics Output Primitives

Slides:



Advertisements
Similar presentations
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann.
Advertisements

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.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms.
30/9/2008Lecture 21 Computer Graphics Assistant Professor Dr. Sana’a Wafa Al-Sayegh 2 nd Semester ITGD3107 University of Palestine.
In the name of God Computer Graphics Bastanfard.
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
Output Primitives Computer Graphics.
Line Drawing by Algorithm. Line Drawing Algorithms Line drawn as pixels Graphics system –Projects the endpoints to their pixel locations in the frame.
CS123 | INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Scan Conversion CS123 1 of 44Scan Conversion - 10/14/2014.
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Circle Drawing algo..
March Shape Drawing Algorithms Shmuel Wimer Bar Ilan Univ., Engineering Faculty.
CGMB214: Introduction to Computer Graphics
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
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
Larry F. Hodges 1 Design of Line and Circle Algorithms.
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)
ELLIPSE GENERATING ALGORITHMS 1. DEFINITION Ellipse is an elongated circle. Elliptical curves can be generated by modifying circle-drawing procedures.
Image Synthesis Rabie A. Ramadan, PhD 7. 2 Image Rasterization.
MIDPOINT CIRCLE & ELLIPSE GENERARTING ALGORITHMS
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.
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.
In the name of God Computer Graphics. Today Introduction Sampling Graphic Output Primitives 1.Line 2.Circle 3.Curve 4.polygon.
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.
Scan Conversion.
Bresenham’s Line Algorithm
OUTPUT PRIMITIVES A.Aruna/Faculty of Information technology/SNSCE13/19/2016.
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 07 Ellipse and Other Curves Taqdees A. Siddiqi
Computer Graphics Lecture 06 Circle Drawing Techniques Taqdees A. Siddiqi
Computer Graphics : output primitives.. 2 of 32 T1 – pp. 103–123, 137–145, 147–150, 164–171 Points and LinesPoints Line Drawing AlgorithmsLine Mid–Point.
Computer Graphics Lecture 05 Line Drawing Techniques Taqdees A. Siddiqi
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.
OUTPUT PRIMITIVES CEng 477 Computer Graphics METU, 2004.
1 Computer Graphics –Lect 03 Graphics Output Primitives Chapter – 3 of Hearn & Baker using OPENGL.
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
Raster Graphics.
CS 4731: Computer Graphics Lecture 20: Raster Graphics Part 1
Scan Conversion or Rasterization
Lecture 05: Mid-point Ellipse algorithm Dr. Manal Helal – Fall 2014
Chapter Three Part I Output Primitives CS 380.
Scan Conversion of Circles
Rasterization and Antialiasing
Scan Conversion (From geometry to pixels)
Chapter 3 Graphics Output Primitives
Presentation transcript:

Chapter 3 Graphics Output Primitives Computer Graphics Chapter 3 Graphics Output Primitives Andreas Savva

Basic Raster Algorithms for 2D Primitives Description of pictures Specified by a set of intensities for the pixel positions. Describe it as a set of complex objects, such as trees, furniture and walls, positioned at specific coordinate locations within the scene. Graphics programming packages provide functions to describe a scene in terms of basic geometric structures referred to as output primitives. Points Straight lines Circles Splines curves and surfaces Polygon color areas Character strings Etc.

Implementing Application programs Description of objects in terms of primitives and attributes and converts them to the pixels on the screen. Primitives – what is to be generated Attributes – how primitives are to be generated

Points (0,0) (maxx,maxy) CRT The electron beam is turned on to illuminate the phosphor at the selected location (x, y) where 0 ≤ x ≤ maxx 0 ≤ y ≤ maxy setpixel(x, y, intensity) – loads an intensity value into the frame-buffer at (x, y). getpixel(x, y) – retrieves the current frame-buffer intensity setting at position (x, y).

Lines Analog devises, such as a random-scan display or a vector plotter, display a straight line smoothly from one endpoint to another. Linearly varying horizontal and vertical deflection voltages are generated that are proportional to the required changes in the x and y directions to produce the smooth line.

Digital devices display a straight line by plotting discrete coordinate points along the line path which are calculated from the equation of the line. Screen locations are referenced with integer values, so plotted positions may only approximate actual line positions between two specific endpoints. A computed line position of (10.48, 20.51) will be converted to pixel position (10, 21). This rounding of coordinate values to integers causes lines to be displayed with a stairstep appearance (the “jaggies”). Particularly noticeable on systems with low resolution. To smooth raster lines, pixel intensities along the line paths must be adjusted.

Line Drawing Algorithms Cartesian equation: y = mx + c where m – slope c – y-intercept x1 y1 x2 y2

Slope +ve -ve if |m| = 1  = 45° if |m|  1 -45° <  < 45° ° ° if |m|  1 45° <  < 90° or -90° <  < -45° ° °

|m| = 1 y = x m = 1 c = 0 y x y 1 2 3 4 5 6 7 8 8  7 6 5 4 3 2 1 x

|m|  1 y = ½ x + 1 m = ½ c = 1 y x x y round(y) 1 1.5 2 3 2.5 4 5 3.5 8 7 6 5  4 3 2 1 x y round(y) 1 1.5 2 3 2.5 4 5 3.5 6 7 4.5 8 x

|m|  1 y = 3x - 2 m = 3 c = -2 y x x y round(y) -2 1 2 4 3 7 10 5 13 8 7  6 5 4 3 2 1 x y round(y) -2 1 2 4 3 7 10 5 13 6 16 19 8 22 x outside

The Digital Differential Analyzer (DDA) Algorithm means that for a unit (1) change in x there is m-change in y. Do not use y = 3x + 1 to calculate y. Use m x y 1 4 2 7 3 10 13 5 16 i.e. y = 3x + 1 m = 3 means that for a unit (1) change in y there is 1/m change in x.

The DDA Method Uses differential equation of the line : m If slope |m|  1 then increment x in steps of 1 pixel and find corresponding y-values. If slope |m|  1 then increment y in steps of 1 pixel and find corresponding x-values.   step through in x step through in y

The DDA Method Desired line (xi+1,round(yi+m)) (xi+1,yi+m) (xi,yi)

if slope m  0 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 xi+1 = xi + 1/m Right Right Left Left

Proceeding from right-endpoint to left-endpoint if slope m  0 if |m|  1 xi+1 = xi - 1 yi+1 = yi - m if |m|  1 yi+1 = yi - 1 xi+1 = xi - 1/m Right Right Left Left

if slope m < 0 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 xi+1 = xi - 1/m Left Left Right Right

Proceeding from right-endpoint to left-endpoint if slope m  0 if |m|  1 xi+1 = xi - 1 yi+1 = yi - m if |m|  1 yi+1 = yi + 1 xi+1 = xi + 1/m Left Left Right Right

Example (DDA) y x x y round(y) 1 4/3 2 5/3 3 4 7/3 5 8/3 6 7 10/3 8 1 4/3 2 5/3 3 4 7/3 5 8/3 6 7 10/3 8 11/3 8 7 6 5 4  3 2 1 x

Example (DDA) y x y x round(x) 8 7 1/3 6 2/3 1 5 4 4/3 3 5/3 2 7/3 8/3 7 1/3 6 2/3 1 5 4 4/3 3 5/3 2 7/3 8/3 8  7 6 5 4 3 2 1 x

void LineDDA(int x0, int y0, int x1, int y1) { int dx = x1 – x0, dy = y1 – y0, steps; if (abs(dx)>abs(dy)) steps = abs(dx); else steps = abs(dy); // one of these will be 1 or -1 double xIncrement = (double)dx / (double )steps; double yIncrement = (double)dy / (double )steps; double x = x0; double y = y0; setPixel(round(x), round(y)); for (int i=0; i<steps; i++) { x += xIncrement; y += yIncrement; } Note: The DDA algorithm is faster than the direct use of y = mx + c. It eliminates multiplication; only one addition.

Example Draw a line from point (2,1) to (12,6) 7 6 5 4 3 2 1 8 9 10 11 12

Bresenham Line Algorithm A more efficient approach Basis of the algorithm: From start position decide A or B next A B Start position

Bresenham Line Algorithm True line ti si For a given value of x one pixel lies at distance ti above the line, and one pixel lies at distance si below the line

Bresenham Line Algorithm Decision parameter di = (si - ti) If di  0, then closest pixel is below true line (si smaller) If di  0, then closest pixel is above true line (ti smaller) We must calculate the new values for di as we move along the line.

Example: x1 x2 x3 x4 x5 x0 y0 y1 y2 y3 y5 At x1 : At x2 : At x3 : Start pixel at (x0,y1) x1 x2 x3 x4 x5 x0 y0 y1 y2 y3 y5 At x1 : s1 = dy t1 = dx - dy d1 = (si - ti) = dy - (dx - dy) = 2dy - dx but 2dy  dx  di  0  y stays the same hence next pixel is at (x1,y1) True line 4dy 3dy At x2 : s2 = 2dy t2 = dx - 2dy d2 = (s2 – t2) = 2dy - (dx - 2dy) = 4dy - dx Suppose d2  0  y is incremented hence next pixel is at (x2,y2) 2dy dy At x3 : s3 = 3dy - dx t2 = 2dx - 3dy d3 = (s2 – t3) = 6dy - 3dx  0 so y stays the same hence next pixel is at (x3,y2)

In General For a line with gradient ≤ 1 For a line with gradient  1 d0 = 2dy – dx if di  0 then yi+1 = yi di+1 = di + 2dy if di ≥ 0 then yi+1 = yi + 1 di+1 = di + 2(dy – dx) xi+1 = xi + 1 For a line with gradient  1 d0 = 2dx – dy if di  0 then xi+1 = xi di+1 = di + 2dx if di ≥ 0 then xi+1 = xi + 1 di+1 = di + 2(dx – dy) yi+1 = yi + 1 Note: For |m| ≤ 1 the constants 2dy and 2(dy-dx) can be calculated once, so the arithmetic will involve only integer addition and subtraction.

Example – Draw a line from (20,10) to (30,18) dx = 10 dy = 8 initial decision d0 = 2dy – dx = 6 Also 2dy = 16, 2(dy – dx) = -4 i di (xi+1,yi+1) 0 6 (21,11) 1 2 (22,12) 2 -2 (23,12) 3 14 (24,13) 4 10 (25,14) 5 6 (26,15) 6 2 (27,16) 7 -2 (28,16) 8 14 (29,17) 9 10 (30,18) 19 18 17 16 15 14 13 12 11 10 20 21 22 23 24 25 26 27 28 29 30 31 32

void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1 { int dx = abs(x1 – x0), dy = abs(y1 – y0); int d = 2 * dy – dx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dy – dx); int x, y; if (x0 > x1) { // determines which point to use as start, which as end x = x1; y = y1; x1 = x0; } else { x = x0; y = y0; setPixel(x,y); while (x < x1) { x++; if (d < 0) d += twoDy; y++; d += twoDyMinusDx; setPixel(x, y);

Special cases Special cases can be handled separately Horizontal lines (y = 0) Vertical lines (x = 0) Diagonal lines (|x| = |y|) directly into the frame-buffer without processing them through the line-plotting algorithms.

Parallel Line Algorithms Take advantage of multiple processors. Given np processors, subdivide the line path into np Bresenham segments. For a line with slope 0  m  1 and leftpoint (x0,y0) the distance to the right endpoint (left endpoint for next segment) is where x = width of the line xp is computed using integer division Numbering the segments, and the processors, as 0, 1, 2, …, np-1, starting x-coordinate for the kth partition is xk = x0 + kxp

Parallel Line Algorithms (continue) i.e. x = 15 , np = 4 processors Starting x-values at x0, x0 + 4, x0 + 8, x0 + 12 yp = mxp At the kth segment, the starting y-coordinates is yk = y0 + round(kyp) Also, the initial decision parameter for Bresenham’s algorithm at the start of the kth subinterval is: pk = (kxp)(2y) – round(kyp)(2x) + 2y – x

Anti-aliasing for straight lines Lines generated can have jagged or stair-step appearance, one aspect of phenomenon called aliasing, caused by fact that pixels are integer coordinate points. Use anti-aliasing routines to smooth out display of a line by adjusting pixels intensities along the line path

Another raster effect Line B Line A Both lines plotted with the same number of pixels, but the diagonal line is longer than the horizontal line Visual effect is diagonal line appears less thick. If the intensity of each pixel is I, then the intensity per unit length of line A is I, whereas for line B it is only I/2; this discrepancy is easily detected by the viewer.

Circle Generating Algorithms Circles and ellipses are common components in many pictures. Circle generation routines are often included in packages.

Circle Equations Polar form x = rCos y = rSin (r = radius of circle) P=(rCos, rSin) rSin) rCos) x y r

Drawing a circle  = 0° while ( < 360°) x = rCos y = rSin setPixel(x,y)  =  + 1° end while Disadvantages To find a complete circle  varies from 0° to 360° The calculation of trigonometric functions is very slow.

Cartesian form Use Pythagoras theorem x2 + y2 = r2 x r y y x r

Circle algorithms Step through x-axis to determine y-values Disadvantages: Not all pixel filled in Square root function is very slow

Circle Algorithms Use 8-fold symmetry and only compute pixel positions for the 45° sector. 45° (x, y) (y, x) (-x, y) (y, -x) (x, -y) (-x, -y) (-y, x) (-y, -x)

Bresenham’s Circle Algorithm Consider only 45° ≤  ≤ 90° General Principle The circle function: and

Bresenham’s Circle Algorithm p1 p3 yi D(si) D(ti) yi - 1 p2 r xi xi + 1 After point p1, do we choose p2 or p3?

Bresenham’s Circle Algorithm Define: D(si) = distance of p3 from circle D(ti) = distance of p2 from circle i.e. D(si) = (xi + 1)2 + yi2 – r2 [always +ve] D(ti) = (xi + 1)2 + (yi – 1)2 – r2 [always -ve] Decision Parameter pi = D(si) + D(ti) so if pi < 0 then the circle is closer to p3 (point above) if pi ≥ 0 then the circle is closer to p2 (point below)

The Algorithm x0 = 0 y0 = r p0 = [12 + r2 – r2] + [12 + (r-1)2 – r2] = 3 – 2r if pi < 0 then yi+1 = yi pi+1 = pi + 4xi + 6 else if pi ≥ 0 then yi+1 = yi – 1 pi+1 = pi + 4(xi – yi) + 10 Stop when xi ≥ yi and determine symmetry points in the other octants xi+1 = xi + 1

Example r = 10 p0 = 3 – 2r = -17 Initial point (x0, y0) = (0, 10) i pi  9 8 7 6 5 4 3 2 1 i pi xi, yi -17 (0, 10) 1 -11 (1, 10) 2 -1 (2, 10) 3 13 (3, 10) 4 -5 (4, 9) 5 15 (5, 9) 6 9 (6, 8) 7 (7,7)

Exercises Draw the circle with r = 12 using the Bresenham algorithm. Draw the circle with r = 14 and center at (15, 10).

Decision Parameters Prove that if pi < 0 and yi+1 = yi then pi+1 = pi + 4xi + 6 Prove that if pi ≥ 0 and yi+1 = yi – 1 then pi+1 = pi + 4(xi – yi) + 10

Advantages of Bresenham circle Only involves integer addition, subtraction and multiplication There is no need for squares, square roots and trigonometric functions

Midpoint Circle Algorithm yi  yi-1 xi xi+1 xi+2 Midpoint x2 + y2 – r2 = 0 Assuming that we have just plotted the pixels at (xi , yi). Which is next? (xi+1, yi) OR (xi+1, yi – 1). - The one that is closer to the circle.

Midpoint Circle Algorithm The decision parameter is the circle at the midpoint between the pixels yi and yi – 1. If pi < 0, the midpoint is inside the circle and the pixel yi is closer to the circle boundary. If pi ≥ 0, the midpoint is outside the circle and the pixel yi - 1 is closer to the circle boundary.

Decision Parameters Decision Parameters are obtained using incremental calculations OR where yi+1 is either yi or yi-1 depending on the sign of pi Note: xi+1 = xi +1

move circle origin at (0,0) by The Algorithm Initial values:- point(0,r) x0 = 0 y0 = r Initial decision parameter At each xi position, starting at i = 0, perform the following test: if pi < 0, the next point is (xi + 1, yi) and pi+1 = pi + 2xi+1 + 1 If pi ≥ 0, the next point is (xi+1, yi-1) and pi+1 = pi + 2xi+1 + 1 – 2yi+1 where 2xi+1 = 2xi + 2 and 2yi+1 = 2yi – 2 Determine symmetry points in the other octants Move pixel positions (x,y) onto the circular path centered on (xc, yc) and plot the coordinates: x = x + xc, y = y + yc Repeat 3 – 5 until x ≥ y move circle origin at (0,0) by x = x – xc and y = y – yc

Example r = 10 p0 = 1 – r = -9 (if r is integer round p0 = 5/4 – r to integer) Initial point (x0, y0) = (0, 10) 10  9 8 7 6 5 4 3 2 1 i pi xi+1, yi+1 2xi+1 2yi+1 -9 (1, 10) 2 20 1 -6 (2, 10) 4 -1 (3, 10) 6 3 (4, 9) 8 18 -3 (5, 9) 10 5 (6, 8) 12 16 (7, 7)

Exercises Draw the circle with r = 12 using the Midpoint-circle algorithm Draw the circle with r = 14 and center at (15, 10).

Exercises Prove that if pi < 0 and yi+1 = yi then pi+1 = pi + 2xi+1 + 1 Prove that if pi ≥ 0 and yi+1 = yi-1 then pi+1 = pi + 2xi+1 + 1 – 2yi+1

Midpoint function void plotpoints(int x, int y) { setpixel(xcenter+x, ycenter+y); setpixel(xcenter-x, ycenter+y); setpixel(xcenter+x, ycenter-y); setpixel(xcenter-x, ycenter-y); setpixel(xcenter+y, ycenter+x); setpixel(xcenter-y, ycenter+x); setpixel(xcenter+y, ycenter-x); setpixel(xcenter-y, ycenter-x); } void circle(int r) int x = 0, y = r; plotpoints(x,y); int p = 1 – r; while (x<y) { x++; if (p<0) p += 2*x + 1; else { y--; p += 2*(x-y) + 1;

Ellipse-Generating Algorithms Ellipse – A modified circle whose radius varies from a maximum value in one direction (major axis) to a minimum value in the perpendicular direction (minor axis). P=(x,y) F1 F2 d1 d2 The sum of the two distances d1 and d2, between the fixed positions F1 and F2 (called the foci of the ellipse) to any point P on the ellipse, is the same value, i.e. d1 + d2 = constant

Ellipse Properties Expressing distances d1 and d2 in terms of the focal coordinates F1 = (x1, x2) and F2 = (x2, y2), we have: Cartesian coordinates: Polar coordinates: ry rx

Ellipse Algorithms Symmetry between quadrants Not symmetric between the two octants of a quadrant Thus, we must calculate pixel positions along the elliptical arc through one quadrant and then we obtain positions in the remaining 3 quadrants by symmetry (x, y) (-x, y) (x, -y) (-x, -y) rx ry

Ellipse Algorithms Decision parameter: 1 Slope = -1 rx ry 2

Ellipse Algorithms 1 Slope = -1 rx ry 2 Starting at (0, ry) we take unit steps in the x direction until we reach the boundary between region 1 and region 2. Then we take unit steps in the y direction over the remainder of the curve in the first quadrant. At the boundary therefore, we move out of region 1 whenever

Midpoint Ellipse Algorithm yi  yi-1 xi xi+1 xi+2 Midpoint Assuming that we have just plotted the pixels at (xi , yi). The next position is determined by: If p1i < 0 the midpoint is inside the ellipse  yi is closer If p1i ≥ 0 the midpoint is outside the ellipse  yi – 1 is closer

Decision Parameter (Region 1) At the next position [xi+1 + 1 = xi + 2] OR where yi+1 = yi or yi+1 = yi – 1

Decision Parameter (Region 1) Decision parameters are incremented by: Use only addition and subtraction by obtaining At initial position (0, ry)

Region 2 Over region 2, step in the negative y direction and midpoint is taken between horizontal pixels at each step. yi  yi-1 xi xi+1 xi+2 Midpoint Decision parameter: If p2i > 0 the midpoint is outside the ellipse  xi is closer If p2i ≤ 0 the midpoint is inside the ellipse  xi + 1 is closer

Decision Parameter (Region 2) At the next position [yi+1 – 1 = yi – 2] OR where xi+1 = xi or xi+1 = xi + 1

Decision Parameter (Region 2) Decision parameters are incremented by: At initial position (x0, y0) is taken at the last position selected in region 1

Midpoint Ellipse Algorithm Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the origin as (x0, y0) = (0, ry) Calculate the initial parameter in region 1 as At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0, 0) is (xi + 1, yi) and otherwise, the next point is (xi + 1, yi – 1) and and continue until

Midpoint Ellipse Algorithm (x0, y0) is the last position calculated in region 1. Calculate the initial parameter in region 2 as At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0, 0) is (xi, yi – 1) and otherwise, the next point is (xi + 1, yi – 1) and Use the same incremental calculations as in region 1. Continue until y = 0. For both regions determine symmetry points in the other three quadrants. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot the coordinate values x = x + xc , y = y + yc

Move out of region 1 since Example rx = 8 , ry = 6 2ry2x = 0 (with increment 2ry2 = 72) 2rx2y = 2rx2ry (with increment -2rx2 = -128) Region 1 (x0, y0) = (0, 6) i pi xi+1, yi+1 2ry2xi+1 2rx2yi+1 -332 (1, 6) 72 768 1 -224 (2, 6) 144 2 -44 (3, 6) 216 3 208 (4, 5) 288 640 4 -108 (5, 5) 360 5 (6, 4) 432 512 6 244 (7, 3) 504 384 Move out of region 1 since 2ry2x > 2rx2y

Example Region 2 (x0, y0) = (7, 3) (Last position in region 1) i pi xi+1, yi+1 2ry2xi+1 2rx2yi+1 -151 (8, 2) 576 256 1 233 (8, 1) 128 2 745 (8, 0) - Stop at y = 0 6  5 4 3 2 1 7 8

Exercises Draw the ellipse with rx = 6, ry = 8. Draw the ellipse with rx = 14, ry = 10 and center at (15, 10).

Midpoint Ellipse Function void ellipse(int Rx, int Ry) { int Rx2 = Rx * Rx, Ry2 = Ry * Ry; int twoRx2 = 2 * Rx2, twoRy2 = Ry2 * Ry2; int p, x = 0, y = Ry; int px = 0, py = twoRx2 * y; ellisePlotPoints(xcenter, ycenter, x, y); // Region 1 p = round(Ry2 – (Rx2 * Ry) + (0.25 * Rx2)); while (px < py) { x++; px += twoRy2; if (p < 0) p += Ry2 + px; else { y--; py -= twoRx2; p += Ry2 + px – py; } // Region 2 p = round(Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1)*(y-1) – Rx2 * Ry2; while (y > 0) { if (p > 0) p += Rx2 – py; p += Rx2 – py + px;