Sine & Cosine Definitions
The sine and cosine functions are defined on what is called the unit circle, which is a circle with a radius of 1 and centered at the origin, (0,0).
t is the length of an arc that starts at the right side of the circle and proceeds in a counter-clockwise direction. Cosine(t) and sine(t) give the x and y coordinates, respectively, of the end point of the arc. That is all there is to it.
There are a few important things to notice:
- t keeps going around and around the circle, and so the sine and cosine values keep repeating.
- cosine and sine values are always between 1 and -1
- for any t, the point (cos t, sin t) is a distance of 1 from the center
- the circumference of the unit circle is 2*pi
It helps to draw the unit circle when trying to get sine and cosine working in a program, especially when they are new to you.
Variables in the demos that represent a value for t will start with “t”, such as “tCar“.
Radians are also defined on the unit circle. In the diagram below, the angle rhas a size of t radians.
There are 2*pi radians in a full rotation. 2*pi radians = 360 degrees.
To convert from degrees to radians, multiply by (2 * pi) / 360 (or pi/180)
To convert from radians to degrees, multiply by 360 / (2 * pi) (or 180/pi)
Calculating t given x and y
t is calculated from x and y by the arc-tangent function. Director provides an arc-tangent function called atan(). Its definition is shown in the diagram above, with the following condition:
Since y/x = -y/-x and y/-x = -y/x, atan(y/x) will return the same values for the shaded quadrants, and the same values for the unshaded quadrants.
atan() doesn’t let you specify which quadrant you mean, and always returns a value on the right side of the circle, in other words between pi/2 and -pi/2.
This means that if x is negative (left side of circle), pi must be added to the result of atan(y/x) to get t.
A trick to doing this in a single expression is:
t = atan(y/x) + pi * (1 – x/abs(x)) / 2.0
This returns a t between -pi/2 and 3pi/2.
If x is zero, the divisions in the expression will cause errors. A few ways to handle x=0 are 1) set x to a very small number and use atan(y/x) or, more correctly, 2) don’t use atan() — if x=0, t is pi/2 if y is positive, or -pi/2 if y is negative.
There is an undocumented Lingo function, atan(y, x), that takes care of the two complications mentioned above. It returns a value between -pi and pi.
- Beginning SQL Joes 2 Pros: The SQL Hands-On Guide for Beginners (SQL Exam Prep Series 70-433 Volume 1 of 5) (Sql Design Series)
- Inside Microsoft® SQL Server® 2008: T-SQL Programming: T-SQL Programming (Pro-Developer)