My octagon is a circle and my pentagon is wonky :'(

Started by
13 comments, last by jHaskell 8 years ago

Its me the shape guy :/

Ok, i spent most of to trying to figure this out but I've hit a brick wall mentally, I output and image of a diamond and its all good, lines are straight and looks nice, I add functionality to draw a octagon and a pentagon and things stop drawing correctly.

The pentagon isn't far off but there's a few (x,y) * out of place but the octagon is...well...its a damn circle <_<

I use a formula of rotation based on the shapes centroid, with the octagon the vertices would be 72 degrees apart, in total 360 degrees with 5 points. Its just not happening for some reason. I have tried apply different degrees, looking at the draw line algorithm, clearing the vertices list before adding new vertices (just in case it was using vertices for other shapes) but still the issue persists.

The code below is the function for plotting the vertices of the octagon:


void octagon::plotVertices()
{
	int x, y, xx, yy;
	double radians;

	x = centroid.getX();
	y = centroid.getY() + radius;
	vertices.push_back(Vertex(x, y));

	x = vertices.back().getX() - centroid.getX();
	y = vertices.back().getY() - centroid.getY();

	radians = 45 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));
	
	radians = 90 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 135 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 180 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 225 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 270 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 315 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));

	radians = 360 * PI / 180;
	xx = round(x * cos(radians) - y * sin(radians));
	yy = round(y * cos(radians) + x * sin(radians));
	xx = xx + centroid.getX();
	yy = yy + centroid.getY();
	vertices.push_back(Vertex(xx, yy));
}

below is an image of the output:

https://drive.google.com/file/d/0B1dEh7m__i2naEZqa01xR2ZZRG8/view?usp=sharing

:(

Advertisement
I have one word for you: loop.

:)


	int x, y, xx, yy;
	double radians;

	x = centroid.getX();
	y = centroid.getY() + radius;
	vertices.push_back(Vertex(x, y));

	x = vertices.back().getX() - centroid.getX();
	y = vertices.back().getY() - centroid.getY();

	int degree = 72;
	for (int i = 0; i < 5; i++)
	{
		radians = degree * PI / 180;
		xx = round(x * cos(radians) - y * sin(radians));
		yy = round(y * cos(radians) + x * sin(radians));
		xx = xx + centroid.getX();
		yy = yy + centroid.getY();
		degree = degree + 72;
		vertices.push_back(Vertex(xx, yy));
	}

still wonky tho :P

You haven't shown us what your pentagon looks like.

You haven't shown us what your pentagon looks like.

You have to buy me dinner first, just kidding.

The link in the original post shows the output

The link in the original post shows the output


No, it doesn't. The pentagon is truncated.

Is the weird appearance just due to the non-square grid that you're drawing to?

Inspiration from my tea:

"Never wish life were easier. Wish that you were better" -Jim Rohn

soundcloud.com/herwrathmustbedragons

Each cell of the console is not perfectly square. By default, on Win10, for example, my console cells are 8x16. They are twice as large as they are wide.

Also, the asterisk isn't centered properly in the cell, so try outputting a character like this.

octagon

https://drive.google.com/file/d/0B1dEh7m__i2nUzJPV3FKcUk1Q1k/view?usp=sharing

pentagon

https://drive.google.com/file/d/0B1dEh7m__i2ncUVyN20wbEtKdDA/view?usp=sharing

I thought it might be the grid but the diamond outputs fine

That looks like a perfectly good pentagon to me. If you are complaining that a 2-color display with a resolution of something like 20x20 isn't very good, I have to agree with you.

This topic is closed to new replies.

Advertisement