View Single Post

  #7 (permalink)  
Old 12-03-2005
Wilfried Wilfried is offline
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,123
Hi,

I made it with a singleton class. No need to create, it create itself and there is only one instance of it in the project:

Code:
    public sealed class MPTools
    {
        static readonly MPTools instance = new MPTools();
        static Map map;
        static Location locNorthPole;
        static Location locSantaCruz;
        static double halfEarth;
        static double quarterEarth;

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static MPTools()
        {
        }

        MPTools()
        {
        }

        public static MPTools Instance
        {
            get
            {
                return instance;
            }
        }

        public static void CalcPos(Map activeMap, Location loc, out double lat, out double lon)
        {
            if (map != activeMap) { // user may have load another active map since last time
                map = activeMap;
                locNorthPole = map.GetLocation(90, 0, 0);
                locSantaCruz = map.GetLocation(0, -90, 0);
                halfEarth = map.Distance(locNorthPole, map.GetLocation(-90, 0, 0));
                quarterEarth = halfEarth / 2;
            }
            lat = 90 - 180 * map.Distance(locNorthPole, loc) / halfEarth;
            double D = map.Distance(map.GetLocation(lat, 0, 0), loc);
            double L = Deg2Rad(lat);
            lon = 180 * Math.Acos((Math.Cos(D * Math.PI / halfEarth) - Math.Pow(Math.Sin(L), 2)) / Math.Pow(Math.Cos(L), 2)) / Math.PI;
            if (map.Distance(locSantaCruz, loc) < quarterEarth)
                lon = -lon;
        }

        private static double Rad2Deg(double rad)
        {
            return rad * 180 / Math.PI;
        }

        private static double Deg2Rad(double degree)
        {
            return degree * Math.PI / 180;
        }
    }
Reply With Quote