我目前正在尝试围绕原点旋转散点图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);
}
关于如何修复它的任何想法?
答案 0 :(得分:1)
你的问题在
title
当您计算新 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
(但这可能不是你想要的)。