2013-08-04 140 views
0

我試圖在Lua(Codea)中構建「縮放以適合」算法。想象一下Canvas上的任何形狀。我想自動放大此形狀的中心,使其佔據畫布的大部分並以其爲中心。最後,我希望能夠縮小到初始狀態,所以矩陣應該能夠完成這項工作。有沒有簡單的方法來做到這一點?任何代碼,即使不在Lua中,也是受歡迎的。縮放以適合算法

回答

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); 
} 
+0

謝謝,我會看到。再次感謝你。埃裏克 – Lostania