user3341249 - 1 year ago 62
C++ Question

# Issues Rotating X,Y Coordinate on Cartesian Plane

I am currently attempting to rotate a scatter plot theta degrees about an origin. Following my previous post, I am using the two sets of equations found here: https://en.wikipedia.org/wiki/Transformation_matrix

Note: This differs from my previous post, as this post addressed how the implementation of the equations is not working.

However, after applying the equations, it is not rotation about the origin at all.

Here is my current code:

``````//Loop through all points
for (int playerEntity = 0; playerEntity < 13; playerEntity++) {

//x and y coords for point
int px;
int py;

//If rotation angle requires counter clockwise (- degree) rotation
if (theta< 0) {

//Load point from scatter plot relative to origin
px = positionX[playerEntity] / 20;
py = positionY[playerEntity] / 20;

//Convert the degree rotation to radians
double rad = (theta*3.1415) / 180;

//Apply Counter Clockwise rotation equations
}
else {
//Load point from scatter plot relative to origin
px = positionX[playerEntity] / 20;
py = positionY[playerEntity] / 20;

//Convert the degree rotation to radians
double rad = (theta*3.1415) / 180;

//Apply Clockwise rotation equations
}

//Define origin
int originX = 1000;
int originY = 500;

//Graph points (Note it subtracts py, as negative y values make it plot upwards and positive y values make it plot downwards
colorPixel(originX + px , originY- py);

}
``````

Any ideas on how to fix it?

``````    px = px * cos(rad) - py * sin(rad);
When you calculate the new `py` you are using the already transformed `px`. Just do something like this:
``````    float px_new = px * cos(rad) - py * sin(rad);
I'm also not sure why you need the `if` branch for negative `theta`. If you always want to rotate clockwise, just write `theta = std::abs(theta);` (but that's likely not what you want).