在点上旋转平面

时间:2018-12-23 06:52:58

标签: java rotation geometry plane

我有一个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)为地图坐标。该方法是什么样子,您能解释一下它的作用吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,这就是您所需要的:

/**
 * @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};
}

关于数学here

的说明