我有一个Java类,它可以创建一个可以平移,调整大小和旋转的虚拟屏幕(我们称之为地图)。
但是,当我旋转它时,它只旋转它(0,0)。
要将点转换为屏幕,请先旋转它,然后调整它的大小,然后平移它。
private double dx; //offset in x and y
private double dy;
private double t; //rotation (radians)
private double sx; //scale of x and y
private double sy;
public double[] toScreen(double x, double y) //takes (x, y) on the map and gives (x1, y1) for the screen
{
double[] xy = {x, y};
if(t != 0)
{
double distance = Math.hypot(xy[0], xy[1]);
double theta = Math.atan2(xy[1], xy[0]) + t;
xy[0] = Math.cos(theta)*distance;
xy[1] = Math.sin(theta)*distance;
}
xy[0] *= sx;
xy[1] *= sy;
xy[0] += dx;
xy[1] += dy;
return xy;
}
要设置或更改旋转,您可以操作变量 t
,但它旋转在 (0, 0) 上。
如果我制作一个接受 (x, y) 像public void changeRotation(double t, double x, double y)
一样旋转的方法.
我希望 (x, y) 是地图坐标。这种方法会是什么样子,你能解释一下它的作用吗?
如果我
理解正确,这就是你需要的:
/**
* @param point point (x,y) of the coordinates to be rotated
* @param center point (x,y) of the center (pivot) coordinates
* @param angle in radians
* @return point (x,y) of the new (translated) coordinates
*/
static Point2D.Double rotateAPoint(Point2D.Double point, Point2D.Double center, double angle){
double newX = center.x + Math.cos(angle) * (point.x - center.x) -
Math.sin(angle) * (point.y-center.y) ;
double newY = center.y + Math.sin(angle) * (point.x - center.x) +
Math.cos(angle) * (point.y - center.y) ;
return new Point2D.Double(newX, newY);
}
尝试使用
Point2D.Double point = new Point2D.Double(200,100);
Point2D.Double center = new Point2D.Double(100,100);
double angle = Math.PI/2 ; //90 degress
System.out.println(rotateAPoint(point, center, angle) );
System.out.println(rotateAPoint(point, center, -angle));
如果您更喜欢使用 double[]
:
/**
* @param point (x,y) of the coordinates to be rotated
* @param center (x,y) of the center (pivot) coordinates
* @param angle in radians
* @return (x,y) of the new (translated) coordinates
*/
static double[] rotateAPoint(double[] point, double[] center, double angle){
double newX = center[0] + Math.cos(angle) * (point[0] - center[0]) -
Math.sin(angle) * (point[0]-center[0]) ;
double newY = center[1] + Math.sin(angle) * (point[1] - center[1]) +
Math.cos(angle) * (point[1] - center[1]) ;
return new double[]{newX, newY};
}
关于数学的解释在这里