[英]Rotating a point around a center point
I am aware that there are a few questions about points and rotation out here, and I feel like Im almost there. 我知道这里有一些关于点和旋转的问题,我觉得我快到了。 I youst need a little push.
我需要一点推动力。
I Have a shape with 6 points like this one. 我有一个像这样的6点形状。
I want to rotate the Point P around Point C 我想绕着C点旋转P点
And I need to do this manually so I am not interested in using AffineTransform 而且我需要手动执行此操作,因此我对使用AffineTransform不感兴趣
thanks in advance 提前致谢
Thread thread = new Thread() {
public void run() {
//THE RADIUS OF THE SHAPE IS 100
//GET THE POINT P
PointClass point_class = points.get(0);
//GET THE CENTER POINT C
Point center = new Point(point_class.point.x - 100, point_class.point.y);
int deg = 0;
while(deg < 360) {
//GET THE ANGLE IN RADIANS
double angle = Math.toRadians(deg);
//FIRST TRANSLATE THE DIFFERENCE
int x1 = point_class.point.x - center.x;
int y1 = point_class.point.y - center.y;
//APPLY ROTATION
x1 = (int) ((double) x1 * Math.cos(angle) - y1 * Math.sin(angle));
y1 = (int) ((double) x1 * Math.sin(angle) + y1 * Math.cos(angle));
//TRANSLATE BACK
point_class.point.x = x1 + center.x;
point_class.point.y = y1 + center.y;
//ROTATE + 1 DEEGRE NEXT TIME
deg++;
try {
//SLEEP TO SEE THE DIFFERENCE
sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
thread.start();
What happens with this code is that the Point P ends up in the center like this 这段代码会发生这样的情况,即P点最终像这样居中
I think your radius is shrinking each time through the while loop due to the casting of doubles to ints. 我认为由于将double转换为int,每次遍历while循环时半径都会缩小。 This might work better instead:
相反,这可能会更好:
double x1 = point_class.point.x - center.x;
double y1 = point_class.point.y - center.y;
//APPLY ROTATION
x1 = x1 * Math.cos(angle) - y1 * Math.sin(angle));
y1 = x1 * Math.sin(angle) + y1 * Math.cos(angle));
//TRANSLATE BACK
point_class.point.x = (int)Math.ceil(x1) + center.x;
point_class.point.y = (int)Math.ceil(y1) + center.y;
So I figured out what was wrong. 因此,我找出了问题所在。
The Translation of the two points, 两点的翻译,
//FIRST TRANSLATE THE DIFFERENCE
double x1 = point_class.point.x - center.x;
double y1 = point_class.point.y - center.y;
has to go outside of the loop, because I need to take base at that location when applying the rotation matrix. 必须走出循环,因为应用旋转矩阵时我需要在该位置取基。 And also in the loop, I should have the deegre fixed at 1, so that it is only incrementet by 1 and not 81+82+83...Dont know why i did that.
而且在循环中,我应该将Deegre固定为1,这样它才是增量1,而不是81 + 82 + 83 ...不知道我为什么这样做。
Hope this helps someone =) 希望这可以帮助某人=)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.