Faster Circles for Apples D

Faster Circles for Apples D

Faster Circles for Apples aniel Lee’s article, “Fast Circle Rou­ (Ycrd-Ymid) A 2 We can reduce the number of these divi­ tine,” in DDJ No. 79 (May 1983) + (Xcrd -Xmid)A 2 = RA2 sions by evaluating the expression for only one eighth of the circle and by plot­ D inspired me to create a similar circle- where Xcrd is the horizontal variable, making approach for the Apple. Although Ycrd is the vertical variable, and Xmid ting the rest of the circle symmetrically about the coordinate axes and about a I first wrote an Applesoft BASIC program and Ymid are constants. Differentiating diagonal. to test the logic, I chose variable names with respect to Xcrd gives that could serve for both BASIC and as­ It is best to select the upper left ex­ sembly language. Thus “X” and “Y” refer 2 * (Ycrd-Ymid) *dYcrd/dXcrd treme of the circle as the starting point. to the X and Y registers of the Apple’s + 2* (Xcrd-Xmid) = 0 According to the Apple coordinate sys­ 6502 chip, and “Xcrd” and “Ycrd” are whence tem, where point (0,0) is the upper left the coordinates for the points of the corner of the screen, our starting point is circle. dYcrd/dXcrd = given by - (Xmid-Xcrd)/(Ymid-Ycrd) A circle of radius R centered at (Xmid-R/SQR(2),Ymid-R/SQR(2)) (Xmid, Ymid) is described by the familiar The last equation implies that, in formula drawing the circle, if we increase Xcrd by From here we move to the right and stop 1 to plot the next point we must decreaseat the extreme top of the circle, which is Ycrd by point (Xmid,Ymid-R). This choice of by Myron L. Pulier starting and ending points facilitates a simple FOR-NEXT program loop (FOR (Xmid-Xcrd)/(Ymid-Ycrd) Xcrd = Xmid-R/SQR(2) to Xmid) and The slowest operation here is division byavoids the divide-by-zero error we might Myron L. Pulier, M.D., 101 Cedar Lane, Ymid-Ycrd, which must be performed encounter at the extreme right and left of Teaneck, New Jersey 07666. each time we want a new value for Ycrd. the circle, where the slope is undefined. Reader Com m entary circle and, using the symmetry of the exhibit the algorithm’s behavior (see circle, plots eight points for each point Figure 2, below). More Fast Circles . ..generated. For use with digital plotters, William A. McWorter, Jr. the algorithm is invoked eight times Mathematics Department Dear DDJ, forward and backward so that the Ohio State University Daniel L. Lee’s algorithm has got points are of concentric circles; the 231 W. 18th Avenue to be faster than Microsoft’s pedestrian low algorithm is plotted in counter­ Columbus, OH 43210 CIRCLE command, but both suffer clockwise order. I enclose a plot of from the same malady: they reinvent concentric circles in low resolution to (Listing Three begins on page 30) the wheel — only this time it’s square! When I think I’ve discovered a marvelous algorithm, I wonder if I’ve outsmarted the professionals. I usually haven’t. But hope springs eternal. I search the literature anyway. My brainchild is at least 17 years old [B. K. P. Horn, “Circle Generators for Display Devices,” Computer Graphics and Image Processing (5), pp. 280­ 288 (1976)]. Neither trigonometry nor calculus is needed to devise a circle generator. For a circle of radius R one wants to plot points (X,Y) with integer coordi­ nates which most nearly solve the equation X2 + Y2 = R2 The difference between the left side and R2 is a measure of nearness. A suitable circle generator simply chooses successive points to minimize this difference. The enclosed listing (see Listing Three, page 30) is a ren­ dering of such an algorithm. It gener­ ates points for about one eighth of the 18 Dr. Dobb’s Journal, December 1983 723 See Figure 1 (at right). For each point that we find by the above method, we can generate seven symmetric points by reflection through the horizontal and vertical diameters of the circle and by exchanging Xcrd with Ycrd. A point (Xcrd,Ycrd) is ABS(Xcrd- Xmid) distant from the vertical axis of the circle. Since its reflection should be the same distance from this axis, its ab­ scissa is 2*Xm id-Xcrd. Similarly, reflec­ tion through the horizontal diameter gives an ordinate of 2 * Ymid-Ycrd. To reflect a point through the diagonal line that runs from upper left to lower right, we find the point whose distance to the vertical axis equals the distance of the original point to the horizontal axis and whose distance to the horizontal axis is the same as the original point’s distance from the vertical axis, namely (Xmid+Ymid-Ycrd,Xmid+Ymid-Xcrd) This new point can now be reflected as before through the vertical and horizontal axes. In Listing One (page 21), subroutine 9000 plots four points symmetrically about the horizontal and vertical axes. Figure 1. Lines 10050 and 10055 switch the X and Faster Circle Y coordinates, then line 10060 calls 9000 to plot the four new points. The parame- Reader Commentary numbers to add up. The same applies An Ellipse . And Fast Ellipsesto y2. To generate an ellipse is a slightly In order to plot a circle, we might more complex matter, but in the end start at the point (0,r) and plot towards we lose little speed. The equation for Dear Sirs: (r,0), using symmetry to generate the an ellipse centered at the origin is Mr. Daniel Lee, in the May ’83 other arcs of the circle. This would issue, presented a fast circle generator. mean that x would go from 0 to r, y b2x2 + a2y2 = a2b2 [2] It compensated for any given screen would go from r to 0, x2 would go aspect ratio, and as such may be used from 0 to r2, and y2 would go from r2 as an ellipse generator. I submit the to 0. It is easier than it first appears to where b is the positive y-intercept, a is algorithm described below as an even calculate y2. Note that y2 is the sum the positive x-intercept, and a/b is the faster alternative. The speed improve­ of the odd numbers from l to 2y - 1. In resulting aspect ratio. I claim that in ment results from the elimination of the initialization phase it will be neces­ order to successfully trace the ellipse all division and most of the multiplica­ sary (perhaps) to compute y2 directly, we need only do exactly as we do for tion. The approach taken could easily but fory’ = y -l, y’2 = y2 - (2y- 1). the circle, but we must multiply every be modified to allow the generation reference to x by b2 and every refer­ of arcs. Above I said “ perhaps” because it ence to y by a2. In other words, every The method which I present here develops that one does not need totime we actually move in the positive is based on the equation of the circle, refer directly to y2 or even to x2. Thex direction, we add b2(2 x -l) to e; for and a trick which eliminates a great procedure for drawing the circle every time we actually move in the deal of multiplication. There is no requires that we assume, as we did negative y direction, we must subtract calculus or trigonometry involved, im­above, that we will draw primarily a2(2y-l) frome. Again we decide plicitly or explictly. from (r,0) to (0,r) and use symmetry which step or combination of steps The equation of the circle is well to generate the rest of the points. As to take by insisting that the e that known: we compute the points for the primary would result from the step or combi­ arc, we maintain a total e. The total nation of steps be as close to 0 as x2 + y2= r2 [ l ] starts at 0; for every time we actually possible, fn this case we are plotting where r is the radius. Since we want to move in the positive x direction, we from (0,b) to (a,0). minimize multiplication, we have to add 2x- l toe; for every time we actu­ If perhaps the terms b2(2x-l) use “magic.” A magical property of ally move in the negative y direction, and a2(2y-l) look like they involve the positive integers is that the square we subtract 2y-l frome. We decide too much multiplication, please realize of a positive integern is the sum of the precisely which step or combination of that in fact no multiplication is re­ first n odd numbers. This means that if steps to take by insisting that the e quired. For example, we would already we want to compute x2 for each x we that would result from the step or know the evaluation of b2(2x-l) to can actually plot (i.e., each integer x), combination of steps be as close to 0 we only need to know which odd as possible. (Continued in box on page 20) Dr. Dobb’s Journal, December 1983 19 VM ters R2, X2, Y2, and XY have been intro­ double-precision variable. It occupies lo­circle. Here the first order of business is duced to speed computation. cations XCRDH and XCRDL. Ycrd is to approximate the value of R/SQR(2) supplemented by a fractional portionby using R*3/4 instead.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us