Calculate coordinates of a regular polygon's vertices

后端 未结 7 1827
醉酒成梦
醉酒成梦 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<double[]> coordinates = new List<double[]>();
                #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!

    0 讨论(0)
  • 2020-12-12 18:44

    Sorry, I dont have a full solution at hand right now, but you should try looking for 2D-Rendering of Circles. All classic implementations of circle(x,y,r) use a polygon like you described for drawing (but with 50+ sides).

    0 讨论(0)
  • 2020-12-12 18:44

    Say the distance of the vertices to the origin is 1. And say (1, 0) is always a coordinate of the polygon.

    Given the number of vertices (say n), the rotation angle required to position the (1, 0) to the next coordinate would be (360/n).

    The computation required here is to rotate the coordinates. Here is what it is; Rotation Matrix.

    Say theta = 360/n;

    [cos(theta) -sin(theta)]
    [sin(theta) cos(theta)]
    

    would be your rotation matrix.

    If you know linear algebra you already know what i mean. If dont just have a look at Matrix Multiplication

    0 讨论(0)
  • 2020-12-12 18:46

    One possible implementation to generate a set of coordinates for regular polygon is to:

    Define polygon center, radius and first vertex1.
    Rotate the vertex n-times2 at an angle of: 360/n.

    In this implementation I use a vector to store the generated coordinates and a recursive function to generate them:

    void generateRegularPolygon(vector<Point>& v, Point& center, int sidesNumber, int radius){
        // converted to radians
        double angRads = 2 * PI / double(sidesNumber);
        // first vertex  
        Point initial(center.x, center.y - radius);
        rotateCoordinate(v, center, initial, angRads, sidesNumber);
    }
    

    where:

    void rotateCoordinate(vector<Point>& v, Point& axisOfRotation, Point& initial, double angRads, int numberOfRotations){
        // base case: number of transformations < 0
        if(numberOfRotations <= 0) return;
        else{
            // apply rotation to: initial, around pivot point: axisOfRotation
            double x = cos(angRads) * (initial.x - axisOfRotation.x) - sin(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.x;
            double y = sin(angRads) * (initial.x - axisOfRotation.x) + cos(angRads) * (initial.y - axisOfRotation.y) + axisOfRotation.y;
            // store the result
            v.push_back(Point(x, y));
            rotateCoordinate(v, axisOfRotation, Point(x,y), angRads, --numberOfRotations);
        }
    }
    

    Note:

    Point is a simple class to wrap the coordinate into single data structure:

    class Point{
    public:
        Point(): x(0), y(0){ }
        Point(int xx, int yy): x(xx), y(yy) { }
    private:
        int x;
        int y; 
    }; 
    

    1 in terms of (relative to) the center, radius. In my case the first vertex is translated from the centre up horizontally by the radius lenght.

    2 n-regular polygon has n vertices.

    0 讨论(0)
  • 2020-12-12 18:48

    The simple method is: Let's take N-gone(number of sides) and length of side L. The angle will be T = 360/N. Let's say one vertices is located on origin.

    * First vertex = (0,0)
    * Second vertex = (LcosT,LsinT)
    * Third vertex = (LcosT+Lcos2T, LsinT+Lsin2T)
    * Fourth vertex = (LcosT+Lcos2T+Lcos3T, LsinT+Lsin2T+Lsin3T)
    

    You can do in for loop

    0 讨论(0)
  • 2020-12-12 18:58
    for (i = 0; i < n; i++) {
      printf("%f %f\n",r * Math.cos(2 * Math.PI * i / n), r * Math.sin(2 * Math.PI * i / n));
    }
    

    where r is the radius of the circumsribing circle. Sorry for the wrong language No Habla C#.

    Basically the angle between any two vertices is 2 pi / n and all the vertices are at distance r from the origin.

    EDIT: If you want to have the center somewher other than the origin, say at (x,y)

    for (i = 0; i < n; i++) {
      printf("%f %f\n",x + r * Math.cos(2 * Math.PI * i / n), y + r * Math.sin(2 * Math.PI * i / n));
    }
    
    0 讨论(0)
提交回复
热议问题