How to draw a n sided regular polygon in cartesian coordinates?

前端 未结 6 1953
北荒
北荒 2020-12-04 17:09

I have been trying to figure out how to write a simple program to compute the x,y points for creating a regular polygon of n sides. Can someone give me some code examples th

6条回答
  •  误落风尘
    2020-12-04 17:17

    angle = start_angle
    angle_increment = 360 / n_sides
    for n_sides:
        x = x_centre + radius * cos(angle)
        y = y_centre + radius * sin(angle)
        angle += angle_increment
    

    in practice, when drawing lines instead of just calculating the corner points, you also need to "join up" the polygon by repeating the first point.

    also, if sin() and cos() work in radians rather than degrees, you want 2 * PI instead of 360.

提交回复
热议问题