# Help with drawing a polygon with N sides [closed]

I am at university on my games development, and I have been given work to fill in an empty pixel plotter program using the methods they have supplied for it to teach us the algorithms used in 2 dimensional games.

I am currently trying to draw a polygon of N Sides using a function called void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour ) which we have been given the parameter but not the algorithm.

Here is my attempt so far:

void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour )
{
// Fill in the correct code here
int x = 0;
int y = 0;
//float currentr = 360 / Sides;
int n = 0;
int r = R;
for (n = 0; n < Sides; n++)
{
x = r * cos(2 * PI*n / Sides) + X;
y = r * sin(2 * PI* n / Sides) + Y;
SetViewportPixel(x, y, PixelColour); // this is where the function is called
// and the pixel is set from this.
}

}


Currently the code does not work as desired, at the moment I have only reached drawing all 4 points, but something when drawing the polygon makes the points go skew-wif so to speak.

The left image is the trace that is drawn as a bounding box from the mouse, dragging it makes it larger like any image editing software, but the result is on the right, seems to set the angle off somehow, not sure why as the point should correlate to the trace. <<-- This image shows the actual result.

I am also of course required to draw the outline of the polygon, so I assume that I should store in a cycle of points to create a triangle so I can draw the hypotenuse, but I am unsure how to do this.

Any help would be appreciated.

NOTE: unfortunately the code for the trace is hidden, I've searched the whole program so I don't unfortunately know how that works.

• Why are you adding R to your angles? – kolrabi Jan 27 '16 at 9:40
• @kolrabi Not sure, Think thats a mistake, still has a similar outcome on removal though. – RNewell122 Jan 27 '16 at 9:41
• @kolrabi nevermind fixed that problem, now I just have the difficulty of drawing from point to point. – RNewell122 Jan 27 '16 at 9:49
• I wouldn't recommend showing this sort of stuff from a Uni course, it's not fair to the creator. Asking about the theory would be more ethical in my opinion. – Syntac_ Jan 27 '16 at 10:00
• The university is ok with it, and so is the lecturer. – RNewell122 Jan 27 '16 at 11:32

I understand you also had problems drawing a line from point to point. I recommend you look into "Bresenham's Line Algorithm" as it typically gives the best result. Here is a Wikipedia article about it.

The article includes pseudocode for the algorithm, including one that only relies on integer arithmetic. I won't be posting it here as I encourage you to actually read it. Once you have it working it should only be a matter of cycling through the points drawing lines between them. Hopefully this helps.

• I'm sitting here wondering if this is really a gamedev related question to begin with. – Nils Ole Timm Jan 27 '16 at 10:03
• I thought it was because in the next worksheet we apply these principles to drawing pixels in a 2dimensional game, sorry I should have made it more clear. – RNewell122 Jan 27 '16 at 10:07
• @NilsOleTimm If it makes you feel any better, I think game-programmers have more knowlege about graphics and graphics algorithms than your standard web-developer, system maintainer or what have you. So I don't blame him for posting here. I go by the test of "Would a game-programmer know more about this than your average programmer?". – Christer Jan 27 '16 at 10:10
• I go by the same test, so I guess we have a different perspective then. I think any programmer can easily solve that issue. – Nils Ole Timm Jan 27 '16 at 10:13
• @Christer funnily enough , I have already used Bresenhams line algorithm, for the line function, I did not think of it thats such a simple answer. I obviously overcomplicated it in my head. – RNewell122 Jan 27 '16 at 10:20

You're not getting the angles properly, the sum of the internal angle of a simple polygon can be calculated with the formula: π(n-2).

We then need the external angle which is the angle from point to point, this can be calculated with : π-internalAngle.

Assuming the first point is in the lower left and there's no rotation this code should work.

void PixelPlotterForm::DrawPolygon( int Sides, int X, int Y, int R, Color PixelColour )
{
// Fill in the correct code here
int x = X;
int y = Y;
internalAngle = PI * (Sides-2) / Sides;
externalAngle = PI - internalAngle
for (int n = 0; n < Sides; n++)
{
x += R * cos(externalAngle * n);
y += R * sin(externalAngle * n);
SetViewportPixel(x, y, PixelColour); // this is where the function is called
// and the pixel is set from this.
}
}

• This doesn't appear to work, it offsets the polygon and then misses the first point. – RNewell122 Jan 27 '16 at 10:15
• i.gyazo.com/4241d2a63b55c10ad5a0eb299e493746.png << heres an image of the outcome. – RNewell122 Jan 27 '16 at 10:17
• Made a mistake, have edited. – Tim Jan 27 '16 at 10:19
• Seems not to work again, maybe there is a discrepancy in the function parameters I need to correct ? i.gyazo.com/2a2bc409d87d8bcddf9bfaed74ab3624.png << image outcome – RNewell122 Jan 27 '16 at 10:23
• Ahhhh, I figured out the mistake, sorry the first point is always the top point, and the coordinates work downwards, as the Y coordinates actually start 0 at the top. I should have clarified that @Tim – RNewell122 Jan 27 '16 at 10:27