2011-05-09 127 views
2

我使用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。进行旋转并在旋转后将其翻转回来。

回答

1

您只需将变换矩阵乘以矩形中每个点的位置矢量。

Transform类大概有这样做的方法(有意义),但我找不到可读的“swt gc”API文档,所以我不能告诉你它是什么。

+0

它给了我相同的结果 – nasirus1983 2011-05-09 10:50:08

+0

也许你需要记住将矩形的位置本身添加到每个顶点的坐标。 – James 2011-05-09 11:09:33