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

懵懂的女人 提交于 2019-11-28 04:33:32

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)

where 0 <= n < N. Note that cos and sin here are working in radians, not degrees (this is pretty common in most programming languages).

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
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.

Here is a full c++ program that prints out the points of a regular polygon. In this case,p is the number of sides,r is the radius of the polygon and d is the direction or angle of the first point from the center. Maybe this will help.

//g++ ck.cpp -o ck && ./ck
#include <stdio.h>
#include <math.h>

int p=3; //number of sides
double r=1000,d=3/4.0;

int main()
{
 int i=0;
 double x,y,t;
 while(i<p)
 {
  t=2*M_PI*((double)i/p+d);
  x=cos(t)*r;
  y=sin(t)*r;
  printf("x%i:%f y%i:%f\n",i,x,i,y);
  i++;
 }
}

If you want to make it faster at the cost of some error accumulation, use a (complex) primitive nth root of unity and take the powers of it (either using built-in complex number support in your language or coding the multiplication by hand). In C:

complex double omega=cexp(2*M_PI*I/n), z;
for (i=0, z=1; i<n; i++, z*=omega) {
    /* do something with z */
}

I know you asked for an answer in Visual Basic, however here is a solution in JavaScript.

The "for n_sides:" answer is the easiest. For the guy who suggested that you could simplify the calculations by using complex numbers, nearly all math libraries have table-based cos() and sin() routines with efficient interpolation so there's no need to delve into relatively obscure solutions. Usually a regular n-gon can be initialized and the hardware scaling of OpenGL used to scale / transform it for any particular instance.

If you want to be hardcore about it, pre-generate all the n-gons you need and load them into vertex buffers.

As an aside, here's the above solution in Lua. It just prints out the coordinates, but you're of course free to return the coordinates in an array / table. The coordinates returned can be used to initialize an OpenGL GL_LINE_LOOP mesh primitive.

require 'math'

-- computes coordinates for n-sided, regular polygon of given radius and start angle
-- all values are in radians

function polypoints(sides, radius, start)
    local x_center = 0.0
    local y_center = 0.0
    local angle = start
    local angle_increment = 2 * math.pi / sides
    local x=0.0
    local y=0.0

    print(string.format("coordinates for a %d sided regular polygon of radius %d\nVertex",sides,radius),"X"," ","Y")
    for i=1,sides do
        x = x_center + radius * math.cos(angle)
        y = y_center + radius * math.sin(angle)
        print(string.format("%d\t%f\t%f",i,x,y))
        angle = angle + angle_increment
    end
end

-- Generate a regular hexagon inscribed in unit circle 
polypoints(6, 1.0, 0.0)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!