简体   繁体   English

发出在笛卡尔平面上旋转X,Y坐标的问题

[英]Issues Rotating X,Y Coordinate on Cartesian Plane

I am currently attempting to rotate a scatter plot theta degrees about an origin. 我目前正在尝试围绕原点旋转散点图theta度。 Following my previous post, I am using the two sets of equations found here: https://en.wikipedia.org/wiki/Transformation_matrix 在上一篇文章之后,我正在使用在此处找到的两组方程式: 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? 关于如何解决它的任何想法?

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 . 计算新的py您使用的是已经转换的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分支用于负theta If you always want to rotate clockwise, just write theta = std::abs(theta); 如果您总是想顺时针旋转,只需编写theta = std::abs(theta); (but that's likely not what you want). (但这可能不是您想要的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM