title: Calculate correct cursor position with pan and zoom
body: <p>I've been messing around with this problem for about an hour now.</p>
  
  <p>I have a viewport which supports panning and zooming, the panning is handled by storing an offset for the X and Y axis. The zoom is just a float going from 0.2 to 14.</p>
  
  <p>My problem is that I need to be able to place stuff where the user clicks in the viewport but when I zoom and pan the mouse coordinates are not correct. I haven't been able to figure out how to properly calculate the mouse coordinates.</p>
  
  <p>Here's an image showing what I have so far: <a href="http://i.imgur.com/WQSXKJ2.png" rel="nofollow">http://i.imgur.com/WQSXKJ2.png</a></p>
  
  <p>As you can see the mouse origin is always at the top-left of the viewport component. You can see the pan X and Y offset as well as the zoom value in the bottom-left corner of the image. I've also added an example of the mouse coordinates in relation to the top-left of the viewport.</p>
  
  <p>Now since in that image it's currently zoomed in the objects I place will be offset.</p>
  
  <p>Thank you for your time!</p>
  
  <p><strong>EDITED WITH SOLUTION THAT WORKS FOR MY CASE:</strong></p>
  
  <pre><code>void Viewport_MouseClick(object sender, MouseEventArgs e){
      Point mousePosition = new Point((int)((e.X - Pan.X) / Zoom),
                                      (int)((e.Y - Pan.Y) / Zoom));
  }
  </code></pre>
  
  <p>This calculates a correct "screen-space" mouse position whilst taking the pan and zoom into account.
  I got that solution by playing around with TaWs answer. Thank you for the help! :)</p>