Advertisement
Guest User

Untitled

a guest
Jul 18th, 2014
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1.         public static List<Coordinate> generateCirclePoints(double centerLat, double centerLon, double radius)
  2.         {
  3.             int iBearing;
  4.             double[] lonArray = new double[360];
  5.             double[] latArray = new double[360];
  6.             List<Coordinate> coords = new List<Coordinate>();
  7.             for (iBearing = 0; iBearing < 360; iBearing++)
  8.             {
  9.                 calcNewPosition(centerLat, centerLon, iBearing, radius, out latArray[iBearing], out lonArray[iBearing]);
  10.                 coords.Add(new Coordinate { lat = latArray[iBearing], lon = lonArray[iBearing] });
  11.             }
  12.  
  13.             return coords;
  14.         }
  15.  
  16.         private static double radians(double x)
  17.         {
  18.             return Math.Acos(0.0) * x / 90.0;
  19.         }
  20.  
  21.         private static void calcNewPosition(double lat, double lon, double bearing, double d, out double newLat,
  22.             out double newLon)
  23.         {
  24.             double lat1, lon1, br, pi;
  25.             double Re = 6371000;
  26.             // convert everything to radians first:
  27.             lat1 = radians(lat);
  28.             lon1 = radians(lon);
  29.             br = radians(bearing);
  30.  
  31.             pi = 2 * Math.Acos(0.0);
  32.  
  33.             double lat2, lon2;
  34.             lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(d / Re) +
  35.                              Math.Cos(lat1) * Math.Sin(d / Re) * Math.Cos(br));
  36.             lon2 = lon1 + Math.Atan2(Math.Sin(br) * Math.Sin(d / Re) * Math.Cos(lat1),
  37.                 Math.Cos(d / Re) - Math.Sin(lat1) * Math.Sin(lat2));
  38.             newLat = 180.0 * lat2 / pi;
  39.             newLon = 180.0 * lon2 / pi;
  40.         }
  41.  
  42.     public class Circle : Polygon
  43.     {
  44.         private double radius = double.NaN;
  45.         private MapPoint center;
  46.         private int pointCount = 360;
  47.        
  48.         private readonly ESRI.ArcGIS.Client.Projection.WebMercator mercator =
  49.             new ESRI.ArcGIS.Client.Projection.WebMercator();
  50.  
  51.         public double Radius
  52.         {
  53.             get { return radius; }
  54.             set { radius = value; CreateRing(); }
  55.         }
  56.  
  57.         [System.ComponentModel.TypeConverter(typeof(MapPointConverter))]
  58.         public MapPoint Center
  59.         {
  60.             get { return center; }
  61.             set { center = value; CreateRing(); }
  62.         }
  63.  
  64.         public int PointCount
  65.         {
  66.             get { return pointCount; }
  67.             set { pointCount = value; CreateRing(); }
  68.         }
  69.  
  70.         private void CreateRing()
  71.         {
  72.             Rings.Clear();
  73.             if (!double.IsNaN(Radius) && Radius > 0 && Center != null && PointCount > 2)
  74.             {
  75.                 PointCollection pnts = new PointCollection();
  76.                
  77.                 foreach (var coord in MapMaths.generateCirclePoints(center.X, center.Y, radius))
  78.                 {
  79.                     Geometry geomPoint = mercator.FromGeographic(new MapPoint(coord.lon, coord.lat));
  80.                     pnts.Add(new MapPoint(geomPoint.Extent.XMax, geomPoint.Extent.YMax));
  81.                 }
  82.                 //for (int i = 0; i <= PointCount; i++)
  83.                 //{
  84.                 //    double rad = 2 * Math.PI / PointCount * i;
  85.                 //    double x = Math.Cos(rad) * radius + Center.X;
  86.                 //    double y = Math.Sin(rad) * radius + Center.Y;
  87.                 //    pnts.Add(new MapPoint(x, y));
  88.                 //}
  89.                 Rings.Add(pnts);
  90.             }
  91.         }
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement