我编写了一个代码,应该将一个点逆时针绕另一个点旋转。但它无法正常工作。
public boolean contains(double x, double y) {
double ox = this.x.get() + (this.width.get()/2);
double oy = this.y.get() + (this.height.get()/2);
double theta = rotate.get() - (rotate.get() * 2);
double px1 = Math.cos(theta) * (x-ox) - Math.sin(theta) * (y-oy) + ox;
double py1 = Math.sin(theta) * (x-ox) + Math.cos(theta) * (y-oy) + oy;
return shape.contains(px1, py1);
}
x、y - 是要旋转的点的坐标。
ox,oy - 是要围绕其旋转的点的坐标。
rotate.get() - 旋转角度
更新:解决问题的代码的修改,谁能派上用场:
double px1 = Math.cos(Math.toRadians(theta)) * (x-ox) - Math.sin(Math.toRadians(theta)) * (y-oy) + ox;
double py1 = Math.sin(Math.toRadians(theta)) * (x-ox) + Math.cos(Math.toRadians(theta)) * (y-oy) + oy;
最佳答案
请检查您的 rotate.get()
是否会为您提供度数值(例如 45°)或弧度值(例如 0.5*pi)。 Math.sin()
和 Math.cos()
仅接受弧度。
要转换它们,您可以使用类似 angle = Math.toRadians(45)
关于java - 以给定角度旋转一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53519156/