
December 22nd, 2010, 03: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, 03:49 PM
#2
Re: Set Sided Polygon in C++
What does not work as you expected?
Victor Nijegorodov

December 22nd, 2010, 04: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 04:37 PM.
mpliam

December 22nd, 2010, 04: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, 05: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, 09: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, 05: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, 05: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, 07: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, 06: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 )

October 16th, 2019, 02:30 PM
#11
Re: Set Sided Polygon in C++
did you get the whole code? I am stuck with this problem

October 16th, 2019, 02:31 PM
#12
Re: Set Sided Polygon in C++
could anyone help me with this problem?

October 17th, 2019, 03:27 AM
#13
Re: Set Sided Polygon in C++
Originally Posted by basilmon
could anyone help me with this problem?
What is the issue  you don't say??
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only  not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums  and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.5)

October 17th, 2019, 03:58 AM
#14
Re: Set Sided Polygon in C++
When I run the code I only getting single pixel , not even a side of polygon.

October 17th, 2019, 04:03 AM
#15
Re: Set Sided Polygon in C++
Originally Posted by basilmon
When I run the code I only getting single pixel , not even a side of polygon.
And what is the code you run?
Could you post it?
Victor Nijegorodov
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
OnDemand Webinars (sponsored)
