Calculate coordinates of a regular polygon's vertices

后端 未结 7 1828
醉酒成梦
醉酒成梦 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 19:03

    The number of points equals the number of sides.

    The angle you need is angle = 2 * pi / numPoints.

    Then starting vertically above the origin with the size of the polygon being given by radius:

    for (int i = 0; i < numPoints; i++)
    {
        x = centreX + radius * sin(i * angle);
        y = centreY + radius * cos(i * angle);
    }
    

    If your centre is the origin then simply ignore the centreX and centreY terms as they'll be 0,0.

    Swapping the cos and sin over will point the first point horizontally to the right of the origin.

    0 讨论(0)
提交回复
热议问题