缩放以适应算法

时间:2013-08-04 23:18:16

标签: algorithm lua zoom zooming

我试图构建一个"缩放以适应" Lua算法(Codea)。想象一下Canvas上的任何形状。我想自动缩放这个形状的中心,使它占据Canvas的大部分区域并以它为中心。最后,我希望能够缩小到最初的情况,因此矩阵应该完成这项工作。有一个简单的方法吗?任何代码,即使不是Lua,都是受欢迎的。

1 个答案:

答案 0 :(得分:1)

在C#中,

double aspectRatio = shape.Width / shape.Height;

if (aspectRatio > 1)
{
    // Width defines the layout
    double origShapeWidth = shape.Width;
    shape.Width = panel.Width;
    shape.Height = panel.Width * shape.Height / origShapeWidth;

    // Center the shape
    double margin = (panel.Height - shape.Height) / 2;
    shape.Margin = new Thickness(0, margin, 0, margin);
}
else
{
    // Height defines the layout
    double origShapeHeight = shape.Height;
    shape.Height = panel.Height;
    shape.Width = panel.Height * shape.Width / origShapeHeight;

    // Center the shape
    double margin = (panel.Width - shape.Width) / 2;
    shape.Margin = new Thickness(margin, 0, margin, 0);
}