如何通过可选地保持宽高比优雅地调整矩形大小

问题描述 投票:0回答:1

我想使用计算机鼠标在二维坐标系(从0,0开始,最大尺寸为1000,1000)中调整矩形的大小。这应该不会很复杂,我已经有了一个简短的解决方案:

伪代码

function setSize(shape, anchor)
    mouseX, mouseY = GetCursorPosition();

    if (anchor == "LEFT") then
        diff = math.abs(mouseX - shape.left);

        if (shape.left > mouseX) then
            shape.width = shape.width + diff
        else
            shape.width = shape.width - diff
        end
    elseif (anchor == "TOPLEFT") then
        diffX = math.abs(mouseX - shape.left);
        diffY = math.abs(mouseY - shape.top);

        if (shape.left > mouseX) then
            shape.width = spahe.width + diffX
        else
            shape.width = shape.width - diffX
        end

        if (shape.top > mouseY) then
            shape.height = shape.height - diffY
        else
            shape.height = shape.height + diffY
        end
    elseif (anchor == "TOP") then
        diffY = math.abs(mouseY - shape.top);

        if (shape.top > mouseY) then
            shape.height = shape.height - diffY
        else
            shape.height = shape.height + diffY
        end
    elseif (anchor == "TOPRIGHT") then
        diffX = math.abs(mouseX - shape.right);
        diffY = math.abs(mouseY - shape.top);

        if (shape.right > mouseX) then
            shape.width = shape.width - diffX
        else
            shape.width = spahe.width + diffX
        end

        if (shape.top > mouseY) then
            shape.height = shape.height - diffY
        else
            shape.height = shape.height + diffY
        end
    elseif (anchor == "RIGHT") then
        diffX = math.abs(mouseX - shape.right);

        if (shape.right > mouseX) then
            shape.width = shape.width - diffX
        else
            shape.width = spahe.width + diffX
        end
    elseif (anchor == "BOTTOMRIGHT") then
        diffX = math.abs(mouseX - shape.right);
        diffY = math.abs(mouseY - shape.bottom);

        if (shape.right > mouseX) then
            shape.width = shape.width - diffX
        else
            shape.width = spahe.width + diffX
        end

        if (shape.bottom > mouseY) then
            shape.height = shape.height + diffY
        else
            shape.height = shape.height - diffY
        end
    elseif (anchor == "BOTTOM") then
        diffY = math.abs(mouseY - shape.bottom);

        if (shape.bottom > mouseY) then
            shape.height = shape.height + diffY
        else
            shape.height = shape.height - diffY
        end
    elseif (anchor == "BOTTOMLEFT") then
        diffX = math.abs(mouseX - shape.left);
        diffY = math.abs(mouseY - shape.bottom);

        if (shape.left > mouseX) then
            shape.width = spahe.width + diffX
        else
            shape.width = shape.width - diffX
        end

        if (shape.bottom > mouseY) then
            shape.height = shape.height + diffY
        else
            shape.height = shape.height - diffY
        end
    end
end

代码中缺少的是矩形的重新定位,以及对可选地保持矩形的纵横比的支持。即使上面的代码中已经存在很多ifs和else,但包括重定位和宽高比在内的更多ifs and else。

我肯定必须有一种非常优雅的方法来完成所有操作,但是我的数学太弱了。

geometry resize 2d rectangles
1个回答
0
投票

startpos(也许是您的锚点)和当前鼠标位置(X,Y)。样本矩形的尺寸为(sw, sh)(例如320x240)。

结果矩形的左上角位置为(rx0, ry0),大小为rw, rh

  nw = X - startpos.x
  nh = Y - startpos.y
  anw = Abs(nw)
  anh = Abs(nh)

  if anw * sh < anh * sw:
      rh = anh
      rw = rh * sw // sh     #integer division if important
      ry0 = Min(Y, startpos.y)
      rx0 = Min(startpos.x, startpos.x + rw * Sign(nw))
  else:
      rw = anw
      rh = rw * sh // sw
      rx0 = Min(X, startpos.x)
      ry0 = Min(startpos.y, startpos.y + rh * Sign(nh))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.