计算与平移和缩放正确的光标位置 [英] Calculate correct cursor position with pan and zoom

查看:296
本文介绍了计算与平移和缩放正确的光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个问题现在乱搞了约一个小时。

I've been messing around with this problem for about an hour now.

我有一个支持平移和缩放,平移通过存储的处理的视对于X和Y轴偏移。变焦只是一个浮动会从0.2到14。

I have a viewport which supports panning and zooming, the panning is handled by storing an offset for the X and Y axis. The zoom is just a float going from 0.2 to 14.

我的问题是,我需要能够把东西当用户点击在视口中,但是当我放大和平移鼠标坐标是不正确的。我一直无法弄清楚如何正确计算出鼠标坐标

My problem is that I need to be able to place stuff where the user clicks in the viewport but when I zoom and pan the mouse coordinates are not correct. I haven't been able to figure out how to properly calculate the mouse coordinates.

下面是显示我有什么迄今为止图像:的 http://i.imgur.com/WQSXKJ2.png

Here's an image showing what I have so far: http://i.imgur.com/WQSXKJ2.png

由于你可以看到鼠标原点总是在视域组件的左上角。可以看到锅里X和Y轴偏移以及在图像的左下角的缩放值。我还添加了鼠标的例子相对于左上角的视口。

As you can see the mouse origin is always at the top-left of the viewport component. You can see the pan X and Y offset as well as the zoom value in the bottom-left corner of the image. I've also added an example of the mouse coordinates in relation to the top-left of the viewport.

现在,因为这种形象在它的对象的当前缩放我的平面坐标将被抵消。

Now since in that image it's currently zoomed in the objects I place will be offset.

感谢您的时间!

编辑过的解决方案,适合我的CASE:

void Viewport_MouseClick(object sender, MouseEventArgs e){
    Point mousePosition = new Point((int)((e.X - Pan.X) / Zoom),
                                    (int)((e.Y - Pan.Y) / Zoom));
}

这计算出正确的屏幕空间鼠标的位置,同时采取锅放大考虑。
我用TAWS答案玩弄了该解决方案。谢谢你的帮助! :)

This calculates a correct "screen-space" mouse position whilst taking the pan and zoom into account. I got that solution by playing around with TaWs answer. Thank you for the help! :)

推荐答案

使用了变焦的TrackBar和另外两个为这个似乎工作的偏移量:

Using a TrackBar for the Zoom and two more for the offsets this seems to work:

private void panel1_Paint(object sender, PaintEventArgs e)
{
    using (Bitmap bmp = new Bitmap(filename))
    {
        e.Graphics.ScaleTransform(zoom, zoom);
        e.Graphics.DrawImage(bmp, trb_offsetX.Value, trb_offsetY.Value);
    }
}

float zoom = 1f;

private void panel1_MouseMove(object sender, MouseEventArgs e)
{

  Point mouseLocation = e.Location;
  Point imageLocation = new Point((int)((mouseLocation.X / zoom - trb_offsetX.Value)),
                                  (int)((mouseLocation.Y / zoom - trb_offsetY.Value)));

   st_mousePos.Text = "   "  +  imageLocation.ToString();

}


private void trackBar1_Scroll(object sender, EventArgs e)
{
    zoom = trackBar1.Value;
    panel1.Invalidate();
}



我已经加入了Paint事件的代码,所以你可以看到,如果这是您正在处理它,太的方式。

I have added the code for the Paint event, so you can see if that's the way your are handling it, too.

这篇关于计算与平移和缩放正确的光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