C++ Rotating 2D shape in list

Started by
39 comments, last by homi576 8 years ago

Again, talk to your teacher.

Advertisement

Thanks to all the members for there advice. Its a shame the moderators a so pretentious and self righteous. Not a pleasant experience on this site, does not live up to its reputation.

We have a strict rule of not doing people's homework on the site.

We will gladly help with a specific error or a specific problem you are having on homework when someone is clearly working on it and stumbling over something small like a syntax error, but anything more is directed back to your teacher.

This pattern is common to nearly all programming forums and discussion boards.

You are not asking about that type of problem, something where there is a subtle error in your code. You have given us what appear to be the contents of your assignment and asked questions about topics your teacher should have covered.

All I'm asking is how to apply rotation to a vertices, whats the point in knowledge when you don't share it? I'm not asking you to do an assignment Im basically asking to be pointed in the direction of an explanation of the logic, the files provided were just to show what the aim was.

Asking you guys to do the assignment would not benefit me in the slightest, I don't know whether you have miss interpreted my intentions with this thread but I want to learn how to do this, I am not a programming wiz kid or pick up a computer and automatically find myself in the matrix but I am committed to learning programming,

Sorry but it makes me angry that your implying I am here to get someone to do an assignment for me just to get a grade.

I'm not a little kid fresh in school, I'm 25 studying a degree and just need a helping hand which is difficult to get when 500 other people are trying to get that as well.


All I'm asking is how to apply rotation to a vertices,

Like this:


// first translate the object to the origin of the graph (0,0)
// then apply the rotation formula to each vertex of the shape
// finally, translate the object back to its original centroid

While that might be a bit snarky of me, the quoted explanation is a very thorough explanation of the solution.

You say you're not asking us to do the assignment for you, but the things you're asking about are the main things the assignment is about.

whats the point in knowledge when you don't share it?
The point is this should be doable for you with the instructions given by your teacher, and with the thorough step-by-step explanation quoted above. If you can't do it, then the teacher needs to know so s/he can adjust the lessons so they work better. It might be something the teacher went too quickly through, or even forgot to explain altogether.
You side-stepping the lessons and getting the answer from us might benefit you right now, but it will not help others in the same situation. It might also make your teacher think that the lessons are moving along in a good pace, and there is a very high chance there will be more problems later on because of this incorrect assumption.
Talking to your teacher is the best way to solve this.
You've already said you don't really understand most of the listed actions. You are unsure how you translate a point, you are unsure of how to perform the required actions on all vertices, etc.
This points towards a fundemental mismatch between what your teacher is expecting and what you actually know.

Hello to all my stalkers.

So how far did you get with my suggestion to start with a translation of a single vertex?

We don't mind helping you, but "explaining the logic" is either the sentences you already have as comment in your assignment, or the actual code. To me, there is nothing in between, and the latter is exactly the solution you don't even want.

As such, I have no idea whatsoever what you don't understand, or where you are stuck. You haven't shown anything either, so I have nothing to point out where you make the wrong turn.

No doubt it's a simple communication problem, but that happens when you discuss something with a bunch of strangers across a text-only medium. A face-to-face discussion with someone like your teacher, is really a lot better in these cases.

So how far did you get with my suggestion to start with a translation of a single vertex?

We don't mind helping you, but "explaining the logic" is either the sentences you already have as comment in your assignment, or the actual code. To me, there is nothing in between, and the latter is exactly the solution you don't even want.

As such, I have no idea whatsoever what you don't understand, or where you are stuck. You haven't shown anything either, so I have nothing to point out where you make the wrong turn.

No doubt it's a simple communication problem, but that happens when you discuss something with a bunch of strangers across a text-only medium. A face-to-face discussion with someone like your teacher, is really a lot better in these cases.

When you put it like that I see your position. I have gone away and had a look at working on applying the function to one vertices as such here is what I have so far, unfortunately it does not rotate the shape but moves it to the right instead.

Could you give a nod if I'm heading in the right direction or way off.


void Shape::rotate(double degrees)
{
	int x, y, xx, yy;
	double radians;

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

	int testX;
	int testY;

	list<Vertex>::iterator current = vertices.begin();

	while (current != vertices.end())
	{
		testX = (*current).getX();
		testY = (*current).getY();

		radians = degrees * PI / 180;
		xx = round(x * cos(radians) - y * sin(radians));
		yy = round(y * cos(radians) + x * sin(radians));
		
		testX = testX + xx +x;
		testY = testY + yy +y;

		(*current).setX(testX);
		(*current).setY(testY);
	
		current++;
	}
}

The function is them called like so


list<Shape*>::iterator itr = shapes.begin();
	while(itr!=shapes.end())
	{
		(*itr)->drawShape();
		(*itr)->rotate(20);
                Console::clear();
		(*itr)->drawShape();
		itr++;
	}
Your x and y variables are set in the wrong place: they use the position of ONE vertex instead of the loop vertex. So you're doing the same calculation for every vertex.

I have revised the code again and can easily manipulate one of the vertices, the issue is when I iterate through the list it does do the other vertices

This allow to manipulate one of the vertices


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

	
		x = centroid.getX();
		y = centroid.getY();


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

		radians = degrees * 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));

once I add the iteration


	int x, y, xx, yy;
	double radians;
	list<Vertex>::iterator itr = vertices.begin();
	while (itr != vertices.end())
	{
		x = centroid.getX();
		y = centroid.getY();


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

		radians = degrees * 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));

		itr++;
	}

}

I now get no output to the console?

It still has problems. I don't think I should provide any more hints - you should be able to solve this, but the code you're writing indicates that you don't understand some pretty fundamental things about the programming and debugging process.

Read the code that you have written, line by line, think about what it does. Think about what is going into each variable, and how it's being used. ESPECIALLY consider how each line of code will affect the OTHER lines of code that are going to execute later.

Compare this with what you think it should be doing. If you don't know what it should be doing, that's what you need to figure out first. Just copy/pasting code in different arrangements and saying it doesn't work is not how programming works.

You should step through it line-by-line in a debugger, watch what it's doing, and see where it isn't doing what you expect it to do.

This topic is closed to new replies.

Advertisement