# 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.

## 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;

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

##### 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 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.

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009849
• ### Who's Online (See full list)

There are no registered users currently online

×