Loading...
Logo
Processing Forum
I'm sure this is simple trig but for some reason I just can't get it to work.  The (a,b) offset keeps messing it up.

Copy code
  1. translate(x,y);
  2. rotate(r);
  3. point(a,b);
How can I return the actual location of (a,b)?

This is correct as long as r = 0:

Copy code
  1. return new PVector(x + cos(r) * a, y + sin(r) * b);
but that gets wonky depending on the value of r.

Thanks in advance!

EDIT:

Oddly, this seems to work right:

Copy code
  1. return new PVector(x + cos(r) * b, y + sin(r) * b);
but that seems very strange to me, so I'd still love it if somebody could explain this to me.

Replies(1)

Rotations and Translations are both types of Affine Transformations -- transformations that preserve straight lines and proportionality of lines.  They're actual very cool and there's a variety of other ones besides rotation and translation; you read more about Affine Transformations here: http://en.wikipedia.org/wiki/Affine_transformation

Anyway, to convert from your 'new' coordinates to the original coordinate system, you must use a transformation matrix: http://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations

What you're looking for is the transformation R*T*v = v'

where R is the rotation matrix, T is the translation matrix, and v is your starting point (in your original coordinates).  v' is then the point's location (in original coordinates) after the transformations.  R, T, and v are all shown in the linked wikipedia article. The vector [x' y' 1] is the point in new coordinates.

What you're asking for is, given [x' y' 1], find [x y 1].  In that case you invert the matrices and can find the solution as v = T^-1 * R^-1 * v'.