I am working with programming and 2d geometry and need to transform between two different coordinate systems.
I have two different representations of the same world, where I can sample any point and get its (x,y) coordinate in each frame. Based on this, I need to write a function that calculates this transform for me, given any input coordinate in one of the reference frames.
I have already solved the problem, but I would like to understand it better. Searching the web, I found and based my solution on this
https://msdn.microsoft.com/en-us/library/jj635757(v=vs.85).aspx
However, I'm missing a good explanation why the above works.
(Edit based on comment: I mean mathematically. I understand the matrix operations, but why is it valid in the first place?)
To summarize, the above link says:
Assuming a typical screen coordinate system (...) , affine transformations are expressed algebraically as follows:
$x' = ax + by + c $
$y' = bx - ay + d $
Two x,y points and their corresponding (x', y') are needed to solve this.
In matrix notation, this linear system can be written as:
$$\begin{bmatrix} x'_1 \\ y'_1 \\ x'_2\\ y'_2 \end{bmatrix} = \begin{bmatrix} x_1 & y_1 & 1 & 0 \\ -y_1 & x_1 & 0 & 1 \\ x_2 & y_2 & 1 & 0\\ -y_2 &x_2 &0 &1\end{bmatrix} \implies \textbf{u=Mv} $$ To determine a through d, we invert M and solve as follows
$$\textbf{u=Mv}\implies \textbf{v=M}^{-1}\textbf{u}$$
The v vector here is our [a,b,c,d] values , and can now be used to transform a new (x,y) coordinate into (x', y') according to the equations above.
There are several parts to a complete answer to your question, so I'll approach them one at a time.
Imagine that a point is "fixed in the world" in the same fashion as a building is fixed with respect to the ground. Now, you could have different coordinate systems to describe the location of that building (e.g., street numbers, postal addresses, GPS, lat/long, and more) but the building itself never moves.
So consider two different coordinate systems, connected by the equations: (I'll limit myself to 2 dimensions but this is all valid for more dimensions as well, with some modifications)
$$ \begin{array}{rcl} x' &=& x + c \\ y' &=& y + d \end{array} $$
Now imagine that the origin of the non-primed coordinate system is where the building (or point) in question is located, i.e., $x = y = 0$. Then the position of the building or point, in the primed coordinate system, is $(x',y') = (c,d)$. What that means is that the two coordinate systems are translated with respect to one another, by the vector $(c,d)$. In other words, to get from the origin of one coordinate system to the origin of the other, you need to move right (or left) by $c$ units and up (or down) by $d$ units. Of course, in general, your building or point isn't at the origin of either coordinate system (as in the figure below) but that's fine. I used that just as a simplifying step in the argument.
Next, instead of having the two coordinate systems translated with respect to one another, you could have them share the same origin but be rotated with respect to one another, as in the figure below:
The figure gets a bit messy if I label all the coordinates so I didn't but I think you get the idea from the color-coding of the previous picture. In any case, now, the equations connecting the two coordinate systems are
$$ \begin{array}{rcl} x' &=& \cos(\phi)\,x + \sin(\phi)\,y \\ y' &=& \cos(\phi)\,y - \sin(\phi)\,x \end{array} $$
where $\phi$ (the Greek letter 'phi') is the angle between the $x$-axis and the $x'$-axis. Note that we could also write these as
$$ \begin{array}{rcl} x' &=& {} + a\,x + b\,y \\ y' &=& {} - b\,x + a\,y \end{array} $$
provided that $a = \cos(\phi)$ and $b = \sin(\phi)$. Also, because $a$ and $b$ must be set like so, it follows that $a^2 + b^2 = 1$ (because $\sin^2\phi + \cos^2\phi = 1$ for any angle $\phi$). So, any two real numbers $a$ and $b$ such that $a^2 + b^2 = 1$ can be used to define a pair of coordinate systems that are rotated with respect to one another, while sharing the same origin.
A third possibility is having the two coordinate systems share the same origin but have the transformation equations:
$$ \begin{array}{rcl} x' &=& u\,x \\ y' &=& v\,y \end{array} $$
where $u$ and $v$ are real numbers. What these represent is a scaling (stretching or shrinking) of the coordinate axes. Note that if $u$ and $v$ have the same values, that scaling is the same for both axes. An example of this would be having one coordinate system measure distances in meters and the other measure distances in kilometers, in which case the common scaling factor would be 1000.
So, finally, what if you combined all of these, that is, what if you had
$$ (*)\quad \begin{array}{rcl} x' &=& u\,( a\,x + b\,y) + c \\ y' &=& v\,(-b\,x + a\,y) + d \end{array} $$
That would represent two coordinate systems that are translated, rotated, and scaled with respect to one another. Then, in the case of equal scaling for both axes (say, $u$), you'd have
$$ \begin{array}{rcl} x' &=& u\,( a\,x + b\,y) + c \\ y' &=& u\,(-b\,x + a\,y) + d \end{array} $$
and you could write, instead,
$$ \begin{array}{rcl} x' &=& A\,x + B\,y + c \\ y' &=& -B\,x + A\,y + d \end{array} $$
where $A = ua$ and $B = ub$. This is what you have in your example, i.e., two coordinate systems that are translated, rotated, and equally scaled with respect to one another.
Note: On a second look at what you have, it appears that your $y'$ axis is reversed with respect to the $y$ axis, which corresponds to a choice of $v = -u$ in the $(*)$ expression above, resulting in
$$ \begin{array}{rcl} x' &=& A\,x + B\,y + c \\ y' &=& B\,x - A\,y + d \end{array} $$
I find that an unusual choice of coordinate systems, but that's ok if that's what you want.