calculate screen pos -> pos on rotated wxBitmap Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

calculate screen pos -> pos on rotated wxBitmap

Post by mael15 » Thu Jan 30, 2020 2:45 pm

I draw a rotated wxBitmap. Is there a function to calculate where on the rotated wxBitmap the mousePos is? Something to give the rotation parameters that transforms the position on screen to the position within the rotated bitmap?
I think I have seen something like this a while ago but cannot find it anymore.

PS: I know I can do this by hand, but it would be nice not to have to...

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 361
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: calculate screen pos -> pos on rotated wxBitmap

Post by New Pagodi » Thu Jan 30, 2020 7:39 pm

To rotate an image by angle α about a point P(x0,y0), you
  1. translate by (-x0,-y0)
  2. perform the rotation
  3. translate back by (x0,y0)
So if you have an image that has already been rotated about a point and you want to get the original point, you just undo those operations:
  1. translate by (-x0,-y0) [this undoes step 3 above]
  2. perform the rotation by -α [this undoes step 2 above]
  3. translate back by (x0,y0) [this undoes step 1 above]
In wxWidgets, you can use wxAffineMatrix2D to do the math for you. If you have your wxPoint p over the rotated image (how you get that point is up to you), you can compute the point that corresponds to this in the original image like so:

Code: Select all

wxPoint p = ...;
wxPoint2DDouble p2(p);

wxAffineMatrix2D mat;
mat.Translate(x0, y0);
mat.Rotate(-alpha*M_PI/180.0);
mat.Translate(-x0, -y0);

wxPoint2DDouble p3 = mat.TransformPoint(p2);
p3 will point in the original image.

alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 318
Joined: Tue Oct 18, 2016 2:31 pm

Re: calculate screen pos -> pos on rotated wxBitmap

Post by alys666 » Thu Jan 30, 2020 10:08 pm

p3 will point in the original image.
transformation matrix A transforms coordinates from Space to Space';
when inverted matrix A' transforms coordinates from Space' to Space;
so if you have matrix

Code: Select all

wxAffineMatrix2D mat;
then

Code: Select all

wxAffineMatrix2D matInversed = mat;
matInversed.Inverse();
will be a backward transformation matrix .
ubuntu 16.04, wxWidgets 3.0.4

Post Reply