I am a 2D novice so please forgive this question for being so simple, but at the same time no one seems to be able to give me an answer that works either so maybe it isn’t so simple?
All I want to know is given a point on an image that you pan and zoom, what would that point be on the image had it not been panned and zoomed?
I have code that zooms and pans that looks like this. All I want to know is what needs to be done to get the new point (mState.touchX) projected back to what it would be in the original image.
Someone mentioned a formula that I tried to use (see the Log statements for details on it) but that didn’t do it so I am asking here in the hope that someone knows how to do this.
private void calculateAspectQuotient()
{
if (mBitmap != null)
{
mAspectQuotient = (((float)mBitmap.getWidth()) / mBitmap.getHeight()) / (((float)getWidth()) / getHeight());
}
}
// Superclass overrides
@Override
protected void onDraw(Canvas canvas)
{
if (mBitmap != null && mState != null)
{
final int viewWidth = getWidth();
final int viewHeight = getHeight();
final int bitmapWidth = mBitmap.getWidth();
final int bitmapHeight = mBitmap.getHeight();
final float panX = mState.getPanX();
final float panY = mState.getPanY();
final float zoomX = mState.getZoomX(mAspectQuotient) * viewWidth / bitmapWidth;
final float zoomY = mState.getZoomY(mAspectQuotient) * viewHeight / bitmapHeight;
// Setup source and destination rectangles
mRectSrc.left = (int)(panX * bitmapWidth - viewWidth / (zoomX * 2));
mRectSrc.top = (int)(panY * bitmapHeight - viewHeight / (zoomY * 2));
mRectSrc.right = (int)(mRectSrc.left + viewWidth / zoomX);
mRectSrc.bottom = (int)(mRectSrc.top + viewHeight / zoomY);
mRectDst.left = getLeft();
mRectDst.top = getTop();
mRectDst.right = getRight();
mRectDst.bottom = getBottom();
// Adjust source rectangle so that it fits within the source image.
if (mRectSrc.left < 0)
{
mRectDst.left += -mRectSrc.left * zoomX;
mRectSrc.left = 0;
}
if (mRectSrc.right > bitmapWidth)
{
mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX;
mRectSrc.right = bitmapWidth;
}
if (mRectSrc.top < 0)
{
mRectDst.top += -mRectSrc.top * zoomY;
mRectSrc.top = 0;
}
if (mRectSrc.bottom > bitmapHeight)
{
mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY;
mRectSrc.bottom = bitmapHeight;
}
if(DebugOptions.DEBUG_ENABLED)
{
Log.d(TAG, "View width: " + viewWidth + " View height: " + viewHeight);
Log.d(TAG, "Bmap width: " + bitmapWidth + " Bmap height: " + bitmapHeight);
Log.d(TAG, "panX: " + panX + " panY: " + panY);
Log.d(TAG, "mRectSrc.left: " + mRectSrc.left);
Log.d(TAG, "mRectSrc.top: " + mRectSrc.top);
Log.d(TAG, "mRectSrc.right: " + mRectSrc.right);
Log.d(TAG, "mRectSrc.bottom: " + mRectSrc.bottom);
Log.d(TAG, "zoomX: " + zoomX);
Log.d(TAG, "zoomY: " + zoomY);
double dx = 0.5 * viewWidth - panX * bitmapWidth * zoomX;
//This is the formula someone sugested...
Log.d(TAG, "dx = 0.5 * viewWidth - panX * bitmapWidth * zoomX");
Log.d(TAG, "dx = 0.5 * " + viewWidth + " - " + panX + " * " + bitmapWidth + " * " + zoomX + " = " + dx);
Log.d(TAG, "xo = (xt - dx) / zoomX");
double xo = (mState.getTouchX() - dx) / zoomX;
//Note mState.getTouchX() returns the X coordinate where the user touched the screen
Log.d(TAG, "xo = (" + mState.getTouchX() + " - " + dx + ")" + "/" + zoomX + " = " + xo);
//So in short, what SHOULD have been done to translate back to get the correct xo here?
Log.d(TAG, "TouchX: " + mState.getTouchX() + " Translated: " + xo);
}
canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
}
}