Basic 2D math problem

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

For panning, it’s easy. Take the pixel’s position in the rectangle which draws the image, then subtract the x and y offsets for panning.

For example, if panning x is 25 (moved to the right) and panning y is -10 (moved up), and you click in the rectangle which draws the image on point [2, 85], then you clicked on [ 2 - 25, 85 - (-10) ] or [-23, 95] in the original image.

For zoom, it gets more complicated. I am at work now, but when I get a chance will post my method.

I stopped reading after your problem statement. Let’s work the math:

Calling the original point p and after scaling and translation p<sup>'</sup>, then:

p<sup>'</sup> = s p + t

Where ‘s’ is the scale and ‘t’ is translation. So we want to know where: p = p<sup>'</sup>
p = s p + t p - sp = t p(1-s) = t p = t/(1-s)
That equation will explode if s=1, which is to be expected, since if there is no translation all point satisfy and if there is then none will.

Or am I misreading your problem?

I will try that and let you know.

Thanks!

OK, I tried that but it doesn’t seem like it works.

In short, I did this:


	float t = panX * viewWidth;
            	
       	float p;
            	
        //Do this in case the zoom is very close to 1
       	if(zoomX > 0.95 && zoomX < 1.05)
       	{
       		p = t;
       	}
       	else
       	{
       		p = t / (1 - zoomX);
       	}

I think you are saying that t needs to be in pixels and not in a percentage right? That’s why I calculated it like I did.

Hmm…looks like the thing is flipping the panX and viewWidth values to be what they should be in portrait rather than landscape mode (this is on a phone). That might have been part of the problem. I’ll play around with it some more and post back what I come up with.

Thanks.

Here’s the deal. I’m not sure I’m understanding your problem statement. What I’ve given you is for: After the transform, what ideal point (not pixel) is the in same position as the untransformed. You might really be wanting: What’s this point in the transformed image in terms of the untransformed.

Actually I think I might have it using my own calculation. At least I think it seems to be working. Haven’t tested it thoroughly yet. Code looks something like this:


	private void corelatePoints(Rect phoneRect, Rect imageRect, int xTouch, int yTouch)
	{
		//Shift out the pan
		int xIntermediate = xTouch - phoneRect.left;
		int yIntermediate = yTouch - phoneRect.top;

		//Now correct for the zoom
		float xRatio = (phoneRect.right  - phoneRect.left) / (float)(imageRect.right  - imageRect.left);
		float yRatio = (phoneRect.bottom - phoneRect.top)  / (float)(imageRect.bottom - imageRect.top);

		int xOriginal = (int)(xIntermediate / xRatio);
		int yOriginal = (int)(yIntermediate / yRatio);
		
		Point pImage = new Point();
		pImage.x = xOriginal;
		pImage.y = yOriginal;
		
		Point pPhone = new Point();
		pPhone.x = (int)mState.getTouchX();
		pPhone.y = (int)mState.getTouchY();
		
		
		callback.coordinateTouched(pPhone, pImage);
	}