Calculate coordinates of a regular polygon's vertices

后端 未结 7 1826
醉酒成梦
醉酒成梦 2020-12-12 18:30

I am writing a program in which I need to draw polygons of an arbitrary number of sides, each one being translated by a given formula which changes dynamically. There is som

7条回答
  •  醉梦人生
    2020-12-12 18:40

    hmm if you test all the versions that are listed here you'll see that the implementation is not good. you can check the distance from the center to each generated point of the polygon with : http://www.movable-type.co.uk/scripts/latlong.html

    Now i have searched a lot and i could not find any good implementation for calculating a polyogon using the center and the radius...so i went back to the math book and tried to implement it myself. In the end i came up with this...wich is 100% good:

                List coordinates = new List();
                #region create Polygon Coordinates
                if (!string.IsNullOrWhiteSpace(bus.Latitude) && !string.IsNullOrWhiteSpace(bus.Longitude) && !string.IsNullOrWhiteSpace(bus.ListingRadius))
                {
                    double lat = DegreeToRadian(Double.Parse(bus.Latitude));
                    double lon = DegreeToRadian(Double.Parse(bus.Longitude));
                    double dist = Double.Parse(bus.ListingRadius);
                    double angle = 36;
    
                    for (double i = 0; i <= 360; i += angle)
                    {
                        var bearing = DegreeToRadian(i);
    
                        var lat2 = Math.Asin(Math.Sin(lat) * Math.Cos(dist / earthRadius) + Math.Cos(lat) * Math.Sin(dist / earthRadius) * Math.Cos(bearing));
                        var lon2 = lon + Math.Atan2(Math.Sin(bearing) * Math.Sin(dist / earthRadius) * Math.Cos(lat),Math.Cos(dist / earthRadius) - Math.Sin(lat) * Math.Sin(lat2));
    
                        coordinates.Add(new double[] { RadianToDegree(lat2), RadianToDegree(lon2) });
    
                    }
    
                    poly.Coordinates = new[] { coordinates.ToArray() };
                }
                #endregion
    

    If you test this you'll see that all the points are at the exact distance that you give ( radius ). Also don't forget to declare the earthRadius.

    private const double earthRadius = 6371.01;
    

    This calculates the coordinates of a decagon. You see the angle used is 36 degrees. You can split 360 degrees to any number of sides that you want and put the result in the angle variable. Anyway .. i hope this helps you @rmx!

提交回复
热议问题