旋转后的矩形坐标
我正在使用 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。进行旋转,旋转后将其平移回来。
I'm using swt gc for drawing images. One of the option of my program is to rotate the image. I'm also drawing a rectangle as a border. To rotate I'm using following code:
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();
After rotation I would like to calculate positions of corners of my rectangle.
I was trying something like this:
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);
But it does not working as I expected.
I've also tried using transformation matrix which I'm geting into elements array after each transformation:
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];
But it gives same results as using equations for rotation. Any ideas ?
I've solved it:
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;
I had back to 0,0. Make rotation and after rotation translate it back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将变换矩阵乘以矩形每个点的位置向量即可。
Transform 类大概有一个方法可以做到这一点(有意义),但我找不到可读的“swt gc”API 文档,所以我无法告诉你它是什么。
You just need to multiply the transformation matrix by the position vector for each of the points of your rectangle.
The Transform class presumably has a method for doing this (would make sense), but I can't find readable "swt gc" API documentation, so I can't tell you what it is.