2
\$\begingroup\$

For Android, let's say you have a image of a map that you draw inside your view. On this map there are many, many "hot spot" locations where you want your user to be able to select. Given that there are so many, you want to give the user the ability to zoom the image, and since it can be zoomed, they need the ability to pan the image too.

Once the user pans and zooms though, the coordinates that originally related to the hot spots have changed since the location the user touches will be based on the view, not the image.

How do you translate the new coordinates back to what they would have been originally before the panning and zooming were done?

Here is the question asked on JavaRanch which may make this more clear:
http://www.coderanch.com/t/567402/java/java/translation#2577725

THANKS!


You're trying to do a coordinate transform, yes?

No, not in the sense that I am doing anything like what I THINK you mean.

What you need to do is apply the transform from the map space to view space in >reverse, or rather undo said transformation.

Hmmm...there is no "map" per se, just the Android screen (the view port I guess you could say) and the image drawn onto it which CAN become larger than the screen.

First store the location and magnification of your viewing area or camera somewhere.

Well, there is no "camera" as in what I think you mean. This isn't 3D, just 2D. The original magnification is 1. Not sure what I would need to store in this case.

Assuming you store the magnification as something like real units/view units or area >of map/area of view. You can then convert from view in magnified units to real units >by doing screen location*magnification. Divide if your ratio is flipped.

Real units? I'm dealing with pixels on a phone.

Now that you have the location relative to the screen in real units you simply do >camera location + relative location which gives you your map location. This does >assume that camera position is in the map coordinate system.

Again, there is no camera position. This is just a phone with an image on it. No 3D. Just a human looking at an image on a phone.

For camera to world transform: (location * magnification) + camera position

So given that there is no camera, what would be used here instead?

For world to camera transform: (location - camera position)/magnification

Again, I am not sure that this is applicable here as there is no camera to have a position.

remember the */ signs flip for view/real rather than real/view

For something more elegant look into how graphics systems do coordinate transforms. >For a 2D system without rotation I think the above is sufficient though.

I really appreciate the effort but I do not see this answer as answering the question. If you look at the code I linked to, I think the question will become much more clearly defined.

It's just an image on a phone that the user can pan and zoom. I keep a pan and a zoom multiplier around and know the touch point on the screen. No camera, no world coordinate system, nothing like that at all. Just an image on a phone that can be manipulated.

\$\endgroup\$
10
  • 2
    \$\begingroup\$ Shiam's answer is entirely correct, based both your original question and the code you have posted. You are interpreting a number of (standard!) terms narrowly or incorrectly. A co-ordinate transform is anything which maps points from one space to another. Panning and zooming are specific cases of this. You do in fact need to invert these co-ordinate transforms. The 'camera' or 'eyepoint' is an abstract concept for a viewpoint. In the given formulae, 'camera position' is just pan. \$\endgroup\$
    – ipeet
    Feb 16, 2012 at 13:41
  • \$\begingroup\$ No it isn't and I can prove that. Pick a point in the dead center of the image. Now zoom the image. No panning, just a zoom. The zoom has then changed from 1 to 5 say. The X and Y of the point has not since they were in the dead center. There is no shift on them at all. The formula given would have you believe that you multiply or divide by the zoom. That just won't work in this case UNLESS you all calculate zoom a different way than the app I gave as an example. \$\endgroup\$
    – Darrin
    Feb 16, 2012 at 15:44
  • 1
    \$\begingroup\$ That is not a disproof. Points are still being mapped to points. Even if it's trivial, the identity transform (which doesn't change any co-ordinate values) is still a co-ordinate transform. You clearly do not understand basic geometry. You are presenting a textbook linear algebra problem, and you are rejecting textbook answers. I find myself wondering: Since you seem to believe that you understand this better than anyone else trying to help you, why are you bothering to ask the question at all? \$\endgroup\$
    – ipeet
    Feb 16, 2012 at 16:28
  • \$\begingroup\$ If I were an expert I wouldn't be asking. Also, I don't understand your taking offense to me pointing out a case where this doesn't work and attacking me personally. Why don't you enlighten me then and point out how this formula does work in this case. :) \$\endgroup\$
    – Darrin
    Feb 16, 2012 at 16:35
  • 1
    \$\begingroup\$ In the horizontal direction, you are applying the following transform: xt = (xo*Sx)+dx ; where xo is the initial x co-ordinate of the point (i.e pixel in image), xt is the transformed (i.e. pixel in display) x co-ordinate, Sx is the horizontal scaling (zoom), and dx is the horizontal translation (pan). Going from a display co-ordinate to image co-ordinate is a matter of solving for xo: xo = (xt - dx) / Sx. Swap 'y' for 'x' and you get the solution for vertical co-ordinates. \$\endgroup\$
    – ipeet
    Feb 16, 2012 at 16:47

1 Answer 1

4
\$\begingroup\$

You're trying to do a coordinate transform, yes? What you need to do is apply the transform from the map space to view space in reverse, or rather undo said transformation.

First store the location and magnification of your viewing area or camera somewhere. Assuming you store the magnification as something like real units/view units or area of map/area of view. You can then convert from view in magnified units to real units by doing screen location*magnification. Divide if your ratio is flipped.

Now that you have the location relative to the screen in real units you simply do camera location + relative location which gives you your map location. This does assume that camera position is in the map coordinate system.

For camera to world transform: (location * magnification) + camera position

For world to camera transform: (location - camera position)/magnification

remember the */ signs flip for view/real rather than real/view

For something more elegant look into how graphics systems do coordinate transforms. For a 2D system without rotation I think the above is sufficient though.

\$\endgroup\$
1
  • \$\begingroup\$ Could be done with matrix calculations too, but as it is 2D, storing just mag and loc may be enough. \$\endgroup\$ Feb 15, 2012 at 23:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .