• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Bresenham's Circle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Bresenham's Circle


  • Subject: Bresenham's Circle
  • From: Paul Skinner <email@hidden>
  • Date: Sun, 23 Mar 2003 11:09:31 -0500

On Thursday, March 20, 2003, at 10:29 AM, Helmut Fuchs wrote:
snip

Or you could search the Internet for Bresenham's Algorithm - which allows to draw a circle using only basic integer math.

-Helmut

Alrighty then, I will...

Bresenham's Circle
http://homepage.mac.com/paulskinner/.cv/paulskinner/Public/ Bresenham's Circle-binhex.hqx

2003 Paul Skinner
email@hidden
http://homepage.mac.com/paulskinner/

Based on Bresenham's Circle formula
http://gameprogrammer.com/mailinglist.html
> ...the circle formula is r^2 = x^2 + y^2 this can be rearranged to be: y = (plus-or-minus)sqrt(r^2-x^2)

This script generates values for the perimeter of a circle using basic math functions. No Scripting Additions are used.
It can generate a plot of the values if you have SmileLab.
It moves a finder window in a circle as a demonstration. The circle it generates is high resolution skipping no pixels
on screen. A 72 pixel radius circle generates 292 points of data. For use in the window move demonstration,
I discard all but 64+/- points of this data to achieve the desired effect.

Bresenham's formula takes advantage of the fact that "a circle is symetrical about the x axis, so only the first 180 degrees need to be calculated. Next we see that it's also symetrical about the y axis, so now we only need to calculate the first 90 degrees. Finally we see that the circle is also symetrical about the 45 degree diagonal axis, so we only need to calculate the first 45 degrees".
Converting the algorithm was straigtforward. Rotating the 45 degrees of generated data around to get 360 degrees without excess code made my head hurt.



set {xValueList, yValueList} to {{}, {}}
set radius to 72
set radiusSquared to radius * radius
set {a, b, c} to {1, radius, 1}

repeat 2 times
repeat 2 times
repeat with x from a to b by c
set the end of yValueList to ((c * ((radiusSquared - (x ^ 2)) ^ (0.5))) div 1)
set the end of xValueList to x
end repeat
set {a, b, c} to {b, a, -c}
end repeat
set {a, b, c} to {a, -b, -c}
end repeat


(* Remove the comment block from the following section to generate a plot of the circle generated.
This requires SmileLab. <http://www.satimage-software.com/en/currslrn.html>
It's free. For now. Makes pretty graphics. Fun.*)
(*
tell application "Smile"
activate
set w to make new graphic window
set p to make new curveplot at end of w
set p's limits to {-100, 100, -100, 100}
set c to make new curve at end of p
set c's ydata to yValueList
set c's xdata to xValueList
draw c
end tell
*)


set fref to path to startup disk as string
tell application "Finder"
activate
try
window 1
on error
open fref
end try
set p to position of window 1
set dataLength to length of yValueList
set step to round (dataLength / 64)
repeat with i from 1 to length of yValueList by step
set the position of window 1 to {((item 1 of p) - (item i of xValueList)), ((item 2 of p) + (item i of yValueList)) - radius}
end repeat
set position of window 1 to p
end tell
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.
  • Follow-Ups:
    • Re: Sine of the times: Much better window shaker
      • From: Doug McNutt <email@hidden>
    • Re: Bresenham's Circle
      • From: Paul Skinner <email@hidden>
  • Prev by Date: Re: date from eudora to appleworks
  • Next by Date: Re: How to make a background applet ?
  • Previous by thread: Re: Word scripting?
  • Next by thread: Re: Bresenham's Circle
  • Index(es):
    • Date
    • Thread