# Algorithms that convert x and y value to an angle (degrees or radian.

Page 1 of 1

## 10 Replies - 1926 Views - Last Post: 17 March 2013 - 03:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=312944&amp;s=2e6ad5b02fbc59d2d9450b146c5b51da&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JeffreyR

Reputation: 0
• Posts: 5
• Joined: 20-February 13

# Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 02:58 AM

Hi i am new on the forum. I am making a smartphone application and this is the first time i’m working with algorithms.
I am looking for algorithms that can convert x and y value on a Cartesian plane (x,y) to an angle. The angle can be 0-360 degrees or 0-2π radian.

Does anyone know of such algorithms?
I have looked for geometric algorithms on Google. I have also found a list of algorithms on Wikipedia, but no luck finding this particular algorithm.
Also are there search databases that solely contain algorithms?

Here can you see a simple visual representation:

Is This A Good Question/Topic? 0

## Replies To: Algorithms that convert x and y value to an angle (degrees or radian.

### #2 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 03:53 AM

Can you be more specific about what you want this algorithm to do? Are you getting the angle from one point to another?

### #3 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 08:30 AM

You should know about vectors. These are the primary representations that we use to calculate angles, or more precisely, the relationship and manipulation a of arbitrary points in either 2D or 3D space.

How is you linear algebra?

### #4 JeffreyR

Reputation: 0
• Posts: 5
• Joined: 20-February 13

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 08:39 AM

stayscrisp, on 21 February 2013 - 03:53 AM, said:

Can you be more specific about what you want this algorithm to do? Are you getting the angle from one point to another?

Okej i'll try a different approach. See new image.

For example in the image if have a coordinate (1.5,2).
From this coordinates i want to calculate angle z.
Numbers on the x and y axis can be positive or negative.

The algorithm:

Input: An x and y value. (For example input (-1.5,5).
Output: angle (z). (105 degrees) (i don't know if this is right just example />)

### #5 JeffreyR

Reputation: 0
• Posts: 5
• Joined: 20-February 13

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 08:45 AM

ButchDean, on 21 February 2013 - 08:30 AM, said:

You should know about vectors. These are the primary representations that we use to calculate angles, or more precisely, the relationship and manipulation a of arbitrary points in either 2D or 3D space.

How is you linear algebra?

You are thinking to difficult />

### #6 stayscrisp

• フカユ

Reputation: 1040
• Posts: 4,325
• Joined: 14-February 08

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 21 February 2013 - 09:12 AM

The function you need is atan2, google it />

### #7 JeffreyR

Reputation: 0
• Posts: 5
• Joined: 20-February 13

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 22 February 2013 - 01:31 AM

stayscrisp, on 21 February 2013 - 09:12 AM, said:

The function you need is atan2, google it />/>

I know how i can calculate this angle with atan2. I know math
I was not asking for a function how to calculate it. I was asking for a algorithm example.
Because i am learning about algorithms.

I have posted this question on 5 diffent forums.
I have gotten few algorithms for calculating this angle.
This helps me understand algorithms.
_____________________________________________________
From johng on mathhelpforum.com
____________________________________________________
double angle(x,y)
d=x*x+y*y
if (d is 0) return error
d=sqrt(d)
v=arccos(x/d)
if (y<0) v=2*pi-v
return v
________________________________________________________
_________________________________________________________
Q1: If x > 0, y > 0 ----> θ = arctan(y/x)
Q2: If x < 0, y > 0 ----> θ = arctan(y/x) + 180° or arctan(y/x) + π
Q3: If x < 0, y < 0 ----> θ = arctan(y/x) + 180° or arctan(y/x) + π
Q4: If x > 0, y < 0 ----> θ = arctan(y/x) + 360° or arctan(y/x) + 2π

If you guys know another algorithm?
The more the better. I want to analyse the difference between them.
_________________________________________________________

### #8 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 22 February 2013 - 12:21 PM

Well, when I suggested linear algebra you said I was overthinking it. Go figure.

How do you intend analyzing these algorithms? They're pretty basic.

### #9 JeffreyR

Reputation: 0
• Posts: 5
• Joined: 20-February 13

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 17 March 2013 - 07:48 AM

Is it so hard to give direct answer to my question?

"No i don't know any algorithms"

I'm doing a application Android application test. I'm testing battery use and speed of algorithms using different sensors.
I have already made 2 algorithms myself and collected 4 algorithms from forums and researchliterature.

So i'm done. Despite the unneccesary discussion, thank you for your time.

ButchDean, on 22 February 2013 - 12:21 PM, said:

Well, when I suggested linear algebra you said I was overthinking it. Go figure.

How do you intend analyzing these algorithms? They're pretty basic.

### #10 luckielordie

Reputation: 0
• Posts: 21
• Joined: 18-October 11

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 17 March 2013 - 10:55 AM

Try researching "Complex Conjugate" and Complex numbers. More specifically "Agrand Diagrams". The only info I have is on my unis portal so I can't link you but there should be plenty of other info out there!

Good luck!

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12298
• Posts: 45,399
• Joined: 27-December 08

## Re: Algorithms that convert x and y value to an angle (degrees or radian.

Posted 17 March 2013 - 03:56 PM

JeffreyR, on 22 February 2013 - 04:31 AM, said:

_________________________________________________________
Q1: If x > 0, y > 0 ----> θ = arctan(y/x)
Q2: If x < 0, y > 0 ----> θ = arctan(y/x) + 180° or arctan(y/x) + π
Q3: If x < 0, y < 0 ----> θ = arctan(y/x) + 180° or arctan(y/x) + π
Q4: If x > 0, y < 0 ----> θ = arctan(y/x) + 360° or arctan(y/x) + 2π

If you guys know another algorithm?
The more the better. I want to analyse the difference between them.
_________________________________________________________

This is the atan2() function.

Quote

Is it so hard to give direct answer to my question?

In the context of your question, the math and algorithms are one and the same. An algorithm is just a finite series of steps that return a specific result. As such, the math falls under the scope of an algorithm here. The inverse trig functions return angles, and are defined on restricted domains. You can get the correct values on all the domains by breaking them up piecewise and compensating appropriately. That's how the atan2() function works, as well as your approach with inverse cosine. You could play around with inverse sine in the same way.

You were getting a lot of direct and relevant answers on this.