gpt4 book ai didi

c++ - 在笛卡尔平面上旋转 X、Y 坐标的问题

转载 作者:行者123 更新时间:2023-11-28 05:28:18 24 4
gpt4 key购买 nike

我目前正在尝试围绕原点旋转散点图 theta 度。在我之前的帖子之后,我正在使用此处找到的两组方程式:https://en.wikipedia.org/wiki/Transformation_matrix

注意:这与我之前的帖子不同,因为这篇帖子解决了方程式的实现如何不起作用。

然而,在应用方程式之后,它根本不是绕原点旋转。

这是我当前的代码:

//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);

}

关于如何修复它有什么想法吗?

最佳答案

你的问题在

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

当您计算新的 py 时,您使用的是已经转换的 px。只需做这样的事情:

    float px_new = px * cos(rad) - py * sin(rad);
float py_new = py * cos(rad) + px * sin(rad);
px = px_new; py = py_new;

我也不确定为什么您需要 if 分支来获取负 theta。如果您总是想顺时针旋转,只需编写 theta = std::abs(theta);(但这可能不是您想要的)。

关于c++ - 在笛卡尔平面上旋转 X、Y 坐标的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com