当缩放和原点在左上方时,保持图像居中

时间:2014-04-15 00:40:58

标签: math graphics

我缩放并将图像绘制为较小的图像。两个图像的原点是左上角(0,0)。较大的图像是100x100px(但可以是任何大小),较小的图像是28x28px。那部分很容易,28/100 =比例因子

接下来的部分是我希望通过缓慢缩小比例因子来缩小图像尺寸,但我希望图像在28x28px框中保持居中,所以问题是,如何计算x和y偏移以根据缩放系数使对象居中于28x28框中?

function scale(scalefactor as float,destregion as object, sourceregion as object) as object
    sourceregion.setalphaenable(true)
    destregion.setalphaenable(true)
    destregion.setscalemode(1)
    destregion.clear(&h00000000)

    xOffset= ****** getStackOverFlowHelp(???) ******
    yOffset= ****** getSTackOverflowHelp(???) ******

    destregion.drawscaledobject(xOffset,yOffset,scalefactor,scalefactor,sourceregion)
    destregion.finish()
end function

1 个答案:

答案 0 :(得分:1)

令人尴尬的是,我在这里找到了答案: 的 Math to center an image on a screen

虽然背景不同......

工作职能:

function scale(scalefactor as float,destregion as object,sourceregion as object) as object
    sourceregion.setalphaenable(true)
    destregion.setalphaenable(true)
    destregion.setscalemode(1)
    destregion.clear(&h00000000)
    ww=sourceregion.getwidth()*scalefactor
    w=destregion.getwidth()
    hh=sourceregion.getwidth()*scalefactor
    h=destregion.getwidth()
    xOffset=(w/2)-(ww/2)
    yoffset=(h/2)-(hh/2)    
    destregion.drawscaledobject(xOffset,yOffset,scalefactor,scalefactor,sourceregion)
    destregion.finish()
end function