2011-01-01 4 views
4

나는 잠시 동안이 일을 시도했지만 많은 성공을 거두지 못했습니다. 내가하고 싶은 것은 사각형을 회전시킨 다음 회전 된 점들을 포함하는 새로운 사각형을 만드는 것입니다.Microsoft.XNA.Framework.Rectangle 회전 및 해당 회전을 기반으로 사각형 만들기?

누구나 어떻게해야 제대로 할 수 있습니까?

코드 나는이 작동하지 않습니다,하지만 예를 들어, (숫자가 내가 그것을 실제로 작동 생각하게) 정확히 잘못된거야 어디 다음과 같은 값을 가진 사각형이 있는지 확실하지 않습니다 :

{X:865 Y:76 Width:22 Height:164} 

결과는 다음과 같습니다가 회전 -1.57094443

{X:1863 Y:1740 Width:164 Height:22} 

내가 뭘하는 잡아 원래 사각형의 네 점이며,이 기능을 회전 :

'앵커'피벗 점 (I이 기능은 수학적으로 소리 있는지 확실하지 않습니다), 나는이 함께 회전 된 사각형의 모서리를 결정하는 것입니다
static public Vector2 RotateVectorAround(Vector2 anchor, Vector2 vector, float rotation) 
     { 
      Matrix mat = new Matrix(); 
      mat.Translation = new Vector3(vector.X - anchor.X, vector.Y - anchor.Y, 0); 

      Matrix rot = new Matrix(); 
      rot = Matrix.CreateRotationZ(rotation); 

      mat *= rot; 

      mat.Translation += new Vector3(anchor.X, anchor.Y, 0); 

      return new Vector2(mat.Translation.X, mat.Translation.Y); 
     } 

:

Vector2 newTopLeft = new Vector2( Math.Min(Math.Min(topleft.X, bottomRight.X), Math.Min(bottomleft.X, topright.X)), 
       Math.Min(Math.Min(topleft.Y, bottomRight.Y), Math.Min(bottomleft.Y, topright.Y))); 

      Vector2 newBottomRight = new Vector2(
       Math.Max(Math.Max(topleft.X, bottomRight.X), Math.Max(bottomleft.X, topright.X)),     
       Math.Max(Math.Max(topleft.Y, bottomRight.Y), Math.Max(bottomleft.Y, topright.Y))); 

답변

6

당신을 사각형의 점에 회전 행렬을 곱할 수 있습니다.

그래서 당신이 전에 피벗 점을 빼지 수있는 점에 대해 회전

a = degrees * (PI/180) 

Rx = Px * cos(a) + Py * -sin(a) 
Ry = Px * sin(a) + Py * cos(a) 

회전을하다

R 점 가 발생합니다 회전에 점 P 제공 그것을 회전시키고 회전 후에 다시 추가하십시오 (따라서 회전은 사실상 (0,0)입니다)

Px = Px - PivotX 
Py = Py - PivotY 

Rx = Px * cos(a) + Py * -sin(a) 
Ry = Px * sin(a) + Py * cos(a) 

Px = Rx + PivotX 
Py = Ry + PivotY 

나는 2 차원 회전 여기 3'th 차원을 사용하지 것 같은 (여기 유감없이 VStudio)는 XNA에서

:

point -= pivot 
point = Vector2.Transform(point, Matrix.CreateRotationZ(angle)); 
point += pivot 
관련 문제