gpt4 book ai didi

java - 旋转后的矩形坐标

转载 作者:行者123 更新时间:2023-11-30 11:54:01 24 4
gpt4 key购买 nike

我正在使用 swt gc 绘制图像。我的程序的一个选项是旋转图像。我还绘制了一个矩形作为边框。要旋转,我使用以下代码:

Transform oldTransform = new Transform(gc.getDevice());  
gc.getTransform(oldTransform);

Transform transform = new Transform(GCController.getCanvas().getDisplay());
transform.translate(this.x+width/2, this.y+height/2);
transform.rotate(rotation);
transform.scale(this.scaleX, this.scaleY);
transform.getElements(elements);
transform.translate(-this.x-width/2, -this.y-height/2);

gc.setTransform(transform);
gc.drawImage(image, this.x, this.y);
gc.setTransform(oldTransform);

transform.dispose();

旋转后我想计算矩形角的位置。我正在尝试这样的事情:

    int tx = (this.x+width/2);
int ty = (this.y+height/2);

double rot = rotation * Math.PI/180;

double newX = tx*Math.cos(rot) - ty*Math.sin(rot);
double newY = tx*Math.sin(rot) + ty*Math.cos(rot);

但它并没有像我预期的那样工作。

我也尝试过使用转换矩阵,每次转换后我都会进入元素数组:

int tx = (this.x+width/2);
int ty = (this.y+height/2);

double newX = this.x * elements[0] + this.y * elements[2];
double newY = this.x * elements[1] + this.y * elements[3];

但它给出的结果与使用旋转方程相同。有什么想法吗?

我已经解决了:

    int tx = (-width/2);
int ty = (-height/2);

double rot = rotation * Math.PI/180;

double newX = (tx)*Math.cos(rot) - (ty)*Math.sin(rot) + this.x + width/2;
double newY = (tx)*Math.sin(rot) + (ty)*Math.cos(rot) + this.y + height/2;

我回到了 0,0。进行旋转并在旋转后将其平移回来。

最佳答案

您只需将变换矩阵乘以矩形每个点的位置 vector 即可。

Transform 类可能有执行此操作的方法(这很有意义),但我找不到可读的“swt gc”API 文档,所以我无法告诉您它是什么。

关于java - 旋转后的矩形坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5935424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com