user3341249 - 6 months ago 27

C++ Question

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

px = px * cos(rad) - py * sin(rad);

py = py * cos(rad) + px * sin(rad);

}

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

px = px * cos(rad) + py * sin(rad);

py = py * cos(rad) - px * sin(rad);

}

//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?

Answer

Your problem is in

```
px = px * cos(rad) - py * sin(rad);
py = py * cos(rad) + px * 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);
float py_new = py * cos(rad) + px * sin(rad);
px = px_new; py = py_new;
```

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).