Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pseudo-code algorithm to create regular polygon with X number of sides

Started by Ueneth Echil Sep 5, 2007 at 5:49 PM 0 replies 5.4k views
Original Post
Ueneth Echil
Ueneth Echil
I'm working on a program for Computer Science II at my High School, I've gotten permission to get help from my teacher. Basically, you are given to write a program that will draw a polygon , a regular one where the sides are of equal length, given an x amount of sides. A draw method is provided, where you input two arrays, the x and y coordinates. Basically, you give the method a set of points, and it will draw, connect, and fill the closed polygon. I'm looking for pseudo-code, or just a description of how to do this. I've ran into problems and haven't been able to try it out, but thinking that depending on the relative location of x and y I can either add or take away to their values to move it to the left, right, up or down. To figure out by how much, I would take the number of sides, divide it by two, and then divide the radius by it, the radius provided. However, this will run into problems with odd numbered polygons.
Ravyne
Ravyne
I'll take your word that its ok seek help with this function, but I won't go as far as to provide source/psuedo code.

This is the approach I would take:

Given the sole constraint that the sides must be an equal length for an arbitrary number of sides, the simplest approach is to inscribe an n-sided polygon within an arbitrary circle of your choosing.

So, choose a center for this circle and give it a reasonable radius. Then, using basic trigonometry, chose n points spaced equidistantly around the circumference of the circle (ie - if n is 3, chose 3 points on the circumference that are 120 degrees apart from one another).

This approach works for any n larger than 2, resulting in non-intersecting, convex polygons. Further, this approach even holds for degenerate polygons such as Digons (2-sided polygons) and, depending how you interpret the results, Henagons (1-sided polygons which are generally considered an imposibility in Euclidean geometry, but are defined in spherical geometry.)

[Edited by - ravyne2001 on September 5, 2007 6:17:21 PM]
throw table_exception("(? ???)? ? ???");

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.