Creating a Polygon with n sides

This is a discussion on Creating a Polygon with n sides within the C++ Programming forums, part of the General Programming Boards category; I need help with coming up with the algorithm that is required to create a ngon. The user gives the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    Unhappy Creating a Polygon with n sides

    I need help with coming up with the algorithm that is required to create a ngon. The user gives the center of the polygon (x, y), the distance to the vertices (r), and the number of sides.

    So for say a pentagon i would get the users information and i can from there go to the first point of the polygon (x+r,y) (this must be one of the point of the polygon) so the pentagon is going to be sideways.

    I can get the side length by taking the inner angle (360 / n) which in this case is 72 degrees. i convert that to radians. then i also find degree of the other two equal angles by taking (180 - 72)/2 which gives 54 and convert that to radians. I then use the formula

    r/sin(54 in radians) = sidelength/sin(72 in radians)

    So now i have the side length i just cant figure out the next x and y coordinates to go to. could someone help explain to me how i would go about solving this? the c++ code would be nice but that i can do on my own after i conquer this problem.

    cheers.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Polar -> rectangular is the way to go. So if we pretend for the moment that the center of the ngon is at the origin, then you'll have the corners at (r,0), (r,72), (r,144), (r,216), and (r,288). You can then translate back to where you're supposed to be by adding the center (x,y) to each of those.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-13-2010, 04:14 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  3. Point in polygon test - spherical coords
    By bhdz in forum C Programming
    Replies: 1
    Last Post: 11-07-2007, 01:25 PM
  4. help how to make polygon
    By gamett711221 in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2005, 08:33 PM
  5. IDEA: Polygon unions and intersections
    By Magos in forum Contests Board
    Replies: 3
    Last Post: 05-21-2003, 08:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21