2011-05-09 9 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); 

을하지만 내가 예상대로 작동하지 않습니다.

I는 I 각 변환 후의 요소의 배열에 geting있어 변환 행렬하여 시도한 :

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

+0

아마도 각 정점의 좌표에 직사각형의 위치를 ​​추가해야한다는 것을 기억해야 할 것입니다. – James