【问题标题】:How to elegantly resize rectangles with optionally keeping aspect ratio如何优雅地调整矩形大小并可选择保持纵横比
【发布时间】:2020-02-27 17:33:50
【问题描述】:

我想使用计算机鼠标在二维坐标系中调整矩形的大小(从 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

代码中缺少的是矩形的重新定位以及对可选地保持矩形纵横比的支持。尽管上面的代码中已经有很多 if 和 else,但它们的数量会更多,包括重新定位和纵横比。

我确信一定有一种非常优雅的方式来做这一切,但我的数学太弱了。

【问题讨论】:

    标签: geometry resize 2d rectangles


    【解决方案1】:

    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))
    

    【讨论】:

    • 嗨。谢谢回复。 //(“双斜线”)是特殊运算符还是简单除法?
    • 我在类似 Python 的伪代码中将它用作整数除法(也许你不需要关心 int/floats)
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 2019-08-10
    • 2014-01-08
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多