Re: Sine of the times: Much better window shaker
Re: Sine of the times: Much better window shaker
- Subject: Re: Sine of the times: Much better window shaker
- From: Doug McNutt <email@hidden>
- Date: Sun, 23 Mar 2003 12:47:29 -0700
At 11:09 -0500 3/23/03, Paul Skinner wrote:
>
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)
The sine/cosine technique avoids a whole lot of sign problems associated with square roots which are double valued and result in a bunch of decision making.
If one knows the sine and cosine of an angle there is a simple way to approximate the sine and cosine of an angle that is a little bigger. It's a Taylor series expansion for those who remember their first semester calculus. One does not need the hardware versions of the trig functions.
Without trying for perfect syntax:
set deltaphi = 0.03 -- the angular increment in radians
set const = (1 - deltaphi^2 / 2) -- watch out for a possible unary minus bug here!
-- ( - deltaphi^2 / 2 + 1) may give a different result if you use a negative deltaphi.
set phi = 0.0 -- the initial angle if you want it
set sinphi = 0.0 -- set initial parameters to match initial angle
set cosphi = 1.0
repeat
-- do whatever you want with the coordinates ycenter +radius * sinphi
-- and xcenter + radius * cosphi
set phi = phi + deltaphi -- the actual angle if you want it (stop the loop?)
set temporary = sinphi * const + cosphi * deltaphi
set cosphi = cosphi * const - sinphi * deltaphi
set sinphi = temporary
end repeat
Because the calculation is an approximation there will be accumulated errors but one ought to be able to go around the circle quite a few times before they become noticeable. Actually there will be a spiraling effect which might be interesting with bigger values of deltaphi. A value of 0.5 shows about 12 circles with smaller diameter each time around. (I cheated and did the plot in M$Excel)
*** Proof: ***
sin(x+y) = sin(x)*cos(y) + cos(x)*sin(y) -- identities from any trig book
cos(x+y) = cos(x)*cos(y) - sin(x)*sin(y)
sin(x) = x - x^3/3! + x^5/5! + . . . -> x -- Taylor series dropping terms above x^2
cox(x) = 1 - x^2/2 + x^4/4! - . . . -> 1 - x^2/2 -- valid for small values of x.
Substituting sin(x) and cos(x) in the trig identities produces the formulas in the loop above. It isn't hard to improve the accuracy by including higher order terms and figuring two new constants. The constants need to be calculated only once. Up to x^4 they would look like this:
set const1 =1-deltaphi^2/2 + deltaphi^4/24
set const2 =deltaphi - deltaphi^3/6
set temporary = sinphi * const1 + cosphi * const2
set cosphi = cosphi * const1 - sinphi * const2
set sinphi = temporary
5! = 5 * 4 * 3 * 2 * 1 A recursive subroutine to calculate the constants to an arbitrary limit is left as a stack overflowing exercise for the reader.
--
--> If you are presented a number as a percentage, and you do not clearly understand the numerator and the denominator involved, you are surely being lied to. <--
_______________________________________________
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.