缩放以适应算法

我试图在 Lua (Codea) 中构建一个“适合缩放”的算法。想象一下画布上的任何图形。我想自动缩放到该形状的中心,使其占据画布的大部分并居中。最后,我想能够缩小回初始状态,所以矩阵应该可以做到这一点。有没有简单的方法可以做到这一点?欢迎任何代码,即使不是 Lua 的代码也可以。

点赞
用户2179987
用户2179987

在 C# 中,

double aspectRatio = shape.Width / shape.Height;

if (aspectRatio > 1)
{
    // 宽度定义布局
    double origShapeWidth = shape.Width;
    shape.Width = panel.Width;
    shape.Height = panel.Width * shape.Height / origShapeWidth;

    // 居中形状
    double margin = (panel.Height - shape.Height) / 2;
    shape.Margin = new Thickness(0, margin, 0, margin);
}
else
{
    // 高度定义布局
    double origShapeHeight = shape.Height;
    shape.Height = panel.Height;
    shape.Width = panel.Height * shape.Width / origShapeHeight;

    // 居中形状
    double margin = (panel.Width - shape.Width) / 2;
    shape.Margin = new Thickness(margin, 0, margin, 0);
}
2013-08-04 23:30:06