I am in the process of converting a block of python code to java. Code determines sun and moon rise and set times.
95% of the code conversion is fine but I am stuck on converting the python array moonArray to java as its not making sense. Coding up the java version is a trival task and dont need help with that, just understanding what the python array is doing.
PYTHON CODE:
def riseSetString(self, dis, utcdis):
moonArray = self.risesetlist(dis, utcdis)
moonString = "moonrise time is " + moonArray[0]
moonString += "\nmoonset time is " + moonArray[1]
moonString += "\n{} iterations".format(moonArray[2])
print(moonString)
def risesetlist(self, dis, utcdis):
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = datetime.datetime.utcnow().hour
mins = datetime.datetime.utcnow().minute
second = datetime.datetime.utcnow().second
moonArray = self.calculate(y, m, d, h, mins, second)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
a = mlha
c = 1
ff = 0
while abs(c) > 0.000001:
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = utcmoon + a
moonArray = self.calculate(y, m, d, h, 0, 0)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
b = mlha
c = abs(b - a)
a = mlha
ff += 1
if ff >= 100:
break
moonset = mlha + utcmoon
c = 1
while abs(c) > 0.000001:
y = datetime.datetime.utcnow().year
m = datetime.datetime.utcnow().month
d = datetime.datetime.utcnow().day + dis
h = utcmoon - a
moonArray = self.calculate(y, m, d, h, 0, 0)
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
hmm = radians(-0.583) - msd
numerator = (sin(hmm) - sin(radians(self.localLat)) * sin(mdecl))
divisor = (cos(radians(self.localLat)) * cos(mdecl))
mlha = numerator / divisor
mlha = acos(mlha) * 12 * 15.0 / (15.04107 * pi)
utcmoon = (degrees(mra) - gmsto * 15 - self.localLong) / 15
b = mlha
c = b - a
a = mlha
ff += 1
if ff >= 200:
break
moonrise = utcmoon - mlha
moonrise = timeadjust(moonrise, utcdis)
moonrise2 = decimaltominsecs(moonrise)
moonset = timeadjust(moonset, utcdis)
moonset2 = decimaltominsecs(moonset)
return ["{}:{}:{}".format(floor(moonrise),
moonrise2[0], moonrise2[1]),
"{}:{}:{}".format(floor(moonset),
moonset2[0], moonset2[1]),
ff, [moonrise, moonrise2, moonset, moonset2]]
In the risesetlist function moonArray is called as follows:
moonArray = self.calculate(y, m, d, h, mins, second)
and the within a while loop
moonArray = self.calculate(y, m, d, h, 0, 0)
So how then can variables be assigned from array locations [8],[6],[11] as in both array constructors only 0..5 exist?
msd = moonArray[8]
mra = moonArray[5]
mdecl = moonArray[6]
gmsto = moonArray[11]
So, I just need to know what the moonArray is doing in python so I can code up the java version. Any help would be appreciated.
Related
This question already has an answer here:
Java Division error
(1 answer)
Closed 7 months ago.
I'm trying to make a "Distance & Speed to Time"
But the fraction is obstructing me to calculate the minute part.
Like... the hour part still have a fractional value somewhere.
This is what I want.
In this example Distance is 130 km and speed is 40 km/hr
Answer I want is
3 Hour(s) 15 Minute(s) 0 Second(s)
But what I get is
3 Hour(s) **0 Minute(s) 0 Second(s)**
float Bx = 130;
float By = 40;
int x = (int) Bx;
int y = (int) By;
var KeepSecond = (x / y * 3600) ;
var HourX = KeepSecond/3600;
int Hour = (int) HourX;
var MinuteX = (KeepSecond-(Hour*3600))/60;
int Minute = (int) MinuteX;
var SecondX = (KeepSecond-(Hour*3600)-(Minute*60));
int Second = (int) SecondX;
String result = String.format(Hour+" Hour(s) "+Minute+" Minute(s) "+Second+" Second(s) ");
Answer.setText(result);
You're doing an int division on distance/speed : 130/40 gives 3 with int types
You need the double or float division of 130.0/40.0 which gives expected 3.25
var KeepSecond = (Bx / By * 3600);
Also use meaningfull variable name and follow Java convention which is lowerCamelCase for variable (UpperCamelCase for class name)
float distance = 130;
float speed = 40;
var keepSecond = (distance / speed * 3600);
int hourInt = (int) keepSecond / 3600;
int minuteInt = (int) (keepSecond - (hourInt * 3600)) / 60;
int secondInt = (int) (keepSecond - (hourInt * 3600) - (minuteInt * 60));
That intermediate casting to ints is the problem.
Along with removing int x = (int) Bx; int y = (int) By;, I would make that code a little clearer and [conventionally] correct as follows:
float distance = 130f;
float speed = 40f;
int secondsInMinute = 60;
int minutesInHour = 60;
int secondsInHour = minutesInHour * secondsInMinute;
int timeInSeconds = (int) (distance / speed * secondsInHour); // formula: t = d/s
int hours = timeInSeconds / secondsInHour;
int minutes = (timeInSeconds - (hours * secondsInHour)) / secondsInMinute;
int seconds = timeInSeconds - ((hours * secondsInHour) + (minutes * secondsInMinute));
String result = String.format("%d Hour(s) %d Minute(s) %d Second(s)", hours, minutes, seconds);
Answer.setText(result);
I made a java implementation of the new perceptualy uniform color space JzAzBz. OSA publication is : https://www.osapublishing.org/oe/fulltext.cfm?uri=oe-25-13-15131&id=368272.
My java code is :
private double b = 1.15;
private double g = 0.66;
private double c1 = 3424 / Math.pow(2, 12);
private double c2 = 2413 / Math.pow(2, 7);
private double c3 = 2392 / Math.pow(2, 7);
private double n = 2610 / Math.pow(2, 14);
private double p = 1.7 * 2523 / Math.pow(2, 5);
private double d = -0.56;
private double d0 = 1.6295499532821566 * Math.pow(10, -11);
public void XYZToJab(double[] xyz, double[] jab) {
double[] XYZp = new double[3];
XYZp[0] = b * xyz[0] - ((b - 1) * xyz[2]);
XYZp[1] = g * xyz[1] - ((g - 1) * xyz[0]);
XYZp[2] = xyz[2];
double[] LMS = new double[3];
LMS[0] = 0.41478972 * XYZp[0] + 0.579999 * XYZp[1] + 0.0146480 * XYZp[2];
LMS[1] = -0.2015100 * XYZp[0] + 1.120649 * XYZp[1] + 0.0531008 * XYZp[2];
LMS[2] = -0.0166008 * XYZp[0] + 0.264800 * XYZp[1] + 0.6684799 * XYZp[2];
double[] LMSp = new double[3];
for (int i = 0; i < 3; i++) {
LMSp[i] = Math.pow((c1 + c2 * Math.pow((LMS[i] / 10000.0), n)) / (1 + c3 * Math.pow((LMS[i] / 10000.0), n)), p);
}
double[] Iab = new double[3];
Iab[0] = 0.5 * LMSp[0] + 0.5 * LMSp[1];
Iab[1] = 3.524000 * LMSp[0] - 4.066708 * LMSp[1] + 0.542708 * LMSp[2];
Iab[2] = 0.199076 * LMSp[0] + 1.096799 * LMSp[1] - 1.295875 * LMSp[2];
jab[0] = (((1 + d) * Iab[0]) / (1 + d * Iab[0])) - d0;
jab[1] = Iab[1];
jab[2] = Iab[2];
}
public void JabToXYZ(double[] jab, double[] xyz) {
double[] Iab = new double[3];
Iab[0] = (jab[0] + d0) / (1 + d - d * (jab[0] + d0));
Iab[1] = jab[1];
Iab[2] = jab[2];
double[] LMSp = new double[3];
LMSp[0] = 1.0 * Iab[0] + 0.13860504 * Iab[1] + 0.05804732 * Iab[2];
LMSp[1] = 1.0 * Iab[0] - 0.13860504 * Iab[1] - 0.05804732 * Iab[2];
LMSp[2] = 1.0 * Iab[0] - 0.09601924 * Iab[1] - 0.81189190 * Iab[2];
double[] LMS = new double[3];
for (int i = 0; i < 3; i++) {
LMS[i] = 10000 * Math.pow((c1 - Math.pow(LMSp[i], 1 / p)) / ((c3 * Math.pow(LMSp[i], 1 / p)) - c2), 1 / n);
}
double[] XYZp = new double[3];
XYZp[0] = 1.92422644 * LMS[0] - 1.00479231 * LMS[1] + 0.03765140 * LMS[2];
XYZp[1] = 0.35031676 * LMS[0] + 0.72648119 * LMS[1] - 0.06538442 * LMS[2];
XYZp[2] = -0.09098281 * LMS[0] - 0.31272829 * LMS[1] + 1.52276656 * LMS[2];
xyz[0] = (XYZp[0] + (b - 1) * XYZp[2]) / b;
xyz[1] = (XYZp[1] + (g - 1) * XYZp[0]) / g;
xyz[2] = XYZp[2];
}
When I test it running XYZToJab and then JabToXYZ I get a good precision for X and Z (delta order is E-9) but for Y I get a bad precision (delta order is 1-5%).
Is there anyone who can help me ?
The implementation is almost correct: The error lies in JabToXYZ where the prior to last line should be changed from
(XYZp[1] + (g - 1) * XYZp[0]) / g;
to
(XYZp[1] + (g - 1) * xyz[0]) / g;
However the fact that you are using rounded invert matrices to 6 decimal places in the JabToXYZ function will prevent you to get a clean inversion. You should try to compute the inverse at full double precision:
>>> import numpy as np
>>> np.set_printoptions(formatter={'float': '{:0.15f}'.format})
>>> import colour.models.jzazbz
>>> colour.models.jzazbz.JZAZBZ_IZAZBZ_TO_LMS_P_MATRIX
array([[1.000000000000000, 0.138605043271539, 0.058047316156119],
[1.000000000000000, -0.138605043271539, -0.058047316156119],
[1.000000000000000, -0.096019242026319, -0.811891896056039]])
>>> colour.models.jzazbz.JZAZBZ_LMS_TO_XYZ_MATRIX
array([[1.924226435787607, -1.004792312595365, 0.037651404030618],
[0.350316762094999, 0.726481193931655, -0.065384422948085],
[-0.090982810982848, -0.312728290523074, 1.522766561305260]])
I am making an application for teachers that you put in the max number of points and it tells you the percent of each amount wrong. I then would like to use that percentage to make each of the textviews display the grade with the corresponding percentage. So the first column is the amount right out of the max, the second column is the percentage, and the third column is the letter grade. I then have eight rows, but would like to expand that in the future. I know that I would have to an if to say if the percent is between two numbers, settext to a letter grade, but would I have to do the same for every textview, or what other way would there be to do this?
This is the code for what I basically have to far:
MaxInt = Integer.parseInt(number);
minus1 = MaxInt - 1;
minus2 = MaxInt - 2;
minus3 = MaxInt - 3;
minus4 = MaxInt - 4;
minus5 = MaxInt - 5;
minus6 = MaxInt - 6;
minus7 = MaxInt - 7;
minus8 = MaxInt - 8;
float PD1 = (minus1 * 100.0f) / MaxInt;
float PD2 = (minus2 * 100.0f) / MaxInt;
float PD3 = (minus3 * 100.0f) / MaxInt;
float PD4 = (minus4 * 100.0f) / MaxInt;
float PD5 = (minus5 * 100.0f) / MaxInt;
float PD6 = (minus6 * 100.0f) / MaxInt;
float PD7 = (minus7 * 100.0f) / MaxInt;
float PD8 = (minus8 * 100.0f) / MaxInt;
int D1 = (int) PD1;
int D2 = (int) PD2;
int D3 = (int) PD3;
int D4 = (int) PD4;
int D5 = (int) PD5;
int D6 = (int) PD6;
int D7 = (int) PD7;
int D8 = (int) PD8;
P1 = String.valueOf(D1);
P2 = String.valueOf(D2);
P3 = String.valueOf(D3);
P4 = String.valueOf(D4);
P5 = String.valueOf(D5);
P6 = String.valueOf(D6);
P7 = String.valueOf(D7);
P8 = String.valueOf(D8);
TV1.setText(Integer.toString(minus1));
TV2.setText(Integer.toString(minus2));
TV3.setText(Integer.toString(minus3));
TV4.setText(Integer.toString(minus4));
TV5.setText(Integer.toString(minus5));
TV6.setText(Integer.toString(minus6));
TV7.setText(Integer.toString(minus7));
TV8.setText(Integer.toString(minus8));
TV9.setText(P1 + "%");
TV10.setText(P2 + "%");
TV11.setText(P3 + "%");
TV12.setText(P4 + "%");
TV13.setText(P5 + "%");
TV14.setText(P6 + "%");
TV15.setText(P7 + "%");
TV16.setText(P8 + "%");
I know that some of that isn't nessesary like converting the float to a integer, then string, but I will fix that when I am done. TV17 - TV24 will be the letter grades.
I'm wanting to use java to help me quickly solve an equation to get x and y on a trilateration project i'm working on.
I get tot his point where the only numbers that i need to change is the ones i'be labelled (a) and (b)
120x - 15600 + 40y - 4400 = 1200(a)
y = -3x + 530
-180x + 20700 + 60y - 8100 = -2400(b)
y = 3x - 250
so i continue to solve and will get the answer
-3x + 530 = 3x - 250
780 = 6x
130 = x
y = 3(130) - 250
y = 140
x= 130, y = 140
I just need java to work out the equation and then be able to change the two values (a) and (b)
so im asking for a little bit of help as to whether or not i can do this? and could someone help me out please? im not a great programmer as its not my field
This is just general math:
Ax + By = C
Dx + Ey = F
// From first formula
x = (C - By) / A
// Applied to second formula
D * ((C - By) / A) + Ey = F
D * C / A - D * By / A + Ey = F
D * C / A + (E - D * B / A) * y = F
y = (F - D * C / A) / (E - D * B / A)
= (A * F - D * C) / (A * E - D * B)
In your case:
120x - 15600 + 40y - 4400 = 1200
-180x + 20700 + 60y - 8100 = -2400
A = 120 B = 40 C = 1200 + 15600 + 4400 = 21200
D = -180 E = 60 F = -2400 - 20700 + 8100 = -15000
y = (A * F - D * C) / (A * E - D * B)
= (120 * -15000 - -180 * 21200) / (120 * 60 - -180 * 40)
= 2016000 / 14400
= 140
x = (C - By) / A
= (21200 - 40 * 140) / 120
= 15600 / 120
= 130
Anyway, I digress, you want:
120x - 15600 + 40y - 4400 = a
-180x + 20700 + 60y - 8100 = b
// Normalized
(A= 120)x + (B=40)y = (C=a + 15600 + 4400)
(D=-180)x + (E=60)y = (F=b - 20700 + 8100)
C = a + 15600 + 4400 = a + 20000
F = b - 20700 + 8100 = b - 12600
y = (A * F - D * C) / (A * E - D * B)
= (120 * (b - 12600) - -180 * (a + 20000)) / (120 * 60 - -180 * 40)
= ((120 * b - 120 * 12600) - (-180 * a + -180 * 20000)) / 14400
= (120 * b - 1512000 + 180 * a + 3600000) / 14400
= 120 * b / 14400 + 180 * a / 14400 + (3600000 - 1512000) / 14400
= b / 120 + a / 80 + 145
x = (C - By) / A
= (a + 20000 - 40 * y) / 120
= a / 120 + 20000 / 120 - 40 * y / 120
= (a / 40 - y + 500) / 3
Verifying formulas with your numbers:
y = b / 120 + a / 80 + 145
= -2400 / 120 + 1200 / 80 + 145
= 140
x = (a / 40 - y + 500) / 3
= (1200 / 40 - 140 + 500) / 3
= 130
Or in Java code:
double a = 1200;
double b = -2400;
double y = b / 120 + a / 80 + 145;
double x = (a / 40 - y + 500) / 3;
System.out.println("x = " + x + ", y = " + y);
IDEONE
You can reorganize (a) and (b) so that the constants are all on the right side of the equation
120x + 40y = 21200(a)
-180x + 60y = -15000(b)
Let's name 120 the coefficient of x, 40 the coefficient of y and 21200 as the constant for equation a. The similar goes for equation b. And use these names as variables for the program.
One way to solve the two unknown variables in a binary quadric equation is to eliminate one unknown variables by manipulate the coefficient and constants in the two equations and combine them to get rid of one unknown variable, which you have shown in your question.
As shown by you, you eliminated x first and then solve for y. In the following program, I eliminated y first and then solve for x. But essentially, they are the same.
Note the program assumes that the coefficients and constants are integers.
public class Equation {
public static void main(String[] args) {
int coefficient_x_1, coefficient_y_1, constant_1, coefficient_x_2, coefficient_y_2, constant_2;
Scanner in = new Scanner(System.in);
// 1. get the inputs
coefficient_x_1 = in.nextInt();
coefficient_y_1 = in.nextInt();
constant_1 = in.nextInt();
coefficient_x_2 = in.nextInt();
coefficient_y_2 = in.nextInt();
constant_2 = in.nextInt();
// 2. try to eliminate x from the two equations to get the value of y
// 2.1 get the least common multiplier of the two coefficient of x in the two equations
int leastCommonMultiplier =
Math.abs(coefficient_x_1) * Math.abs(coefficient_x_2) / getMaxCommonFactor(Math.abs(coefficient_x_1), Math.abs(coefficient_x_2));
int cancellationFactor = -1;
if (coefficient_x_1 * coefficient_y_1 < 0) cancellationFactor = 1;
int multiplier_1 = leastCommonMultiplier / coefficient_x_1;
int multiplier_2 = cancellationFactor * leastCommonMultiplier / coefficient_x_2;
// 2.2 eleminate x and solve for y
int y = (constant_1 * multiplier_1 + constant_2 * multiplier_2) / (coefficient_y_1 * multiplier_1 + coefficient_y_2 * multiplier_2);
// 3. get the value of x based on the value of y
int x = (constant_1 - coefficient_y_1 * y) / coefficient_x_1;
System.out.println("x is : " + x);
System.out.println("y is : " + y);
in.close();
}
public static int getMaxCommonFactor(int a, int b) {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
while (a % b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return b;
}
}
I'm having trouble porting movable-type's latlong-gridref from JavaScript to Java. I've made a little headway but stuck now.
Issues I'm having with porting code:
I'm a little lost porting the OsGridRef function ( can't really make heads or tails of it)
Similarly can't figure out how to sort out LatLon from osGridToLatLong function
Purpose of code:
To convert Ordnance Survey grid reference easting/northing coordinates to (OSGB36) latitude/longitude
Link to original JavaScript:
http://www.movable-type.co.uk/scripts/latlong-gridref.html
Original JavaScript and code I've managed to port so far is below:
Original
/**
* Creates a OsGridRef object
*
* #constructor
* #param {Number} easting: Easting in metres from OS false origin
* #param {Number} northing: Northing in metres from OS false origin
*/
function OsGridRef(easting, northing) {
this.easting = parseInt(easting, 10);
this.northing = parseInt(northing, 10);
}
/**
* Convert Ordnance Survey grid reference easting/northing coordinate to (OSGB36) latitude/longitude
*
* #param {OsGridRef} easting/northing to be converted to latitude/longitude
* #return {LatLon} latitude/longitude (in OSGB36) of supplied grid reference
*/
OsGridRef.osGridToLatLong = function(gridref) {
var E = gridref.easting;
var N = gridref.northing;
var a = 6377563.396, b = 6356256.909; // Airy 1830 major & minor semi-axes
var F0 = 0.9996012717; // NatGrid scale factor on central meridian
var lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180; // NatGrid true origin
var N0 = -100000, E0 = 400000; // northing & easting of true origin, metres
var e2 = 1 - (b*b)/(a*a); // eccentricity squared
var n = (a-b)/(a+b), n2 = n*n, n3 = n*n*n;
var lat=lat0, M=0;
do {
lat = (N-N0-M)/(a*F0) + lat;
var Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);
var Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0);
var Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0));
var Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0));
M = b * F0 * (Ma - Mb + Mc - Md); // meridional arc
} while (N-N0-M >= 0.00001); // ie until < 0.01mm
var cosLat = Math.cos(lat), sinLat = Math.sin(lat);
var nu = a*F0/Math.sqrt(1-e2*sinLat*sinLat); // transverse radius of curvature
var rho = a*F0*(1-e2)/Math.pow(1-e2*sinLat*sinLat, 1.5); // meridional radius of curvature
var eta2 = nu/rho-1;
var tanLat = Math.tan(lat);
var tan2lat = tanLat*tanLat, tan4lat = tan2lat*tan2lat, tan6lat = tan4lat*tan2lat;
var secLat = 1/cosLat;
var nu3 = nu*nu*nu, nu5 = nu3*nu*nu, nu7 = nu5*nu*nu;
var VII = tanLat/(2*rho*nu);
var VIII = tanLat/(24*rho*nu3)*(5+3*tan2lat+eta2-9*tan2lat*eta2);
var IX = tanLat/(720*rho*nu5)*(61+90*tan2lat+45*tan4lat);
var X = secLat/nu;
var XI = secLat/(6*nu3)*(nu/rho+2*tan2lat);
var XII = secLat/(120*nu5)*(5+28*tan2lat+24*tan4lat);
var XIIA = secLat/(5040*nu7)*(61+662*tan2lat+1320*tan4lat+720*tan6lat);
var dE = (E-E0), dE2 = dE*dE, dE3 = dE2*dE, dE4 = dE2*dE2, dE5 = dE3*dE2, dE6 = dE4*dE2, dE7 = dE5*dE2;
lat = lat - VII*dE2 + VIII*dE4 - IX*dE6;
var lon = lon0 + X*dE - XI*dE3 + XII*dE5 - XIIA*dE7;
return new LatLon(lat.toDeg(), lon.toDeg());
}
What I've managed to port
LatLon osGridToLatLong(OsGridRef osGridRef) {
int E = osGridRef.easting;
int N = osGridRef.northing;
double a = 6377563.396, b = 6356256.910; // Airy 1830 major & minor semi-axes
double F0 = 0.9996012717; // NatGrid scale factor on central meridian
double lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180; // NatGrid true origin
double N0 = -100000, E0 = 400000; // northing & easting of true origin, metres
double e2 = 1 - (b*b)/(a*a); // eccentricity squared
double n = (a-b)/(a+b), n2 = n*n, n3 = n*n*n;
double lat=lat0, M=0;
int count = 0;
do {
count++;
lat = (N-N0-M)/(a*F0) + lat;
double Ma = (1 + n + (5.0/4)*n2 + (5.0/4)*n3) * (lat-lat0);
double Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0);
double Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0));
double Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0));
M = b * F0 * (Ma - Mb + Mc - Md); // meridional arc
} while (N-N0-M >= 0.00001); // ie until < 0.01mm
double cosLat = Math.cos(lat), sinLat = Math.sin(lat);
double nu = a*F0/Math.sqrt(1-e2*sinLat*sinLat); // transverse radius of curvature
double rho = a*F0*(1-e2)/Math.pow(1-e2*sinLat*sinLat, 1.5); // meridional radius of curvature
double eta2 = nu/rho-1;
double tanLat = Math.tan(lat);
double tan2lat = tanLat*tanLat, tan4lat = tan2lat*tan2lat, tan6lat = tan4lat*tan2lat;
double secLat = 1/cosLat;
double nu3 = nu*nu*nu, nu5 = nu3*nu*nu, nu7 = nu5*nu*nu;
double VII = tanLat/(2*rho*nu);
double VIII = tanLat/(24*rho*nu3)*(5+3*tan2lat+eta2-9*tan2lat*eta2);
double IX = tanLat/(720*rho*nu5)*(61+90*tan2lat+45*tan4lat);
double X = secLat/nu;
double XI = secLat/(6*nu3)*(nu/rho+2*tan2lat);
double XII = secLat/(120*nu5)*(5+28*tan2lat+24*tan4lat);
double XIIA = secLat/(5040*nu7)*(61+662*tan2lat+1320*tan4lat+720*tan6lat);
double dE = (E-E0), dE2 = dE*dE, dE3 = dE2*dE, dE4 = dE2*dE2, dE5 = dE3*dE2, dE6 = dE4*dE2, dE7 = dE5*dE2;
lat = lat - VII*dE2 + VIII*dE4 - IX*dE6;
double lon = lon0 + X*dE - XI*dE3 + XII*dE5 - XIIA*dE7;
return new LatLon(lat.toDeg(), lon.toDeg());
}
Do you have any experience with Javascript? Just looking at what you have posted, I can tell you that OsGridRef is a class. To make that is Java:
class OsGridRef {
public int easting;
public int northing;
public OsGridRef (String easting, String northing) {
this.easting = Interger.parseInt(easting);
this.northing = Integer.parseInt(northing);
}
}
That would work with the code you have, even if it's not best practice, but I'm not sure what to tell you about the rest. toDeg() probably comes from some library that's being used, so you'll have to figure out where that came from in order to port that. If I knew what it was before converting it to degrees I could offer some guidance, but just looking at it I have no idea.