Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<Coordinate> generateCirclePoints(double centerLat, double centerLon, double radius)
- {
- int iBearing;
- double[] lonArray = new double[360];
- double[] latArray = new double[360];
- List<Coordinate> coords = new List<Coordinate>();
- for (iBearing = 0; iBearing < 360; iBearing++)
- {
- calcNewPosition(centerLat, centerLon, iBearing, radius, out latArray[iBearing], out lonArray[iBearing]);
- coords.Add(new Coordinate { lat = latArray[iBearing], lon = lonArray[iBearing] });
- }
- return coords;
- }
- private static double radians(double x)
- {
- return Math.Acos(0.0) * x / 90.0;
- }
- private static void calcNewPosition(double lat, double lon, double bearing, double d, out double newLat,
- out double newLon)
- {
- double lat1, lon1, br, pi;
- double Re = 6371000;
- // convert everything to radians first:
- lat1 = radians(lat);
- lon1 = radians(lon);
- br = radians(bearing);
- pi = 2 * Math.Acos(0.0);
- double lat2, lon2;
- lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(d / Re) +
- Math.Cos(lat1) * Math.Sin(d / Re) * Math.Cos(br));
- lon2 = lon1 + Math.Atan2(Math.Sin(br) * Math.Sin(d / Re) * Math.Cos(lat1),
- Math.Cos(d / Re) - Math.Sin(lat1) * Math.Sin(lat2));
- newLat = 180.0 * lat2 / pi;
- newLon = 180.0 * lon2 / pi;
- }
- public class Circle : Polygon
- {
- private double radius = double.NaN;
- private MapPoint center;
- private int pointCount = 360;
- private readonly ESRI.ArcGIS.Client.Projection.WebMercator mercator =
- new ESRI.ArcGIS.Client.Projection.WebMercator();
- public double Radius
- {
- get { return radius; }
- set { radius = value; CreateRing(); }
- }
- [System.ComponentModel.TypeConverter(typeof(MapPointConverter))]
- public MapPoint Center
- {
- get { return center; }
- set { center = value; CreateRing(); }
- }
- public int PointCount
- {
- get { return pointCount; }
- set { pointCount = value; CreateRing(); }
- }
- private void CreateRing()
- {
- Rings.Clear();
- if (!double.IsNaN(Radius) && Radius > 0 && Center != null && PointCount > 2)
- {
- PointCollection pnts = new PointCollection();
- foreach (var coord in MapMaths.generateCirclePoints(center.X, center.Y, radius))
- {
- Geometry geomPoint = mercator.FromGeographic(new MapPoint(coord.lon, coord.lat));
- pnts.Add(new MapPoint(geomPoint.Extent.XMax, geomPoint.Extent.YMax));
- }
- //for (int i = 0; i <= PointCount; i++)
- //{
- // double rad = 2 * Math.PI / PointCount * i;
- // double x = Math.Cos(rad) * radius + Center.X;
- // double y = Math.Sin(rad) * radius + Center.Y;
- // pnts.Add(new MapPoint(x, y));
- //}
- Rings.Add(pnts);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement