如何在笛卡尔坐标系中绘制n边的正多边形? [英] How to draw a n sided regular polygon in cartesian coordinates?
问题描述
我一直在试图找出如何编写一个简单的程序来计算x,y点的方法,以创建n边的规则多边形.有人可以给我一些不使用预先绘制多边形的函数的代码示例吗?我想了解这个过程,我假设是这样的:
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 that don't use preexisting functions that draw polygons? I want to understand the process, which I assume is something like this:
- 从半径和中心点开始选择角度
- 以某种方式计算距中心的那个距离的x,y位置(如何?)
- 将360除以边数,移动该距离,然后从第一个x,y点绘制下一条线
- 继续直到angle = 360除以那个数字.
假设我的假设是正确的,主要是要了解如何计算x,y点.
Assuming that my assumptions are correct, the main thing is to understand how to compute the x,y points.
建议以视觉基础(甚至是老式的Microsoft/Atari/Commodore BASIC)或易于理解的英语步骤来回答.如果您必须使用数学公式来回答,请使用计算机语言来完成它,以便我可以阅读它,即使使用C或C ++也可以弄清楚,但我不知道如何阅读数学符号.我使用的语言是一种类似于Visual Basic的语言,除了线条画外几乎没有其他图形基元.
Prefer answers in a visual basic (or even old style Microsoft/Atari/Commodore BASIC) or a human readable set of steps in English. If you have to answer with a math formula, do it in a computer language so I can read it, even in C or C++ I can figure it out, but I don't know how to read mathematical notation. The language I'm using is a Visual Basic-like language that has almost no graphics primitives other than line drawing.
推荐答案
让我们假设您要绘制一个半径为 r 的 N 面多边形,其中心为(0 ,0).然后 n 个顶点由下式给出:
Let's assume you want to draw an N-sided polygon of radius r, centred at (0,0). Then the n vertices are given by:
x[n] = r * cos(2*pi*n/N)
y[n] = r * sin(2*pi*n/N)
其中0< = n < N .请注意,这里的cos
和sin
是弧度的,而不是度数的(在大多数编程语言中,这是很常见的).
where 0 <= n < N. Note that cos
and sin
here are working in radians, not degrees (this is pretty common in most programming languages).
如果您想要一个不同的中心,只需将中心点的坐标添加到每个坐标( x [n] , y [n] ).如果要改变方向,只需要增加一个恒定的角度即可.因此,一般形式为:
If you want a different centre, then just add the coordinates of the centre point to each (x[n], y[n]). If you want a different orientation, you just need to add a constant angle. So the general form is:
x[n] = r * cos(2*pi*n/N + theta) + x_centre
y[n] = r * sin(2*pi*n/N + theta) + y_centre
这篇关于如何在笛卡尔坐标系中绘制n边的正多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!