• 9
• 10
• 10
• 9
• 10

# C++ Rotating 2D shape in list

This topic is 733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hay everyone

This is going to be a really basic concept but I am struggling with this immensely and am finding my self getting more and more confused as time goes on so any help with this would be a god send.

Basically I have a list which takes vertices for a shape, print out shape to the screen, such as a triangle and am trying to then implement a function which will rotate the triangle by a give input e.g 20 degrees. I have look over formulas and translating to the origin, perform the rotation and then move back to the centeroid but I an struggling to implement all this, I struggling to to see what the code is doing. Any help would be great thanks.

I have attached some .cpp and header files to the post for anyone to review, thanks. I have renamed the extensions as .txt to allow me to attach them.

##### Share on other sites
Fragment from shape.cpp:
    // rotates the vertices of a shape by a specified angle in degrees
//
// a simple method for rotating a point around 0,0 is
// x = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)
// 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
// remember that 0,0 is at the top left, not the bottom left, corner of the console
// this means that your shapes may appear upside down

Is this home work?

The three steps are literally listed above.

Do you know how to translate? I'd suggest to start with implementing a translation on a vertex, and then on the shape.

##### Share on other sites

Is this home work?

Seems like homework to me too.  But even if it isn't, this is how you learn to program.  Figure out what the code is doing.

##### Share on other sites

Yes it is homework, ok ill look into translating thanks

##### Share on other sites

Fragment from shape.cpp:

    // rotates the vertices of a shape by a specified angle in degrees
//
// a simple method for rotating a point around 0,0 is
// x = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)
// 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
// remember that 0,0 is at the top left, not the bottom left, corner of the console
// this means that your shapes may appear upside down

Is this home work?

The three steps are literally listed above.

Do you know how to translate? I'd suggest to start with implementing a translation on a vertex, and then on the shape.

Would I use a matrix to translate to point of origin?

##### Share on other sites

// x = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)

Instead, I would do this like so:

// x2 = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)

.. So x won't change in the first line, since it's used in the second one too (you're going to need the original x, not the rotated one for y rotation) :)

##### Share on other sites

// x = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)

Instead, I would do this like so:

// x2 = x * cos(degrees in radians) - y * sin(degrees in radians)
// y = y * cos(degrees in radians) + x * sin(degrees in radians)

.. So x won't change in the first line, since it's used in the second one too (you're going to need the original x, not the rotated one for y rotation) :)

There's a good habit I follow which is not to reuse variables. If the coordinates of the unrotated point are x and y, the coordinates of the rotated point should be something else, like x_rotated, y_rotated. Edited by Álvaro

##### Share on other sites

Thanks for the good advice guys, I have been told before not to reuse variables, can anyone point me in the direction of a tutorial of translation etc?

##### Share on other sites

You posted the steps yourself. You don't need another tutorial, you already have the steps documented in your files as a tutorial.  If you want you can put the search terms in your favorite search engine and find more, but you don't need them.

Your teacher has spoon-fed you exactly the information you need, right there in those files you posted.

If you need more help with it, contact your teacher.  It is their job to help you here, and if you don't talk to your teacher they won't know where you are struggling and where you need more instruction.  The teacher will assume you understand the material and move on to more complex material.

Edited by frob

##### Share on other sites

You posted the steps yourself. You don't need another tutorial, you already have the steps documented in your files as a tutorial.  If you want you can put the search terms in your favorite search engine and find more, but you don't need them.

Your teacher has spoon-fed you exactly the information you need, right there in those files you posted.

If you need more help with it, contact your teacher.  It is their job to help you here, and if you don't talk to your teacher they won't know where you are struggling and where you need more instruction.  The teacher will assume you understand the material and move on to more complex material.

Its not the math part that is the issue its applying it to each vertices