Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  1. pick an angle to start from a radius and a center point
  2. somehow calculate the x,y position at that distance from the center(how?)
  3. divide 360 by the number of sides, move that distance and draw the next line from the first x,y point
  4. continue until the angle=360 divided by that number.

Assuming that my assumptions are correct, the main thing is to understand how to compute the x,y points.

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.

like image 495
alphablender Avatar asked Aug 25 '11 22:08

alphablender


People also ask

How do you draw a regular polygon in Matlab?

Description. pgon = nsidedpoly( n ) returns a regular polygon with n equal-length sides. The center of pgon is at the point (0,0), and the circumscribed circle of the polygon has radius 1. pgon = nsidedpoly( n , Name,Value ) specifies additional properties of the polygon using one or more name-value pair arguments.

What are the coordinates of the polygon?

Polygons are usually represented by the coordinates of their vertices. A polygon may have any number of vertices and any number of edges. The coordinates of the vertices are typically represented using WGS84 latitude and longitude pairs.


2 Answers

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 
like image 51
Oliver Charlesworth Avatar answered Oct 06 '22 04:10

Oliver Charlesworth


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.

like image 29
andrew cooke Avatar answered Oct 06 '22 02:10

andrew cooke