私はあまりにも長い間このコードを見つめたり遊んだりしてきました!
currentCrop画像は(x,y,width,height) でトリミングされます。currentCrop画像が存在しなければならない使用可能な寸法に基づいて事前に計算されます。
cropCentre(x,y) は、トリミングの中心点を格納する一時変数です。
desiredPoint(x,y) は表示したい画像内のポイントであるため、トリミングが計算されたら、cropCentre(それによって) ができるだけcurrentCrop近くなるように画像のトリミングをシフトするとよいでしょう。画像の枠外には出ません。desiredPointcurrentCrop
次のコードは機能しますが、while ループを使用しなくても同じことが実現できると確信しています。
if ( cropCentre.x < desiredPoint.x ) {
while( currentCrop.x + currentCrop.width < sourceWidth && cropCentre.x < desiredPoint.x ) {
cropCentre.x = currentCrop.x + currentCrop.width / 2;
currentCrop.x ++;
}
} else if ( cropCentre.x > desiredPoint.x ) {
while( currentCrop.x > 0 && cropCentre.x > desiredPoint.x ) {
cropCentre.x = currentCrop.x + currentCrop.width / 2;
currentCrop.x --;
}
}
// code for vertical omitted - if the problem can be solved for x, then the solution for y is identical...