
December 22nd, 2010, 02:26 PM
#1
Set Sided Polygon in C++
Hello basically I've been working on this for 2 days now, I'm completely stuck. I've looked around the internet and can't really find anything in relation.
I have a program that I've created, It makes shapes. I've managed to make a draw line function & a square function.
Now it's sort of like a pixel plotter, with X & Y as the view ports.
I'm struggling to create a polygon with a given number of sides i.e. the user inputs how many sides that want.
Any help would be much appreciated, also I'm pretty rubbish at triangularly and that kinda stuff, so noobish explanations please!
This is what I got so far, still hasn't managed to figure out what i need next :(
Code:
// Draw a regular polygon with the given number of sides. The centre of the polygon will be at
// (X, Y) and the points are on a circle of radius R
void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour )
{
int x = X;
int y = Y;
int r = R;
int sides = Sides;
float cir = PI * (r * 2);
float edgelength = cir / sides;
float tempsides = 0;
float angle = 360 / sides;
while(tempsides < sides)
{
//set view port pixel, this acually places the pixel on the program, X, Y and colour (ignore colour for now, it works)
SetViewportPixel(x + r, y + r, PixelColour);
r = r + angle;
tempsides++;
}
}

December 22nd, 2010, 02:49 PM
#2
Re: Set Sided Polygon in C++
What does not work as you expected?
Victor Nijegorodov

December 22nd, 2010, 03:08 PM
#3
Re: Set Sided Polygon in C++
I do not understand your algorithm. Perhaps there is something I am missing. It seems to me that if one specifies a polygon with the number of sides greater than or equal to 3, there are an infinite number of possible such polygons. Thus one would need to specify the length of each side and some clever algorithm to determine the angles between each side (such algorithms exist  try google). For more details, see:
http://www.mathleague.com/help/geome...ns.htm#polygon
After further thought I realized you must by trying to inscribe a regular polygon ( all sides equal ) on a circle. You will need some algorithm to divide the circumference of the circle into N+1 segments, then draw lines from each segment point to the next going around the circle. The math shouldnt be to difficult. I would be interested in seeing how you solve the problem.
Hope this is helpful
Last edited by Mike Pliam; December 22nd, 2010 at 03:37 PM.
mpliam

December 22nd, 2010, 03:16 PM
#4
Re: Set Sided Polygon in C++
I've almost got it figured, I've got it too do one side of the polygon when a given side is given.
Basically I'm struggling too create it with an infinite number of sides.

December 22nd, 2010, 04:43 PM
#5
Re: Set Sided Polygon in C++
Anyone have any idea how I would do this, been trying to figure it out for ages!

December 22nd, 2010, 08:03 PM
#6
Re: Set Sided Polygon in C++
Originally Posted by Mike Pliam
It seems to me that if one specifies a polygon with the number of sides greater than or equal to 3, there are an infinite number of possible such polygons.
A polygon with less than three sides is an interesting concept... Aside from that: The possibilities of any polygon (number of sides >= 3) are infinite unless you specify some parameters. Even if you postulate a regular polygon they're infinite unless you specify a center and a starting point (e.g.).
Originally Posted by abakiz
Basically I'm struggling too create it with an infinite number of sides.
Hint: A regular polygon with an infinite number of sides is a circle.
Aside from that: http://en.wikipedia.org/wiki/Polygon
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

December 23rd, 2010, 04:37 AM
#7
Re: Set Sided Polygon in C++
I see what your saying, but what sort of algorithm could I do this with?

December 23rd, 2010, 04:54 AM
#8
Re: Set Sided Polygon in C++
What I got so far:
Code:
int x = X;
int y = Y;
//The radious, half of the circle length
int r = R;
int sides = Sides;
float cir = PI * (r * 2);
//basically saying, the edgelength and shave off the bit when you curve it straight, so you get the true length of each side.
float edgelength = (cir / sides)  ((cir / sides) / PI);
//splitting the angle of the sides
float angle = cir / sides;
//you need to rotate or move the line to the next angle / side or something.
float EndX_Y;

December 23rd, 2010, 06:19 AM
#9
Re: Set Sided Polygon in C++
The point on a circle centered on (xc,yc) with radius r are given by
x = xc + r*cos(angle)
y = yc + r*sin(angle)
Code:
void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R )
{
double xc = X;
double yc = Y;
double radius = R;
double angle_step = 2.0 * PI / static_cast<double>(Sides);
for (int i=0; i<Sides; ++i)
{
double angle = angle_step * i;
double x = xc + radius*cos(angle);
double y = yc + radius*sin(angle);
}
}

January 14th, 2014, 05:59 PM
#10
Re: Set Sided Polygon in C++
abakiz, did you manage to fix this issue? Im working on something similar and as I am new to this struggling with:
void PixelPlotterForm:rawRectangle( int X, int Y, int Width, int Height, Color PixelColour )
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
