Author Topic: C++ to lisp  (Read 9697 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio123

  • Guest
C++ to lisp
« on: December 01, 2014, 05:45:00 AM »
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: [Select]
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());

    }

Jeff H

  • Needs a day job
  • Posts: 6150
Re: C++ to lisp
« Reply #1 on: December 01, 2014, 06:50:53 AM »
It looks like you have a C# snippet and not C++

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #2 on: December 01, 2014, 07:30:42 AM »
It looks like you have a C# snippet and not C++
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: [Select]
  1. (defun ToLatLon (utmX utmY utmZone)
  2.   (setq latitude 0.0
  3.         longitude 0.0
  4.         isNorthHemisphere (wcmatch utmZone "*[Nn]")
  5.         diflat -0.00066286966871111111111111111111111111
  6.         diflon -0.0003868060578
  7.         zone (atoi (substr utmZone 1 (1- (strlen utm))))
  8.         c_sa 6378137.0
  9.         c_sb 6356752.314245
  10.         e2 (/ (expt (- (expt c_sa 2) (expt c_sb 2)) 0.5) c_sb)
  11.         ;; Carry on with same principles - the in-line maths are simply changed to polish notation
  12.   )
  13.   (princ "\nLatitud: ")
  14.   (princ latitude)
  15.   (princ "\nLongitud: ")
  16.   (princ longitude)
  17. )
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
« Last Edit: December 01, 2014, 08:03:21 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: C++ to lisp
« Reply #3 on: December 01, 2014, 09:09:39 AM »
This may be of interest.
https://github.com/jl2/utm/
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #4 on: December 01, 2014, 09:22:51 AM »
This may be of interest.
https://github.com/jl2/utm/
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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

ymg

  • Guest
Re: C++ to lisp
« Reply #5 on: December 01, 2014, 11:31:05 AM »
There is also the proj.lsp library by David Allison in Autolisp which can be downloaded
here:  http://www.usouthal.edu/geography/allison/mappro/mappro.zip

Does Transverse Mercator, Polyconic, Lambert Conformal Conic, Alber's Equal Area, and Mercator.

ymg

pedroantonio123

  • Guest
Re: C++ to lisp
« Reply #6 on: December 02, 2014, 01:53:54 AM »
Thanks for the help. I tried the convert. Everything I have not done. Can someone please help me?

Code: [Select]
        var e2cuadrada = Math.Pow(e2, 2);
(setq e2cuadrada (expt e2 2))
 
        var c = Math.Pow(c_sa, 2) / c_sb;
