WPF Canvas.Left / Canvas.Top 旋转时坐标不太正确,为什么?

发布于 2024-08-21 04:55:21 字数 1385 浏览 4 评论 0原文

我有一个奇怪但大的问题。 我试图让用户在画布上移动/旋转一些矩形。我的机制基于操作 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小墨 2024-08-28 04:55:21

丹尼尔,每个人告诉你的都是正确的。如果你有一个矩形,并将原点设置为 0.5, 0.5,这意味着“围绕该对象的质心旋转”,所以如果你旋转 90 或 270 度,当然它看起来会和你看到的一模一样。

如果您希望矩形旋转 90 度并仍然位于左上角,则必须旋转 90 度,然后平移 X 和 Y 以使角与 0,0 匹配,或者需要旋转约一个不同的中心(如 0,0),并且仅在 X 中翻译。

这是一个例子:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/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>
              <TransformGroup>
                <RotateTransform Angle="90" />
                <TranslateTransform X="-25" Y="25" />
              </TransformGroup>
            </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> 
</Page>

也许如果你解释一下你真正想用这个做什么会是最好的。这也很奇怪,你说 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:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/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>
              <TransformGroup>
                <RotateTransform Angle="90" />
                <TranslateTransform X="-25" Y="25" />
              </TransformGroup>
            </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> 
</Page>

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.

肩上的翅膀 2024-08-28 04:55:21

您描述的行为在以下条件下发生:

  1. 您的画布是非矩形的,
  2. 您的 RenderTransformOrigin 为“0.5,0.5”并且
  3. 您为 RenderTransform 使用 RotateTransform

您看到它的原因是因为矩形旋转了 180 度围绕其中心的度数具有完全相同的边界,但旋转 90 或 270 度的矩形则不然。想象一个宽而细的旋转矩形,您可以很容易地看到事实如此。

当您旋转 90 或 270 度时,您的角不再与容器匹配。

有许多解决方案可供使用:

  • 使容器完全呈正方形
  • 以 90 度和 270 度旋转更改画布的大小(翻转宽度和高度)
  • 将 ScaleTransform 与 RotateTransform 组合,在一个方向上按 w/h 缩放画布,并在任何时候按 h/w 缩放画布旋转角度为 90 或 270 度,因此画布会拉伸以适合新的矩形
  • 将 TranslateTransform 添加到 RotateTransform,每当旋转角度为 90 或 270 度时,它会将坐标系移动 wh 和 hw
  • 计算新的 RenderTransformOrigin 以使角出来你想要的地方

The behavior you describe occurs under the following conditions:

  1. Your canvas is non-rectangular,
  2. You have a RenderTransformOrigin of "0.5,0.5" and
  3. You use a RotateTransform for your RenderTransform

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:

  • Make the container exactly square
  • Change the size of your canvas (flip the width and height) at 90 and 270 rotations
  • Combine a ScaleTransform with your RotateTransform that scales the canvas by w/h in one direction and h/w whenever rotation is at 90 or 270 degrees so the canvas is stretched to fit the new rectangle
  • Add a TranslateTransform to your RotateTransform that shifts the coordinate system by w-h and h-w whenever rotation is at 90 or 270 degrees
  • Calculate a new RenderTransformOrigin to make the corner come out where you want it
孤千羽 2024-08-28 04:55:21

坐标发生变化的原因是画布的坐标系随画布旋转。

http://msdn.microsoft.com/en- us/library/system.windows.media.rotatetransform.aspx 指出:

当您使用 RotateTransform 时,
意识到转变
旋转坐标系一个
关于点 (0,
0)。

假设你绕着一个正方形的质心旋转,当你旋转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:

When you use a RotateTransform,
realize that the transformation
rotates the coordinate system for a
particular object about the point (0,
0).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文