Converting scalled coordinates of mouse to original.

Hi. I'm currently developping a program and I encountered a problem. In my program there is a map image that can be zoomed and navigated using arrow keys. It works fine. But now I need display some infornation when mouse is over a specified city. I'm trying to do it in the following way:

`int mX = (int)(mouseX/zoom) - trX;
  int mY = (int)(mouseY/zoom) - trY;

  if((mX - pv.x)*(mX - pv.x) + (mY - pv.y)*(mY - pv.y) <= r*r){
    fill(0,0,0);
    textSize(10);
    text(cd.getCity(),pv.x+5, pv.y-5);
  }`

where zoom is the variable that is passed to scale(), cd.getCity() - returns a string with info and trX, trY - parameters for translate function. They a called before this code in the following order:

`translate(trX, trY);
  scale (zoom);`

When there is not zoom and translation - it works fine. But when I use zoom, the program is not showing correect data. Maybe somebody can help me? How can I convert coordinates to original after zoom scale and trasnlation. Thanks.

Answers

  • edited April 2015

    If I were going to approach this problem, I would get out a bunch of sheets of graph paper and draw a bunch of examples of different translations and scales, along with different mouseX and mouseY values and what their corresponding map positions would be. Can you give us a few examples of that?

    Also, it would help if you posted an MCVE of a single function that takes mouseX and mouseY and translates it to the map's position. You can hardcode values to make it easier.

  • Answer ✓

    Kevin, thank you for response. I found a solution for this :)

Sign In or Register to comment.