Advertisement

Zooming about to a point (2D surface)

Started by February 24, 2013 10:20 AM
1 comment, last by Amnesty2 11 years, 11 months ago

Tiger.jpg

Hello, I need some help with this math. I've been at this for hours now.

I have a large surface (the whole tiger) and a view in blue that is 700,400. Right now, i have it zooming and panning with bounds checking fine, but can't figure out how to zoom to the mouse cursor (red dots).

Say, im at 660, 300 local coordinates of view A and zoom in, how do I get what appears in view B? Currently, it just zooms into 0,0 and would get a close up of the ear instead. Basically, I want the functionality of Paint, or Paint.NET or any image editor.

This is all 2D math btw


    // x, and y are local coordinates
    void Canvas::setScale(float s, int x, int y) 
    {
        // m_x and m_y are are coordinates into the full canvas
        // the view treats them as 0,0
        // in view marked A, these would something like 200,100 and would be at the tip of the ear
        // after this function i want them to point right above the eye like in the view B example

        float oldscale = m_scale;
        m_scale = s;

        // TODO add the math 

        CheckBounds();
    }

Right now, i have some math filled in but it only kinda working.. Its more of a hack and is definately not fluid or percise.
I know there most be a standard way of doing this.
Thanks
First figure out the location of the red dot in full-canvas coordinates. I'll call that z:
z_x = m_x + x/s
z_y = m_y + y/s
That assumes that s increases as the zoom gets bigger, so that s = 2 for 2x zoom. Now use z to find the location of the upper-left corner in full-canvas coordinates:
m_x = z_x - 0.5*viewSize_x/s
m_y = z_y - 0.5*viewSize_y/s
Putting those together:
m_x = m_x + x/s - 0.5*viewSize_x/s
m_y = m_y + y/s - 0.5*viewSize_y/s
Of course, if it makes you happy you can make a very slight optimization by factoring out the 1/s. If I'm understanding your code correctly, that should do what you want - though, as a disclaimer, it's way too late in the morning for me to test it out myself.
Advertisement

Works Great! thanks

In addition to your formulas, I averaged the new m_x and m_y values with the old ones so they were not changed so drastically. Seems really smooth now.

This topic is closed to new replies.

Advertisement