Jump to content
  • Advertisement
Sign in to follow this  

Zooming about to a point (2D surface)

This topic is 2068 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts




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 



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.

Share this post

Link to post
Share on other sites
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. 

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!