(setq c (/ (expt c_sa 2) c_sb)
 
        var x = utmX - 500000;
(setq x (- utmX 500000))
 
        var y = isNorthHemisphere ? utmY : utmY - 10000000;
(setq y (if isNorthHemisphere (/ utmY (- utmY 10000000))))
 
        var s = ((zone * 6.0) - 183.0);
(setq s (- (* zone 6.0) 183.0))
 
        var lat = y / (6366197.724 * 0.9996); // Change c_sa for 6366197.724
(setq lat (/ y (* 6366197.724 0.9996)))
 
        var v = (c / Math.Pow(1 + (e2cuadrada * Math.Pow(Math.Cos(lat), 2)), 0.5)) * 0.9996;
(setq v (* (/ c (expt (+ 1 (* e2cuadrada (expt (cos lat) 2) 0.5)) 0.9996))
 
        var a = x / v;
(setq a (/ x v))
 
        var a1 = Math.Sin(2 * lat);
(setq a1 (sin (* 2 lat)))
 
        var a2 = a1 * Math.Pow((Math.Cos(lat)), 2);
(setq a2 (* a1 (expt (cos lat) 2)))
 
        var j2 = lat + (a1 / 2.0);
(setq j2 (+ lat (/ a1 2.0)))
 
        var j4 = ((3 * j2) + a2) / 4.0;
(setq j4 (/ (+ (* 3 j2) a2) 4.0))
 
        var j6 = (5 * j4 + a2 * Math.Pow((Math.Cos(lat)), 2)) / 3.0;
(setq j6 (/ (+ (* 5 j4) (* a2 (expt (cos lat) 2))) 3.0))
 
        var alfa = (3.0 / 4.0) * e2cuadrada;
(setq alfa (* (/ 3.0 4.0) e2cuadrada))
 
        var beta = (5.0 / 3.0) * Math.Pow(alfa, 2);
(setq beta (* (/ 5.0 3.0) (expt alpfa 2)))       
       
        var gama = (35.0 / 27.0) * Math.Pow(alfa, 3);
(setq gama (* (/ 35.0 27.0) (expt alfa 3)))
 
        var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
(setq bm (* 0.9996 c (- (+ (- lat (* alfa j2)) (* beta j4)) (* gama j6))))   
       
        var b = (y - bm) / v;
(setq b (/ (- y bm) v))       
       
        var epsi = ((e2cuadrada * Math.Pow(a, 2)) / 2.0) * Math.Pow((Math.Cos(lat)), 2);
(setq epsi (* (/ (* e2cuadrada (expt a 2)) 2.0) (expt (cos lat) 2)))
 
        var eps = a * (1 - (epsi / 3.0));
(setq eps (* a (- 1 (/ epsi 3.0))))       
       
        var nab = (b * (1 - epsi)) + lat;
(setq nab (+ (* b (1- epsi)) lat))
 
        var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
(setq senoheps    (/ ????
   
        var delt = Math.Atan(senoheps / (Math.Cos(nab)));
(setq delt (/ ???)
 
        var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
(setq tao (* ???       
 
        longitude = (delt / Math.PI) * 180 + s;
(setq longitude (+ (* (/ delt 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
(setq latitude ????



irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #7 on: December 02, 2014, 02:34:51 AM »
Thanks for the help. I tried the convert. Everything I have not done. Can someone please help me?
Here's one error in that translation. The C# conditional operator is like a Lisp if statement - the piece following ? is the then portion, and the piece following the : is the else portion. So the translation should rather be:
Code - Auto/Visual Lisp: [Select]
  1. ;; var y = isNorthHemisphere ? utmY : utmY - 10000000;
  2. (setq y (if isNorthHemisphere utmY (- utmY 10000000)))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #8 on: December 02, 2014, 03:12:52 AM »
Also, if you've got issues with converting infix notation to prefix (i.e Polish / s-expressions) for Lisp. You could try my Lisp code to do those things for you: http://www.theswamp.org/index.php?topic=41681.10

Just note I have found some errors in it before. I cannot guarantee that it works in every single instance - though it "should"  :wink:

As an example, here's that long line you're having problems with:
Code: [Select]
_$ (infix->prefix '(((((lat + (1 + e2cuadrada * Cos(lat) ^ 2) - (3.0 / 2.0) * e2cuadrada * Sin(lat) * Cos(lat) * (tao - lat)) * (tao - lat))) / PI) * 180))
(* (/ (* (+ LAT (- (+ 1 (* E2CUADRADA (^ (COS LAT) 2))) (* (/ 3.0 2.0) (* E2CUADRADA (* (SIN LAT) (* (COS LAT) (- TAO LAT))))))) (- TAO LAT)) PI) 180)
Note I had to do a bit of altering in there - e.g. Changing the Math.Pow into ^ and Math.Sin into Sin. Else it went all haywire.

Thereafter the ^ function is simply expt. So you either add something like this:
Code - Auto/Visual Lisp: [Select]
Or you find-n-replace all ^ with expt in that line.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio123

  • Guest
Re: C++ to lisp
« Reply #9 on: December 04, 2014, 02:08:15 AM »
Thanks for the help. There are still 3 lines to translate. Can anyone help me with this yet?

Thank you  :-P  :-D


Code: [Select]
        var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
(setq senoheps    (/ ????
   
        var delt = Math.Atan(senoheps / (Math.Cos(nab)));
(setq delt (/ ???)
 
        var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
(setq tao (* ???   

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #10 on: December 04, 2014, 02:54:44 AM »
First you need to know those functions. E.g. C#'s Math.Exp raises the math constant e to the specified power: http://msdn.microsoft.com/en-us/library/system.math.exp(v=vs.110).aspx

The same function in AutoLisp is simply exp: http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-FD0C918B-A162-4939-9F4E-FFE8863C3FC8

Same goes for:
Unfortunately AutoLisp doesn't come standard with the tan function. But it's reasonably easy to make one yourself. Basically tan = sin/cos. So
Code - Auto/Visual Lisp: [Select]
  1. (defun tan (x) (/ (sin x) (cos x)))

Though that means you run the risk of getting a divide-by-zero error on angles like 90 degrees. So a safer way might be:
Code - Auto/Visual Lisp: [Select]
  1. (defun tan (x / c)
  2.    (if (= (setq c (cos x)) 0.0)
  3.       (* (if (< x 0) -1 1) 1.7976931348623158e+308) ;Closest we can get to infinity using a double floating point
  4.       (/ (sin x) c)))

Then those 3 lines are:
Code - Auto/Visual Lisp: [Select]
  1. (setq senoheps (/ (- (exp eps) (exp (- eps))) 2.0))
  2. (setq delt (atan (/ senoheps (cos nab))))
  3. (setq tao (atan (* (cos delt) (tab nab))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio123

  • Guest
Re: C++ to lisp
« Reply #11 on: December 05, 2014, 07:19:05 AM »
Hello, I have now assembled everything.
It also works without error but the result is not correct.

 (ToLatLon 451602.19 4519076.99 "33N")
should Latitud: 40.821287 Longitud; 14.426080
but with (ToLatLon) Result is Latitud: 40.5467 Longitud: 14.4284

Can someone please check the code C#. Whether this outputs the correct result  :evil: :-P :oops: :cry: :-D

Thanks

Lisp Code
Code: [Select]
(defun ToLatLon (utmX utmY utmZone)

(defun tan (x / c)
(if (= (setq c (cos x)) 0.0)
(* (if (< x 0) -1 1) 1.7976931348623158e+308) ;Closest we can get to infinity using a double floating point
(/ (sin x) c)
)
)

        (setq latitude 0.0)
  (setq longitude 0.0)

    ;;bool isNorthHemisphere = utmZone.Last() >= 'N';
(setq isNorthHemisphere (wcmatch utmZone "*[Nn]"))

    ;;var diflat = -0.00066286966871111111111111111111111111;
        (setq diflat -0.00066286966871111111111111111111111111)

    ;;var diflon = -0.0003868060578;
(setq diflon -0.0003868060578)

    ;;var zone = int.Parse(utmZone.Remove(utmZone.Length - 1));
(setq zone (atoi (substr utmZone 1 (1- (strlen utmZone)))))

    ;;var c_sa = 6378137.000000;
(setq c_sa 6378137.0)
   
    ;;var c_sb = 6356752.314245;
(setq c_sb 6356752.314245)

    ;;var e2 = Math.Pow((Math.Pow(c_sa, 2) - Math.Pow(c_sb, 2)), 0.5) / c_sb;
(setq e2 (/ (expt (- (expt c_sa 2.0) (expt c_sb 2.0)) 0.5) c_sb))
   
    ;;var e2cuadrada = Math.Pow(e2, 2);
(setq e2cuadrada (expt e2 2.0))
 
    ;;var c = Math.Pow(c_sa, 2) / c_sb;
(setq c (/ (expt c_sa 2.0) c_sb))
 
    ;;var x = utmX - 500000;
(setq x (- utmX 500000))

    ;;var y = isNorthHemisphere ? utmY : utmY - 10000000;
(setq y (if isNorthHemisphere utmY (- utmY 10000000)))
 
    ;;var s = ((zone * 6.0) - 183.0);
(setq s (- (* zone 6.0) 183.0))
 
    ;;var lat = y / (6366197.724 * 0.9996); // Change c_sa for 6366197.724
(setq lat (/ y (* 6366197.724 0.9996)))
 
    ;;var v = (c / Math.Pow(1 + (e2cuadrada * Math.Pow(Math.Cos(lat), 2)), 0.5)) * 0.9996;
(setq v (* (/ c (expt (+ 1 (* e2cuadrada (expt (cos lat) 2) )) 0.5) ) 0.9996))
 
    ;;var a = x / v;
(setq a (/ x v))
 
    ;;var a1 = Math.Sin(2 * lat);
(setq a1 (sin (* 2.0 lat)))
 
    ;;var a2 = a1 * Math.Pow((Math.Cos(lat)), 2);
(setq a2 (* a1 (expt (cos lat) 2.0)))
 
    ;;var j2 = lat + (a1 / 2.0);
(setq j2 (+ lat (/ a1 2.0)))

    ;;var j4 = ((3 * j2) + a2) / 4.0;
(setq j4 (/ (+ (* 3.0 j2) a2) 4.0))
 
    ;;var j6 = (5 * j4 + a2 * Math.Pow((Math.Cos(lat)), 2)) / 3.0;
(setq j6 (/ (+ (* 5.0 j4) (* a2 (expt (cos lat) 2.0))) 3.0))
 
    ;;var alfa = (3.0 / 4.0) * e2cuadrada;
(setq alfa (* (/ 3.0 4.0) e2cuadrada))
 
    ;;var beta = (5.0 / 3.0) * Math.Pow(alfa, 2);
(setq beta (* (/ 5.0 3.0) (expt alfa 2.0)))       
       
    ;;var gama = (35.0 / 27.0) * Math.Pow(alfa, 3);
(setq gama (* (/ 35.0 27.0) (expt alfa 3.0)))
 
    ;;var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
(setq bm (* 0.9996 c (- lat (+ (* alfa j2) (* beta j4)) (* gama j6))))   ;;;not shore
         
    ;;var b = (y - bm) / v;
(setq b (/ (- y bm) v))       
       
    ;;var epsi = ((e2cuadrada * Math.Pow(a, 2)) / 2.0) * Math.Pow((Math.Cos(lat)), 2);
(setq epsi (* (/ (* e2cuadrada (expt a 2.0)) 2.0) (expt (cos lat) 2.0)))
 
    ;;var eps = a * (1 - (epsi / 3.0));
(setq eps (* a (- 1 (/ epsi 3.0))))       
       
    ;;var nab = (b * (1 - epsi)) + lat;
(setq nab (+ (* b (1- epsi)) lat))
 
    ;;var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
(setq senoheps (/ (- (exp eps) (exp (- eps))) 2.0))
   
    ;;var delt = Math.Atan(senoheps / (Math.Cos(nab)));
(setq delt (atan (/ senoheps (cos nab))))
 
    ;;var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
(setq tao (atan (* (cos delt) (tan nab))))       
 
    ;;longitude = (delt / Math.PI) * 180 + s;
(setq longitude (+ (* (/ delt pi) 180.0) 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
(setq latitude (* (/ (+ lat (* (- (+ 1 (* e2cuadrada (expt (cos lat) 2.0))) (* (/ 3.0  2.0) e2cuadrada (sin lat) (cos lat) (- tao lat))) (- tao lat))) pi) 180.0))


    (princ "\nLatitud: ")
    (princ latitude)
    (princ "\nLongitud: ")
    (princ longitude)
    (princ)
)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #12 on: December 06, 2014, 08:00:17 AM »
Nope, it seems as if the C# code is running it properly. E.g. running this in MonoDevelop in Linux:
Code - C#: [Select]
  1. using System;
  2.  
  3. namespace ToLatLon_Test
  4. {
  5.         class MainClass
  6.         {
  7.                 public static void Main(string[] args)
  8.                 {
  9.                         Console.WriteLine("UTM to LatLon test");
  10.                         string input;
  11.                         double utmX, utmY;
  12.                         string Zone;
  13.                         try {
  14.                                 Console.Write("Enter utmX: ");
  15.                                 input = Console.ReadLine();
  16.                                 utmX = double.Parse(input);
  17.                                 Console.Write("Enter utmY: ");
  18.                                 input = Console.ReadLine();
  19.                                 utmY = double.Parse(input);
  20.                                 Console.Write("Enter UTM Zone: ");
  21.                                 Zone = Console.ReadLine();
  22.                                 Console.WriteLine();
  23.                                 ToLatLon(utmX, utmY, Zone);
  24.                         } catch(Exception e) {
  25.                                 Console.Write("Error: ");
  26.                                 Console.WriteLine(e.Message);
  27.                         }
  28.                 }
  29.  
  30.                 public static void ToLatLon(double utmX, double utmY, string utmZone)
  31.                 {
  32.                         double latitude = 0;
  33.                         double longitude = 0;
  34.  
  35.                         // Changed from utmZone.Last as the Linq version is much slower than simply indexing
  36.                         // the last position in the string's array of characters
  37.                         bool isNorthHemisphere = utmZone[utmZone.Length-1] >= 'N';
  38.  
  39.                         var diflat = -0.00066286966871111111111111111111111111;
  40.                         var diflon = -0.0003868060578;
  41.  
  42.                         var zone = int.Parse(utmZone.Remove(utmZone.Length - 1));
  43.                         var c_sa = 6378137.000000;
  44.                         var c_sb = 6356752.314245;
  45.                         var e2 = Math.Pow((Math.Pow(c_sa, 2) - Math.Pow(c_sb, 2)), 0.5) / c_sb;
  46.                         var e2cuadrada = Math.Pow(e2, 2);
  47.                         var c = Math.Pow(c_sa, 2) / c_sb;
  48.                         var x = utmX - 500000;
  49.                         var y = isNorthHemisphere ? utmY : utmY - 10000000;
  50.  
  51.                         var s = ((zone * 6.0) - 183.0);
  52.                         var lat = y / (6366197.724 * 0.9996); // Change c_sa for 6366197.724
  53.                         var v = (c / Math.Pow(1 + (e2cuadrada * Math.Pow(Math.Cos(lat), 2)), 0.5)) * 0.9996;
  54.                         var a = x / v;
  55.                         var a1 = Math.Sin(2 * lat);
  56.                         var a2 = a1 * Math.Pow((Math.Cos(lat)), 2);
  57.                         var j2 = lat + (a1 / 2.0);
  58.                         var j4 = ((3 * j2) + a2) / 4.0;
  59.                         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
  60.                         var alfa = (3.0 / 4.0) * e2cuadrada;
  61.                         var beta = (5.0 / 3.0) * Math.Pow(alfa, 2);
  62.                         var gama = (35.0 / 27.0) * Math.Pow(alfa, 3);
  63.                         var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
  64.                         var b = (y - bm) / v;
  65.                         var epsi = ((e2cuadrada * Math.Pow(a, 2)) / 2.0) * Math.Pow((Math.Cos(lat)), 2);
  66.                         var eps = a * (1 - (epsi / 3.0));
  67.                         var nab = (b * (1 - epsi)) + lat;
  68.                         var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
  69.                         var delt = Math.Atan(senoheps / (Math.Cos(nab)));
  70.                         var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
  71.  
  72.                         longitude = (delt / Math.PI) * 180 + s;
  73.                         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
  74.  
  75.                         Console.WriteLine("Latitud: " + latitude.ToString() + "\nLongitud: " + longitude.ToString());
  76.  
  77.                 }
  78.         }
  79. }
Produces the following:
Code: [Select]
UTM to LatLon test
Enter utmX: 451602.19
Enter utmY: 4519076.99
Enter UTM Zone: 33N

Latitud: 40.8212871342299
Longitud: 14.4260799700565

Press any key to continue...
« Last Edit: December 06, 2014, 08:10:03 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: C++ to lisp
« Reply #13 on: December 06, 2014, 08:26:39 AM »
Looking through your Lisp code ... this might be the culprit:
Code - Auto/Visual Lisp: [Select]
  1. ;;var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
  2. (setq bm (* 0.9996 c (- lat (+ (* alfa j2) (* beta j4)) (* gama j6))))   ;;;not shore
  3. ;; Should be something like this
  4. (setq bm (* 0.9996 c (- (+ (- lat (* alpha j2)) (* beta j4)) (* gama j6)))))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pedroantonio124

  • Guest
Re: C++ to lisp
« Reply #14 on: December 08, 2014, 04:40:17 AM »
Hello irneb,

Thanks for your help  :-)

Unfortunately, I still receive the wrong result with the lisp code.
Maybe I can find the mistake if I know the result of every line.
Could you let me the C # run through over again code? Please
With a Console.WriteLine for every line.

PedroAntonio  :evil:

Code: [Select]
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;
Console.WriteLine ("\n" e2.ToString() )

        var e2cuadrada = Math.Pow(e2, 2);
Console.WriteLine ("\n" e2cuadrada.ToString() )

        var c = Math.Pow(c_sa, 2) / c_sb;
Console.WriteLine ("\n" c.ToString() )

        var x = utmX - 500000;
Console.WriteLine ("\n" x.ToString() )

        var y = isNorthHemisphere ? utmY : utmY - 10000000;
Console.WriteLine ("\n" y.ToString() )

        var s = ((zone * 6.0) - 183.0);
Console.WriteLine ("\n" s.ToString() )

        var lat = y / (6366197.724 * 0.9996); // Change c_sa for 6366197.724
Console.WriteLine ("\n" lat.ToString() )

        var v = (c / Math.Pow(1 + (e2cuadrada * Math.Pow(Math.Cos(lat), 2)), 0.5)) * 0.9996;
Console.WriteLine ("\n" v.ToString() )

        var a = x / v;
Console.WriteLine ("\n" a.ToString() )

        var a1 = Math.Sin(2 * lat);
Console.WriteLine ("\n" a1.ToString() )

        var a2 = a1 * Math.Pow((Math.Cos(lat)), 2);
Console.WriteLine ("\n" a2.ToString() )

        var j2 = lat + (a1 / 2.0);
Console.WriteLine ("\n" j2.ToString() )

        var j4 = ((3 * j2) + a2) / 4.0;
Console.WriteLine ("\n" j4.ToString() )

        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
        Console.WriteLine ("\n" j6.ToString() )

var alfa = (3.0 / 4.0) * e2cuadrada;
Console.WriteLine ("\n" alfa.ToString() )

        var beta = (5.0 / 3.0) * Math.Pow(alfa, 2);
Console.WriteLine ("\n" beta.ToString() )

        var gama = (35.0 / 27.0) * Math.Pow(alfa, 3);
Console.WriteLine ("\n" gama.ToString() )

        var bm = 0.9996 * c * (lat - alfa * j2 + beta * j4 - gama * j6);
Console.WriteLine ("\n" bm.ToString() )

        var b = (y - bm) / v;
Console.WriteLine ("\n" b.ToString() )

        var epsi = ((e2cuadrada * Math.Pow(a, 2)) / 2.0) * Math.Pow((Math.Cos(lat)), 2);
Console.WriteLine ("\n" epsi.ToString() )

        var eps = a * (1 - (epsi / 3.0));
Console.WriteLine ("\n" eps.ToString() )

        var nab = (b * (1 - epsi)) + lat;
Console.WriteLine ("\n" nab.ToString() )

        var senoheps = (Math.Exp(eps) - Math.Exp(-eps)) / 2.0;
Console.WriteLine ("\n" senoheps.ToString() )

        var delt = Math.Atan(senoheps / (Math.Cos(nab)));
Console.WriteLine ("\n" delt.ToString() )

        var tao = Math.Atan(Math.Cos(delt) * Math.Tan(nab));
Console.WriteLine  ("\n" tao.ToString() )

        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());

    }