0
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Are you missing a column vector with $a,b,c,d$ as entries in the line that precedes "To determine $a$ through ..." ? $\endgroup$ Aug 3, 2015 at 23:24
  • $\begingroup$ Also could you clarify if you're trying to find the mathematical idea that motivates this process -- or details as to why the process works using matrix operations? $\endgroup$ Aug 3, 2015 at 23:26
  • $\begingroup$ I mean why it is valid in the first place, not why the matrix operations solve it. Sorry if anything was unclear. I assumed the procedure would be well known, but whoever authored at the Microsoft website left out any source or reference. $\endgroup$
    – user985366
    Aug 3, 2015 at 23:42

2 Answers 2

1
$\begingroup$

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.

enter image description here

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:

enter image description here

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.

$\endgroup$
2
  • $\begingroup$ Excellent explanation! I think the reversed y' axis comes from the fact that the given guide was part of a graphics chapter, where a computer screen is one of the frames of reference. On the computer screen, starting from the top-left (0,0) pixel, the y axis increases downwards, which is the opposite of a standard axis in mathematics. $\endgroup$
    – user985366
    Aug 4, 2015 at 2:41
  • $\begingroup$ Thank you! In regards to the reversal of the $y$ axis, I realised the same thing you just mentioned but I wanted to make the point explicit that there was a reversal, in case you wanted something different but hadn't realised that there was a reversal. $\endgroup$
    – wltrup
    Aug 4, 2015 at 3:03
0
$\begingroup$

For sake of discussion, let's call a coordinate system a point $p=(p_1,p_2)$ along with two vectors $v_1,v_2$ called the coordinate frame (the order of these vectors matters). Let's also define standard coordinates, denoted as $S$, to be based at $(0,0)$ with the coordinate frame $(1,0),(0,1)$.

To avoid confusion from writing points and coordinates in the same way, for now I will write $(c_1,c_2)$ to mean the point in 2D space, but $[c_1,c_1]_C$ to mean a pair of coordinates in coordinate system $C$.

A coordinate system $C$ provides a mapping between a pair of coordinates $[c_1,c_2]_C$ to a point by $c_1v_1 + c_2v_2 + p$. That is, starting from $p$, go $c_1$ in the $v_1$ direction (scaled by the length of $v_1$) and $c_2$ in the $v_2$ direction (scaled by the length of $v_2$). Notice that if you write $v_1$ and $v_2$ as column vectors, you can write it as a matrix plus a vector. (This can be turned into a single matrix multiply without the addition by extending each vector with an extra $1$ and using $p$ as another vector in the frame, basing everything at $(0,0,0)$; I can explain this more upon request). The standard coordinate system gives the mapping $[c_1,c_2]_S=(c_1,c_2)$. This is why it is standard.

I'm not clear on the exact question, and there are a number questions which can be answered about topic. One is, given some points along with their coordinates, can one determine $C$? Another is, given two coordinate systems, can one devise a map converting between them?

For the first, this really is just writing out a system of linear equations and solving for $v_1$, $v_2$, and $p$. This requires three different points, if I've counted the equations and unknowns properly.

For the second, given two coordinate systems, we may construct a map between them by passing through the standard coordinate system and back. Let $C$ be the above system,and $D$ be based at $(q_1,q_2)$ with the coordinate frame $w_1,w_2$. Given coordinates $c_1,c_2$, we wish to find $d_1,d_2$ such that $[d_1,d_2]_D=[c_1,c_2]_C$. The claim is that $d_1,d_2$ will indeed be a function of $c_1,c_2$, and that it can be described by a matrix. The answer comes fairly easily due to linearity (relative the base point). You compute $[1,0]_C$ and $[0,1]_C$ and write them in terms of $D$ (this involves a matrix inverse) since $[c_1,c_2]_C=c_1([1,0]_C - p) + c_2([0,1]_C - p) + p$. From this one may construct the transformation matrix. (Sorry this is rather abridged, but I can expand if this is going in a useful direction.)

The moral is supposed to be that explicitly keeping track of which coordinate system you are in can reduce confusion.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.