B4J Question [BANano] ATan2D is not converted correct

Hi,

When I use this code in BANano:
B4X:
Angle = ATan2D((y2 - y1), (x2 - x1))

That will be converted to:
JavaScript:
__1fb=(Math.atan2((_y2-_y1)*(180/Math.PI)),(_x2-_x1))

It should be:
JavaScript:
__1fb=(Math.atan2((_y2-_y1),(_x2-_x1))*(180/Math.PI))

Please look at where the parentheses is located in the generated code. Math.atan2 only includes the (y2-y1) and not the (x2-x1)!
So in this case if (y2-y1) is zero then the Angel will be equal to (x2-x1) which is not correct. The angel in this case should be 0.

Edit:
ATan2 is also converted wrong!
JavaScript:
__1fb=(Math.atan2((_y2-_y1)),(_x2-_x1))

Best regards
Christian
 
Last edited:
Found a way around this problem:
B4X:
Dim y_diff As Double = (y2 - y1)
Dim x_diff As Double = (x2 - x1)

Angle = ATan2D(y_diff, x_diff)

Then it works ok
 
Upvote 0
Solution

emexes

Expert
Licensed User
Longtime User
Found a way around this problem:
What happens in the generated code if you put both call styles together, eg:
Atan2D:
TestAngle1 = ATan2D((y2 - y1), (x2 - x1))

Dim y_diff As Double = (y2 - y1)
Dim x_diff As Double = (x2 - x1)
TestAngle2 = ATan2D(y_diff, x_diff)
I don't understand why one way would work but not the other. Or why the problem hasn't been picked up previously. Although now that you can rotate panels via Android just by specifying the angle, perhaps the function doesn't get the workout that it used to. :oops:
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I did use the JavaScript formulas from somewhere, but I never really has a use for it so they may be wrong. If they are actually wrong, they will be corrected in the next version. At first sight, it looks that it may have something to do with the terms being in Degrees/Radials. Can you elaborate just a bit more beofre I make the change, as I thought I got the same values in B4J as in BANano?

Alwaysbusy
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
I did use the JavaScript formulas from somewhere ... Can you elaborate just a bit more before I make the change
I think I am misunderstanding the problem. I thought you were looking at the Java (not JavaScript) output from B4J, and that it was compiling differently (as in: correctly/incorrectly) when you used interim variables x_diff and y_diff.
 
Upvote 0
Hi,

I looked at the Javascript output in the console for a browser.
I did use the ATan2D((y2 - y1), (x2 - x1)) in my B4J project where it works fine but now when I'm trying to convert to BANano then the parentheses gets messed up and leaving only the y2-y1 within the atan2 converted Javascript. But I had some similar issues on other places in function calls with not only pure variables but then it has been missing parenthesis or extra parenthesis which results in a non working page.
This time it is the correct amount of parenthesis but on wrong places and gives a function that is not working as it should.

And @alwaysbusy it is not due to Radians/Degrees as you can see in my first post. If I use ATan2 I get the same result as ATan2D.
It has something to do with the parentheses within the function call it self.
Now I did a test also without the parenthesis and then it also works!
B4j:
B4X:
Angle = ATan2D(y2 - y1, x2 - x1)

Generated Javascript:
JavaScript:
__1fb=(Math.atan2(_y2-_y1,_x2-_x1)*(180/Math.PI))

I guess that when doing a function call it is best to only use interim values or at least without parenthesis in BANano to avoid problems.

Keep on the good work!
Best regards
Christian
 
Upvote 0
Top