Android Question Math.doDegrees equivalent

rboeck

Well-Known Member
Licensed User
Longtime User
Hi,

currently i try to convert this java code:
B4X:
 static double tile2lat(int y, int z) {
    double n = Math.PI - (2.0 * Math.PI * y) / Math.pow(2.0, z);
    return Math.toDegrees(Math.atan(Math.sinh(n)));
  }

to
B4X:
Sub tile2lat(y As Int,z As Int)
    Dim n As Double =cPI-(2.0*cPI*y) / Power(2.0,z)
    Return ToDegrees(ATan(ASin(n)))
End Sub

What is the b4x function for java Math.toDegrees and is the rest of my conversion ok?
 

wonder

Expert
Licensed User
Longtime User
Try this:
B4X:
Sub Process_Globals
        ...
    'Declare these constants as global variables
    Dim DEG_TO_RADIANS = 0.017453292519943295769236907684886127134428718885417 As Double
    Dim RAD_TO_DEGREES = 57.29577951308232087679815481410517033240547246656432 As Double
        ...
End Sub

Sub toDegrees(radians As Double) As Double
    Return radians * RAD_TO_DEGREES
End Sub

Sub toRadians(degrees As Double) As Double
    Return degrees * DEG_TO_RADIANS
End Sub
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
There is a mistake in your code. The Java code doesn't call Math.asin(n) (the arc-sine or inverse sine). It calls Math.sinh(n) (the hyperbolic sine). There's no built-in B4X function that performs the hyperbolic sine but you can easily build your own by appealing to the definition of the hyperbolic sine: https://en.wikipedia.org/wiki/Hyperbolic_function or using JavaObject to access Java's built-in hyperbolic sine function (you'll want to create-static a java.lang.Math object).
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Thanks for all helpers... my mathematical and english knowledge in combination is not good enough to unterstand the meaning of hyberbolic, asin etc.
Maybe i can better explain what i want to do: i want to use this page: mapire.eu on android. In the past this page used simple long and lat values inside the url, so that i could use my own position to search the position on the page.
Now the changed to another coodinate system, now they are using openstreetmap instead of google maps and the coordinates are gone and the url is in this way:

http://mapire.eu/de/map/secondsurve...33983821,1811421.0637459855,6214901.335736876
Maybe its easy for someone to calculate this values from the former values: lat: 47.89 long:14.76 zoom=9
Thanks for your interest and helping!
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You should enclose those two Power() calls in parentheses as, due to the order of operations, you are only dividing the latter Power() call by 2.
B4X:
     Return ATanD((Power(cE, n) - Power(cE, -n)) / 2)
 
Upvote 0
Top