放大和缩小并将当前像素保持在鼠标坐标处 [英] Zoom in and out and keep current pixel at mouse coordinates

查看:96
本文介绍了放大和缩小并将当前像素保持在鼠标坐标处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我写的这段代码在Opengl的正交模式下绘制了一个四边形. 我知道它的位置和大小.有时是正方形..有时是矩形. 我想做的是放大和缩小并停留在四边形的相同位置. 我以为可以,但是没有. 知道从鼠标位置到拐角的距离,offset.x以及等于rect_size.x- old_size_w的差,基本数学将为我提供这一点: (offset.x/rect_size.x)*差异.那应该让我根据鼠标的位置来确定该位置需要移动多少. 我希望有人可以解决这个问题. 谢谢!....

So... I have this code I wrote to that draws a quad in ortho mode in Opengl. I know where its located and its size. Sometimes square.. some times its rectangular. What I'm attempting to do is zoom in and out and stay over the same location on the quad. I thought it would work but It's not. Knowing the distance from the mouse location to the corner, offset.x and the difference equaling rect_size.x- old_size_w the basic math would me this: (offset.x/rect_size.x) * difference. That should give me the scale of how much the location needs to move based on where the mouse is sitting. I hope someone can sort this out.. Thank you!....

一些数字...

     location = 100,100
     old_size_w = 1024
     rect_size.x = 1088 (new size old_ * 1.0625)
     mouse_delta.x = 425
     offset = 100 - 425 (-325)
     difference = 1088-1024 (64)
     delta_x = 325/1088 (.2987132....)
     x_offset = Cint(delta_x * difference) (19) (19.11764...)

SO ....我们只能移动19个像素..如果我们从另一个方向进行数学运算.. 2必须=与旧缩放和新缩放之差

SO.... we are only moving by 19 pixels.. If we do the math from the other direction.. the 2 must = the difference from the old zoom and the new zoom

    delta_x = (1088-325) /1088 (.701286...)
    x_offset2 = Cint(delta_x * difference) (45) (44.88235...)
    19 + 45 = 64   <--- this proves out the math

但是...我正在变得令人讨厌的转变,越接近我所移动图像的右边,这种转变就越糟. 也许有人可以找到问题..r_x在下面的代码中保留X,用于证明数学.

Yet... I am getting a nasty shift that gets worse the closer to the right of the image I move. Maybe someone can find the problem.. r_x is remaining X in the code below and is for proving the math.

  Public Sub img_scale_up()
    If Not ready_to_render Then Return
    If Zoom_Factor >= 4.0 Then
        Zoom_Factor = 4.0
        Return 'to big and the t_bmp creation will hammer memory.
    End If
    Dim amt As Single = 0.0625
    Zoom_Factor += amt


    Dim offset As New Point
    'old_w and old_h are the orginal size of the image.

    Dim old_size_w, old_size_h As Double
    old_size_w = (old_w * (Zoom_Factor - amt))
    old_size_h = (old_h * (Zoom_Factor - amt))
    offset = rect_location - (mouse_delta)
    rect_size.X = Zoom_Factor * old_w
    rect_size.Y = Zoom_Factor * old_h
    Dim r_x As Double = ((rect_size.X - -offset.X) / rect_size.X) * (rect_size.X - old_size_w)
    Dim delta_x As Double = CSng(offset.X / rect_size.X)
    Dim delta_y As Double = CSng(offset.Y / rect_size.Y)
    Dim x_offset = delta_x * (rect_size.X - old_size_w)
    Dim y_offset = delta_y * (rect_size.Y - old_size_h)

    rect_location.X += CInt(x_offset)
    rect_location.Y += CInt(y_offset)

    draw_(current_image)

推荐答案

好..我对此进行了整理..在放大和缩小时都使用了几个错误的值. 这是围绕鼠标中心缩放并保持鼠标中心的完整代码. 任何人都想弄清楚这一点..那么这就是完美运行的代码:)

Ok.. I sorted this out.. I used the a couple of the wrong values for both zooming in and out. This is the entire code for zooming around the mouse center and maintaining the center of the mouse. Anyone trying to figure this out.. well here's the code that works perfectly :)

Public Sub img_scale_up()
    If Not ready_to_render Then Return
    If Zoom_Factor >= 4.0 Then
        Zoom_Factor = 4.0
        Return 'to big and the t_bmp creation will hammer memory.
    End If
    Dim amt As Single = 0.0625
    Zoom_Factor += amt

    'this bit of math zooms the texture around the mouses center during the resize.
    'old_w and old_h is the original size of the image in width and height
    'mouse_pos is current mouse position in the window.

    Dim offset As New Point
    Dim old_size_w, old_size_h As Double
    old_size_w = (old_w * (Zoom_Factor - amt))
    old_size_h = (old_h * (Zoom_Factor - amt))

    offset = rect_location - (mouse_pos)

    rect_size.X = Zoom_Factor * old_w
    rect_size.Y = Zoom_Factor * old_h

    Dim delta_x As Double = CDbl(offset.X / old_size_w)
    Dim delta_y As Double = CDbl(offset.Y / old_size_h)

    Dim x_offset = delta_x * (rect_size.X - old_size_w)
    Dim y_offset = delta_y * (rect_size.Y - old_size_h)

    rect_location.X += CInt(x_offset)
    rect_location.Y += CInt(y_offset)

    draw_(current_image)
End Sub

Public Sub img_scale_down()
    If Not ready_to_render Then Return
    If Zoom_Factor <= 0.25 Then
        Zoom_Factor = 0.25
        Return
    End If
    Dim amt As Single = 0.0625
    Zoom_Factor -= amt

    'this bit of math zooms the texture around the mouses center during the resize.
    'old_w and old_h is the original size of the image in width and height
    'mouse_pos is current mouse position in the window.

    Dim offset As New Point
    Dim old_size_w, old_size_h As Double

    old_size_w = (old_w * (Zoom_Factor - amt))
    old_size_h = (old_h * (Zoom_Factor - amt))

    offset = rect_location - (mouse_pos)

    rect_size.X = Zoom_Factor * old_w
    rect_size.Y = Zoom_Factor * old_h

    Dim delta_x As Double = CDbl(offset.X / (rect_size.X + (rect_size.X - old_size_w)))
    Dim delta_y As Double = CDbl(offset.Y / (rect_size.Y + (rect_size.Y - old_size_h)))

    Dim x_offset = delta_x * (rect_size.X - old_size_w)
    Dim y_offset = delta_y * (rect_size.Y - old_size_h)

    rect_location.X += -CInt(x_offset)
    rect_location.Y += -CInt(y_offset)

    draw_(current_image)
End Sub

这篇关于放大和缩小并将当前像素保持在鼠标坐标处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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