Code Red > AutoLISP (Vanilla / Visual)
C++ to lisp
pedroantonio123:
Hi
Can someone please help translate the C ++ code in Lisp.
There is a code to go from UTM Coordinates to lang long.
Thanks for the Help
PedroAntonio
--- Code: --- public static void ToLatLon(double utmX, double utmY, string utmZone)
{
double latitude = 0;
double longitude = 0;
bool isNorthHemisphere = utmZone.Last() >= 'N';
var diflat = -0.00066286966871111111111111111111111111;
var diflon = -0.0003868060578;
var zone = int.Parse(utmZone.Remove(utmZone.Length - 1));
var c_sa = 6378137.000000;
var c_sb = 6356752.314245;
var e2 = Math.Pow((Math.Pow(c_sa, 2) - Math.Pow(c_sb, 2)), 0.5) / c_sb;
var e2cuadrada = Math.Pow(e2, 2);
var c = Math.Pow(c_sa, 2) / c_sb;
var x = utmX - 500000;
var y = isNorthHemisphere ? utmY : utmY - 10000000;
var s = ((zone * 6.0) - 183.0);
var lat = y / (6366197.724 * 0.9996); // Change c_sa for 6366197.724
var v = (c / Math.Pow(1 + (e2cuadrada * Math.Pow(Math.Cos(lat), 2)), 0.5)) * 0.9996;
var a = x / v;
var a1 = Math.Sin(2 * lat);
var a2 = a1 * Math.Pow((Math.Cos(lat)), 2);
var j2 = lat + (a1 / 2.0);
var j4 = ((3 * j2) + a2) / 4.0;
var j6 = (5 * j4 + a2 * Math.Pow((Math.Cos(lat)), 2)) / 3.0; // saque a2 de multiplicar por el coseno de lat y elevar al cuadrado
var alfa = (3.0 / 4.0) * e2cuadrada;
var beta = (5.0 / 3.0) * Math.Pow(alfa, 2);
var gama = (35.0 / 27.0) * Math.Pow(alfa, 3);
var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
var b = (y - bm) / v;
var epsi = ((e2cuadrada * Math.Pow(a, 2)) / 2.0) * Math.Pow((Math.Cos(lat)), 2);
var eps = a * (1 - (epsi / 3.0));
var nab = (b * (1 - epsi)) + lat;
var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
var delt = Math.Atan(senoheps / (Math.Cos(nab)));
var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
longitude = (delt / Math.PI) * 180 + s;
latitude = (((lat + (1 + e2cuadrada * Math.Pow(Math.Cos(lat), 2) - (3.0 / 2.0) * e2cuadrada * Math.Sin(lat) * Math.Cos(lat) * (tao - lat)) * (tao - lat))) / Math.PI) * 180; // era incorrecto el calculo
Console.WriteLine("Latitud: " + latitude.ToString() + "\nLongitud: " + longitude.ToString());
}
--- End code ---
Jeff H:
It looks like you have a C# snippet and not C++
irneb:
--- Quote from: Jeff H on December 01, 2014, 06:50:53 AM ---It looks like you have a C# snippet and not C++
--- End quote ---
Correct! That's definitely NOT C++. The use of var already shows something's different, if it was C++ (and only the newest C++) a similar idea would have used auto instead of var. But the clincher is the use of the Math library for the Pow method, if it was C++ it would have been a situation of adding a include clause for std::pow or math.h and then simply a function call to pow instead (not to mention if it was an object oriented library then it should have been Math->Pow instead of Math.Pow).
Anyhow, to get back to the OP's request, here's a start to show you the general idea of a direct translation:
--- Code - Auto/Visual Lisp: ---(defun ToLatLon (utmX utmY utmZone) (setq latitude 0.0 longitude 0.0 isNorthHemisphere (wcmatch utmZone "*[Nn]") diflat -0.00066286966871111111111111111111111111 diflon -0.0003868060578 zone (atoi (substr utmZone 1 (1- (strlen utm)))) c_sa 6378137.0 c_sb 6356752.314245 e2 (/ (expt (- (expt c_sa 2) (expt c_sb 2)) 0.5) c_sb) ;; Carry on with same principles - the in-line maths are simply changed to polish notation ) (princ "\nLatitud: ") (princ latitude) (princ "\nLongitud: ") (princ longitude))Just carry on doing th exact same things for the other variables also.
Edit: Just to ensure your Lisp code doesn't screw up something else - you would need to localize those variables. For an explanation of how and why see Lee's tut on the subject: http://www.lee-mac.com/localising.html
And a quick way to get VLIDE to help you localize them: http://www.lee-mac.com/quicklocalising.html
CAB:
This may be of interest.
https://github.com/jl2/utm/
irneb:
--- Quote from: CAB on December 01, 2014, 09:09:39 AM ---This may be of interest.
https://github.com/jl2/utm/
--- End quote ---
Very nice! Not to mention it does the reverse also: LatLon->UTM.
Though it's written in Common Lisp. Some of those things aren't available for AutoLisp - I mean stuff like the setf and hash tables would need to be a setq (i.e. re-create the entire list each time instead of just setting an item in it) and a normal association list. But the rest should be easy to translate to AL as is.
Navigation
[0] Message Index
[#] Next page
Go to full version