#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# applying transformation matrix to mouse coords.

edited June 2015

Hello there, I've been coding in Processing for about 3 years, and I haven't been stumped like this for a while!

Alright, so what I need help with is to come up with some algorithm to convert from screen coordinates, that is, the coordinates of the mouse cursor, to transformed coordinates. in other words, if I were to click to place a dot on the screen, the raw mouse coords need to be converted such that, after undergoing the transformations the dot shows up at my (screen) mouse coords.

when you're only working with translation and scaling this pretty simple, its the rotation that I'm struggling with.

I was at first trying something like this:

PVector transform( PVector v ){
float x = v.x - transX;
float y = v.y - transY;
x /= scale;
y /= scale;
float d = dist( x, y, transX, transY );
float a = atan2( y - transY, x - transX );
x = transX + d * cos(a - rotation);
y = transY + d * sin(a - rotation);
return new PVector( x, y );
}

but I could never make this work, so I started thinking with matrices, and I got this from this post:

float a = scale * cos(TWO_PI - rotation);
float b = scale * sin(TWO_PI - rotation);
float c = -b;
float d = a;
float e = transXa - transYb + v.x;
float f = transXb + transYa + v.y;

I'm adjusting the angle there because according to wikipedia the rotation is meant to be counterclockwise. ( although I tried it a few different ways ) I wasn't sure about the E and the F due to the potetial ambiguity of the two translations, I tried many different interpretations, all equally wrong.

and using equations I found at a stackoverflow page I arrived at this:

float x = ( (v.x/a) - ((v.y * c) / (da)) + ( (f * c) / (d * a) ) - (e/a) ) / ( 1 - ((b * c)/(d * a)) );
float y = ( (v.y/d) - ((v.x * b) / (a
d)) + ( (e * b) / (a * d) ) - (f/d) ) / ( 1 - ((c * b)/(a * d)) );

where his screen_X is what I'm receiving, my "v.x", and his internal_X is what I want. But no variation of that worked either.

My last attempt was to find the inverse of the matrix

a| c| e
b| d| f
0| 0| 1

to end up with

d/det | -c/det | (cf - ed)/det
-b/det | a/det | (eb - af)/det
0 | 0 | 1

and applying this matrix to the mouse coordinates with

float x = v.x * A + v.y * C + E;
float y = v.x * B + v.y * D + F;

( where the caps are the values of the inverse matrix in the same disposition as the first ) like in the stackoverflow thread, but didn't work at all, in fact these x,y coords seem to be stuck..

Am I way off course here? thanks in advance for any pointers!

Tagged: