如何优雅地调整矩形大小并可选择保持纵横比
How to elegantly resize rectangles with optionally keeping aspect ratio
我想使用计算机鼠标调整二维坐标系中的矩形(从 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 和 elses,但它们还会更多,包括重新定位和纵横比。
我确定一定有一种非常优雅的方法可以完成所有这些,但我的数学太弱了。
有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))
我想使用计算机鼠标调整二维坐标系中的矩形(从 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 和 elses,但它们还会更多,包括重新定位和纵横比。
我确定一定有一种非常优雅的方法可以完成所有这些,但我的数学太弱了。
有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))