Calculate coordinates of a regular polygon's vertices

后端 未结 7 1861
醉酒成梦
醉酒成梦 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: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));
    }
    

提交回复
热议问题