java - 在一点上旋转平面
问题描述
我有一个 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};
}
关于这里的数学解释
推荐阅读
- jdeveloper - JAVA正则表达式字符识别
- java - 为什么用户ID不按顺序输入?
- r - 嵌套循环查找最新评论
- c++ - 在下面的代码中使用释放和移动的区别
- ruby-on-rails - 是否可以使用 Liquid shopify 读取本地文件内容并将其内容加载到页面中
- python - 我已经在我的电脑上尝试过,它运行良好,但在 pyschools 网站上仍然出现“浮点除以零”。为什么 ??:
- dart - 缺少 Getter 和 Setter 的具体实现
- dialogflow-es - 使用 java dialogflowv2 生成包含快速回复的 WebhookResponse
- sql - 我的数据只显示一个,但数据应该不止一个
- lambda - 使用 Terraform/CloudFormation/Something 有效地部署 lambdas(只部署那些改变的)