WPF Canvas.Left / Canvas.Top 旋转时坐标不太正确,为什么?
我有一个奇怪但大的问题。 我试图让用户在画布上移动/旋转一些矩形。我的机制基于操作 Canvas.Left / Canvas.Top 属性进行移动,以及 RenderTransform.Rotate 进行旋转。我只需要旋转 0/90/180/270 角度。
问题在于,放置在 0.0 角旋转的坐标 0.0 处的矩形显示正常,但位于 0.0 处且 90 角旋转的同一矩形显示位于 0.0 以外其他坐标的元素。正如我所看到的,在旋转为 0 或 180 的情况下,坐标总是正确的,但在 90 / 270 的情况下,坐标是错误的。 设置的坐标和用户看到的坐标之间的差异与高度和宽度之间的差异有关。
以前有人遇到过此类问题吗?
谢谢你, Daniell
编辑:
当然,这里有一些 xaml:
<画布 身高=“500” 宽度=“500” 背景=“绿色” <矩形 画布.左=“0” 画布.Top =“0” 身高=“50” 宽度=“100” 渲染变换原点=“0.5,0.5” 填充=“红色” <矩形.RenderTransform> <旋转变换 角度=“90” >> <矩形 画布.左=“0” 画布.Top =“0” 身高=“50” 宽度=“100” 渲染变换原点=“0.5,0.5” 填充=“皇家蓝” <矩形.RenderTransform> <旋转变换 角度=“0” >>
正如您所看到的,蓝色矩形似乎正确放置在 (0,0),但红色矩形显示在不同的坐标处,即使它返回的仍然是 0,0。
我发现公式:
displayPointX = Canvas.Left + height /2 - width / 2
diaplayPointY = Canvas.Top + height /2 - width/2
I have a weird but big problem.
I am trying to let the user move / rotate some Rectangles on a Canvas. I am basing my mechanism on manipulating Canvas.Left / Canvas.Top properties for move, and RenderTransform.Rotate for rotation. I only need rotation on 0/90/180/270 angles.
The problem is that a rectangle, which is placed at coordinates 0.0 on a 0 angle rotation shows ok, but the same rectangle at 0.0 with 90 angle rotation shows the element at other coordinates than 0.0. As I see, the coordinates are always right in the case the rotation is 0 or 180, but wrong in the case of 90 / 270.
The differencee between what coordinates are set and what the user sees, is related to the difference between the Height and Width.
Has anyone run into this sort of problem before?
Thank you,
Daniell
edit:
Sure, here is some xaml:
<Canvas Height="500" Width="500" Background="Green" <Rectangle Canvas.Left="0" Canvas.Top="0" Height="50" Width="100" RenderTransformOrigin="0.5,0.5" Fill="Red" <Rectangle.RenderTransform> <RotateTransform Angle="90" /> </Rectangle.RenderTransform> </Rectangle> <Rectangle Canvas.Left="0" Canvas.Top="0" Height="50" Width="100" RenderTransformOrigin="0.5,0.5" Fill="RoyalBlue" <Rectangle.RenderTransform> <RotateTransform Angle="0" /> </Rectangle.RenderTransform> </Rectangle> </Canvas> </Grid>
as you can see, the blue rectangle seems correctly placed (0,0) but the red one shows at different coordinates, even if it returns stil 0,0.
I have found that the Formula:
displayPointX = Canvas.Left + height /2 - width / 2
diaplayPointY = Canvas.Top + height /2 - width/2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
丹尼尔,每个人告诉你的都是正确的。如果你有一个矩形,并将原点设置为 0.5, 0.5,这意味着“围绕该对象的质心旋转”,所以如果你旋转 90 或 270 度,当然它看起来会和你看到的一模一样。
如果您希望矩形旋转 90 度并仍然位于左上角,则必须旋转 90 度,然后平移 X 和 Y 以使角与 0,0 匹配,或者需要旋转约一个不同的中心(如 0,0),并且仅在 X 中翻译。
这是一个例子:
也许如果你解释一下你真正想用这个做什么会是最好的。这也很奇怪,你说 0 和 180 很好,因为角与 0,0 匹配,但随后你的形状被旋转,我们不知道保留方向对你是否重要。
Daniell, what everyone is telling you is correct. If you have a Rectangle, and you set the origin to 0.5, 0.5, that means "rotate about the centroid of this object", so if you rotate 90 or 270 degrees, of course it'll look exactly like what you see.
If you want the rectangle to rotate 90 degrees and still end up in the upper left corner, you either have to rotate 90, then translate in X and Y to get the corner to match up with 0,0, or you need to rotate about a different center (like 0,0), and translate only in X.
Here's an example:
Perhaps it would be best if you explained what you were really trying to do with this. It's also weird that you say that 0 and 180 are great because the corner matches up with 0,0, but then your shape is rotated, and we don't know if preservation of orientation is important to you or not.
您描述的行为在以下条件下发生:
您看到它的原因是因为矩形旋转了 180 度围绕其中心的度数具有完全相同的边界,但旋转 90 或 270 度的矩形则不然。想象一个宽而细的旋转矩形,您可以很容易地看到事实如此。
当您旋转 90 或 270 度时,您的角不再与容器匹配。
有许多解决方案可供使用:
The behavior you describe occurs under the following conditions:
The reason you are seeing it is because a rectangle rotated 180 degrees around its center has exactly the same bounds, but a rectangle rotated 90 or 270 degrees does not. Picture a wide but skinny rectangle rotating and you can easily see this is so.
When you rotate 90 or 270 degrees your corners no longer match up with the container.
Many solutions are available:
坐标发生变化的原因是画布的坐标系随画布旋转。
http://msdn.microsoft.com/en- us/library/system.windows.media.rotatetransform.aspx 指出:
假设你绕着一个正方形的质心旋转,当你旋转90度时(顺时针,RotateTransform认为方向为正),(0,0)指的是屏幕的右上角,180指的是下角右角,对于 270,它指的是左下角,而对于 0,如预期的那样,它指的是左上角。
The reason the coordinates are changing is because the coordinate system of a canvas rotates with the canvas.
http://msdn.microsoft.com/en-us/library/system.windows.media.rotatetransform.aspx states:
Presuming you are rotating about the centroid of a square, when you're rotated 90 degrees (clockwise, the direction RotateTransform considers positive), (0,0) refers to the upper right corner on the screen, for 180 it refers to the lower right corner, for 270 it refers to the bottom left corner, and at 0 it refers, as expected, to the upper left corner.