【发布时间】: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