polygon coordinates

回眸只為那壹抹淺笑 提交于 2019-12-13 11:24:55

问题


how can we draw a polygon. when only the sides and radius is given. I have to make a pop up box which will take as input the radius and number of sides and will draw a ploygon. just need the formula.


回答1:


Imagine a circle of radius r. It is like a regular polygon with an infinite number of sides.

Trigonometry tells us:

x = r * cos(a);
y = r * sin(a);

We know there are 360 degrees or 2pi radians in a circle. So to draw it we would start with angle = 0, calculate that co-ord, step to the next angle and calculate that point, then draw a line between the two.

There are only so many points we can calculate around the edge of the circle, eventually it won't make any difference. If the circle is small enough, even 8 sides will look round.

To draw an 8 sided circle we want 8 points evenly spaced around the circle. Divide the circle into 8 angles, each one is 2 * pi / 8 radians.

So:

angle = 0.0;
step = 2 * pi / 8;

for ( n = 0; n < 8; n++ ) {
    x = radius * cos(angle);
    y = radius * sin(angle);
    angle += step;
}

Now you can draw an octagon, change it to draw the general case.



来源:https://stackoverflow.com/questions/11340305/polygon-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!