# Thread: vidoe gamer programmer needs help with a trigonometry qustion.

1. ## vidoe gamer programmer needs help with a trigonometry qustion.

Hello it has been quite some time since I was in high school math and I have a trigonometry question that I need help with for an Iphone game I have been working on. I need a function that takes 2 variables as inputs and return an angle (radians or degrees) as an output. Here is a simple chart of the inputs and outputs

input x | input y | output radians (output degrees)
0 | 1 | 1.57(180)
0 | -1 | 0
1 | 0 | .78(90)
.5 | -.5 | .39 (45)
-1 | 0 | 2.34(270)

Does that chart make sense? Let me know if you need additional clarifications. I supple the input and output and you supply me the function that created the outputs from the following inputs. I am not sure but I think ArcTan needs to be used. Thanks in advance.

2. What is the x-input and y-input? What do they represent?

3. It seems that the function should return the angle α such that vector (0, -1) rotated by α counterclockwise becomes vector (x, y).

Edit: The rotated vector (0, -1) does not have to become (x, y), but it should be parallel to it and point in the same direction.

4. Originally Posted by pickslides
What is the x-input and y-input? What do they represent?
The Iphone has 3 accelmeters in it. I care about 2 of them. The X and Y inputs represents the force of gravity in the x and y direction. They both range from 1 to -1. (example upright =1 and upside-down =-1 for the y.) So I assume the only thing that is relevant it the ratio of x and y and both of there signs.

Originally Posted by emakarov
It seems that the function should return the angle α such that vector (0, -1) rotated by α counterclockwise becomes vector (x, y).

Edit: The rotated vector (0, -1) does not have to become (x, y), but it should be parallel to it and point in the same direction.
I am not 100% sure what you mean by this. I suppose you could think of the input as a vector instead of 2 numbers.

5. I am not 100% sure what you mean by this. I suppose you could think of the input as a vector instead of 2 numbers.
Well, I believethe description concerning vector rotation is pretty clear. If you don't understand it, please point out exactly which word or phrase is not clear.

In trigonimetry it is customary to count angles from the OX axis, and positive angles rotate counterclockwise. E.g., OY axis makes +90 degrees. Here is a function that I believe returns the angle between OX and the given vector (x, y); the return angle is in the range 0 ... 360 degrees.

Code:
f(x,y)  =
if x = 0
if y > 0 return 90
else if y = 0 return undefined
else if y < 0 return 270
else
let a = arctan(y / x) in
if x > 0
if y >= 0 return a // first quadrant
else return 360 + a // fourth quadrant
else return 180 + a // second and third quadrants
Now, angles between (0, -1) and (x, y) are computed by g(x,y) where

g(x,y) = f(-y,x)

This needs to be tested.

Edit: formatting.