11 posts
• Page **1** of **2** • **1**, 2

It appears the expression atan2(y,x) isn't available in construct 2. Could we get it implemented? It's already available in javascript, but not in traditional expressions. I could code my own extension, but it's better if it were implemented as a SYSTEM expression.Fimbul 2011-08-17 23:44:48

**Posts:**532**Reputation:**6,903

B

36
S

8
G

8
It's used in the system angle() expression, which as far as I'm aware, is the only purpose for atan2 - calculating the angle between points.

If you really do need atan2 for something else, I'd be interested to know what it is! You can also get an atan2(y, x) anyway by doing angle(0, 0, x, y).

If you really do need atan2 for something else, I'd be interested to know what it is! You can also get an atan2(y, x) anyway by doing angle(0, 0, x, y).

**Posts:**24,550**Reputation:**195,527

Scirra Founder

B

400
S

237
G

89
[QUOTE=Fimbul] It appears the expression atan2(y,x) isn't available in construct 2.[/QUOTE]

[code]var a = Math.atan2(y, x);[/code]

I'm not sure if it is what you are looking for, but doesn't it work ?

If it doesn't, then the EightDir behavior where this is used is buggedKyatric 2011-08-18 13:37:28

[code]var a = Math.atan2(y, x);[/code]

I'm not sure if it is what you are looking for, but doesn't it work ?

If it doesn't, then the EightDir behavior where this is used is bugged

**Posts:**7,313**Reputation:**71,094

Moderator

B

294
S

117
G

96
The only known purpose of atan2(), as far as I know too, is to calculate the angle between two points, just like Ashley said. The proposed solution of using angle() works for me. Would be nice to have atan2() though, even as a hidden expression that must be typed, for when we're using premade formulas, but this is just a very minor inconvenience.

**Posts:**532**Reputation:**6,903

B

36
S

8
G

8
I found this thread looking how to calculate smallest difference between two angles (like this atan2(sin(x-y), cos(x-y)) ).

If you're looking for same thing just use awesome embedded construct function:

Unfortunately, I don't have enough reputation to post url to "System expressions" Construct 2 reference page

If you're looking for same thing just use awesome embedded construct function:

- Code: Select all
`// returns the smallest difference between two angles`

anglediff(a1, a2)

Unfortunately, I don't have enough reputation to post url to "System expressions" Construct 2 reference page

**Posts:**15**Reputation:**457

B

7
S

1
I'm using atan2 to find the distance between two points, following the code in this page:

http://www.movable-type.co.uk/scripts/latlong.html

The inputs for atan2 in the "Distance" section of that page can be used in C2 using the angle function, as mentioned by Ashley above. The syntax is angle(0,0,sqrt(1-a),sqrt(a)). On the website, the syntax is atan2(sqrt(a),sqrt(1-a)).

I've been able to get the function working, but the output is off by 10-20%. Not sure what's happening.

Has anyone used this successfully?

Here's my code:

http://www.movable-type.co.uk/scripts/latlong.html

The inputs for atan2 in the "Distance" section of that page can be used in C2 using the angle function, as mentioned by Ashley above. The syntax is angle(0,0,sqrt(1-a),sqrt(a)). On the website, the syntax is atan2(sqrt(a),sqrt(1-a)).

I've been able to get the function working, but the output is off by 10-20%. Not sure what's happening.

Has anyone used this successfully?

Here's my code:

**Posts:**88**Reputation:**1,503

B

17
S

2
@hmott

In C2 sin() and cos() take angles not degrees, so if the value is in radians you need to convert it to degrees inside sin and cos. Also in the context of this equation it expects angle() to give a value in radians instead of degrees, so you'll need to convert it.

So literally it would be this:

r = 6371000

phi1 = lat1*pi/180

phi2 = lat2*pi/180

deltaPhi = (lat2-lat1)*pi/180

deltaLambda = (lng1-lng2)*pi/180

a = sin(deltaPhi/2*180/pi)^2 + cos(phi1*180/pi)*cos(phi2*180/pi)*sin(deltaLambda/2*180/pi)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

But you can cut out most of the radian conversions and get just this:

r = 6371000

a = sin((lat2-lat1)/2)^2 + cos(lat1)*cos(lat2)*sin((lng1-lng2)/2)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

In C2 sin() and cos() take angles not degrees, so if the value is in radians you need to convert it to degrees inside sin and cos. Also in the context of this equation it expects angle() to give a value in radians instead of degrees, so you'll need to convert it.

So literally it would be this:

r = 6371000

phi1 = lat1*pi/180

phi2 = lat2*pi/180

deltaPhi = (lat2-lat1)*pi/180

deltaLambda = (lng1-lng2)*pi/180

a = sin(deltaPhi/2*180/pi)^2 + cos(phi1*180/pi)*cos(phi2*180/pi)*sin(deltaLambda/2*180/pi)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

But you can cut out most of the radian conversions and get just this:

r = 6371000

a = sin((lat2-lat1)/2)^2 + cos(lat1)*cos(lat2)*sin((lng1-lng2)/2)^2

c = 2*angle(0,0,sqrt(1-a),sqrt(a))*pi/180

d = r*c

**Posts:**5,359**Reputation:**73,779

B

94
S

33
G

114
@R0J0hound

That did the trick. I used your condensed method and got the exact results I needed.

Here's the calc done on the website:

Here's the code that calc's it:

Here's my app's calc:

What I wasn't able to get was the 0.1822 km result (or 182 m in my app's output). My app was generating an output of 211 m (or 0.211 km). Really close but wrong, not sure why. Anyway, I can move on thanks to you.

That did the trick. I used your condensed method and got the exact results I needed.

Here's the calc done on the website:

Here's the code that calc's it:

Here's my app's calc:

What I wasn't able to get was the 0.1822 km result (or 182 m in my app's output). My app was generating an output of 211 m (or 0.211 km). Really close but wrong, not sure why. Anyway, I can move on thanks to you.

**Posts:**88**Reputation:**1,503

B

17
S

2
11 posts
• Page **1** of **2** • **1**, 2

## Who is online |

Users browsing this forum: No registered users and 5 guests |