Geometry 如何在保持纵横比的情况下优雅地调整矩形的大小

Geometry 如何在保持纵横比的情况下优雅地调整矩形的大小,geometry,resize,2d,rectangles,Geometry,Resize,2d,Rectangles,我想用电脑鼠标在二维坐标系中调整矩形的大小(从0,0开始,最大尺寸为10001000)。 这应该不是很复杂,我已经有了一个简单的解决方案: 伪代码 function setSize(shape, anchor) mouseX, mouseY = GetCursorPosition(); if (anchor == "LEFT") then diff = math.abs(mouseX - shape.left); if (shape.left

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

伪代码

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,但它们会有更多,包括重新定位和纵横比


我相信一定有一种非常优雅的方法可以做到这一切,但我的数学太弱了。

startpos
(可能是你的锚)和当前鼠标位置
(X,Y)
。示例矩形具有尺寸
(西南、东南)
(例如,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))
nw=X-startpos.X
nh=Y-起始时间
anw=绝对值(nw)
anh=绝对值(nh)
如果anw*sh

你好。谢谢你的答复。
/
(“双斜杠”)是一个特殊的运算符还是简单的除法?我在类似Python的伪代码中使用它作为整数除法(也许您不需要关心它的int/float)