WPF: Getting new coordinates after a Rotation

前端 未结 5 1239
一向
一向 2020-12-09 04:57

With reference to this programming game I am currently building.

alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg

To translat

相关标签:
5条回答
  • 2020-12-09 05:40

    I use this method:

    Point newPoint = rotateTransform.Transform(new Point(oldX, oldY));
    

    where rotateTransform is the instance on which I work and set Angle...etc.

    0 讨论(0)
  • 2020-12-09 05:44

    Look at GeneralTransform.TransformBounds() method.

    0 讨论(0)
  • 2020-12-09 05:46

    If I understand your question right:

    given:
    shape has corner (x1,y1), center (xc,yc)
    rotated shape has corner (x1',y1') after being rotated about center
    
    desired:
    how to map any point of the shape (x,y) -> (x',y') by that same rotation
    

    Here's the relevant equations:

    (x'-xc) = Kc*(x-xc) - Ks*(y-yc)
    (y'-yc) = Ks*(x-xc) + Kc*(y-yc)
    

    where Kc=cos(theta) and Ks=sin(theta) and theta is the angle of counterclockwise rotation. (to verify: if theta=0 this leaves the coordinates unchanged, otherwise if xc=yc=0, it maps (1,0) to (cos(theta),sin(theta)) and (0,1) to (-sin(theta), cos(theta)) . Caveat: this is for coordinate systems where (x,y)=(1,1) is in the upper right quadrant. For yours where it's in the lower right quadrant, theta would be the angle of clockwise rotation rather than counterclockwise rotation.)

    If you know the coordinates of your rectangle aligned with the x-y axes, xc would just be the average of the two x-coordinates and yc would just be the average of the two y-coordinates. (in your situation, it's xc=75,yc=85.)

    If you know theta, you now have enough information to calculate the new coordinates. If you don't know theta, you can solve for Kc, Ks. Here's the relevant calculations for your example:

    (62-75) = Kc*(50-75) - Ks*(50-85)
    (40-85) = Ks*(50-75) + Kc*(50-85)
    
    -13 = -25*Kc + 35*Ks = -25*Kc + 35*Ks
    -45 = -25*Ks - 35*Kc = -35*Kc - 25*Ks
    

    which is a system of linear equations that can be solved (exercise for the reader: in MATLAB it's:

    [-25 35;-35 -25]\[-13;-45]
    

    to yield, in this case, Kc=1.027, Ks=0.3622 which does NOT make sense (K2 = Kc2 + Ks2 is supposed to equal 1 for a pure rotation; in this case it's K = 1.089) so it's not a pure rotation about the rectangle center, which is what your drawing indicates. Nor does it seem to be a pure rotation about the origin. To check, compare distances from the center of rotation before and after the rotation using the Pythagorean theorem, d2 = deltax2 + deltay2. (for rotation about xc=75,yc=85, distance before is 43.01, distance after is 46.84, the ratio is K=1.089; for rotation about the origin, distance before is 70.71, distance after is 73.78, ratio is 1.043. I could believe ratios of 1.01 or less would arise from coordinate rounding to integers, but this is clearly larger than a roundoff error)

    So there's some missing information here. How did you get the numbers (62,40)?

    That's the basic gist of the math behind rotations, however.

    edit: aha, I didn't realize they were estimates. (pretty close to being realistic, though!)

    0 讨论(0)
  • 2020-12-09 05:48

    I'm not sure, but is this what you're looking for - rotation of a point in Cartesian coordinate system: link

    0 讨论(0)
  • 2020-12-09 06:00

    You can use Transform.Transform() method on your Point with the same transformations to get a new point to which these transformations were applied.

    0 讨论(0)
提交回复
热议问题