WPF:旋转后获取新坐标
替代文本 http://img12。 imageshack.us/img12/2089/shapetransformationf.jpg
要在 WPF 中翻译 Canvas,我使用两种表单:TranslateTransform
(移动它)和 RotateTransform
(旋转它)[同一个子元素 TransformationGroup
]
当画布不旋转(或旋转 90 度)时,我可以轻松获取画布的左上角 x,y 坐标,因为它是相同的),但我面临的问题是获取左上角(和其他 3 个点)坐标。
这是因为当应用 RotateTransform
时,TranslateTransform
的 X
和 Y
属性不会更改(并且因此仍然表明正方形的左上角就像虚线正方形(来自图像)
画布正在从其中心旋转,所以这就是它的原点
那么我怎样才能获得“新”x。旋转后 4 个点的 y 坐标?
[更新]
alt text http://img25.imageshack.us/img25/8676/shaperotationaltransfor.jpg
我找到了一种在旋转后查找左上角坐标的方法 您从新图像中看到的)通过将旋转中的 OffsetX 和 OffsetY 添加到起始 X 和 Y 坐标,
但我现在无法计算出其余的坐标(其他 3 个)。
(正如 形状,如何计算出剩余 3 个角的 x 和 y 坐标?
[编辑]
第二张图像中的点不是准确且精确的点。 我在脑子里做出了估计。
[更新]解决方案:
首先,我要感谢Jason S 发表了一篇冗长且内容丰富的文章,他在其中描述了整个过程背后的数学原理; 通过阅读你的帖子并尝试这些价值观,我当然学到了很多东西。
但我现在找到了一个代码片段(感谢 EugeneZ 提到的 TransformBounds
) 正是我想要的:
public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
// Might throw an exception if of and from are not in the same visual tree
GeneralTransform transform = of.TransformToVisual(from);
return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}
参考:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/86350f19-6457-470e- bde9-66e8970f7059/
With reference to this programming game I am currently building.
alt text http://img12.imageshack.us/img12/2089/shapetransformationf.jpg
To translate a Canvas in WPF, I am using two Forms: TranslateTransform
(to move it), and RotateTransform
(to rotate it) [children of the same TransformationGroup
]
I can easily get the top left x,y coordinates of a canvas when its not rotated (or rotated at 90deg, since it will be the same), but the problem I am facing is getting the top left (and the other 3 points) coordinates.
This is because when a RotateTransform
is applied, the TranslateTransform
's X
and Y
properties are not changed (and thus still indicate that the top-left of the square is like the dotted-square (from the image)
The Canvas is being rotated from its center, so that is its origin.
So how can I get the "new" x and y coordinates of the 4 points after a rotation?
[UPDATE]
alt text http://img25.imageshack.us/img25/8676/shaperotationaltransfor.jpg
I have found a way to find the top-left coordinates after a rotation (as you can see from the new image) by adding the OffsetX and OffsetY from the rotation to the starting X and Y coordinates.
But I'm now having trouble figuring out the rest of the coordinates (the other 3).
With this rotated shape, how can I figure out the x and y coordinates of the remaining 3 corners?
[EDIT]
The points in the 2nd image ARE NOT ACCURATE AND EXACT POINTS. I made the points up with estimates in my head.
[UPDATE] Solution:
First of all, I would like to thank Jason S for that lengthy and Very informative post in which he describes the mathematics behind the whole process; I certainly learned a lot by reading your post and trying out the values.
But I have now found a code snippet (thanks to EugeneZ's mention of TransformBounds
) that does exactly what I want:
public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
// Might throw an exception if of and from are not in the same visual tree
GeneralTransform transform = of.TransformToVisual(from);
return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}
Reference: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/86350f19-6457-470e-bde9-66e8970f7059/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我正确理解你的问题:
这是相关的方程:
其中
Kc=cos(theta)
和Ks=sin(theta)
和theta
是逆时针旋转的角度。 (验证:如果 theta=0 则坐标保持不变,否则如果 xc=yc=0,则将 (1,0) 映射到 (cos(theta),sin(theta)) 并将 (0,1) 映射到 (- sin(theta), cos(theta)) 。注意:这适用于 (x,y)=(1,1) 位于右上象限的坐标系,而对于位于右下象限的坐标系,theta 将是。顺时针旋转的角度而不是逆时针旋转的角度。)如果您知道与 xy 轴对齐的矩形的坐标,则 xc 将只是两个 x 坐标的平均值,yc 将只是两个 y 坐标的平均值。 (在您的情况下,它是 xc=75,yc=85。)
如果您知道 theta,您现在就有足够的信息来计算新坐标。
如果您不知道 theta,您可以求解 Kc、Ks。 以下是您的示例的相关计算:
这是一个可以求解的线性方程组(练习对于读者:在 MATLAB 中,它是:
在本例中,产生 Kc=1.027, Ks=0.3622,这是没有意义的 (K2 = Kc2 + Ks<对于纯旋转,sup>2 应该等于 1;在本例中,它是 K = 1.089),因此它不是围绕矩形中心的纯旋转,这也不是您的绘图所指示的。要进行检查,请使用毕达哥拉斯定理 d2 = deltax2 + deltay 比较旋转前后距旋转中心的距离。 2(绕xc=75,yc=85旋转,前距离为43.01,后距离为46.84,比率K=1.089;绕原点旋转,前距离为70.71,后距离为73.78 ,比率为 1.043。我相信坐标舍入到整数会产生 1.01 或更小的比率,但这显然大于舍入误差)
因此这里缺少一些信息。 你是如何得到数字(62,40)的?
然而,这就是旋转背后数学的基本要点。
编辑:啊哈,我没有意识到它们是估计值。 (不过,非常接近现实!)
If I understand your question right:
Here's the relevant equations:
where
Kc=cos(theta)
andKs=sin(theta)
andtheta
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:
which is a system of linear equations that can be solved (exercise for the reader: in MATLAB it's:
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!)
我使用这种方法:
其中rotateTransform是我工作并设置角度...等的实例。
I use this method:
where rotateTransform is the instance on which I work and set Angle...etc.
查看 GeneralTransform.TransformBounds() 方法。
Look at GeneralTransform.TransformBounds() method.
我不确定,但这就是您正在寻找的 - 笛卡尔坐标系中点的旋转:
链接
I'm not sure, but is this what you're looking for - rotation of a point in Cartesian coordinate system:
link
您可以在 Point 上使用具有相同变换的 Transform.Transform() 方法来获取应用这些变换的新点。
You can use Transform.Transform() method on your Point with the same transformations to get a new point to which these transformations were applied.