-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear-algebra:homogeneous-coordinates.xhtml
359 lines (359 loc) · 23.2 KB
/
linear-algebra:homogeneous-coordinates.xhtml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?xml version="1.0" encoding="utf-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Homogeneous Coordinates</title>
<meta name="author" content="Magnus Achim Deininger" />
<meta name="description" content="All you need to know about homogeneous coordinates - for affine transformations, anyway. From a computer scientist's point of view." />
<meta name="date" content="2012-09-13T17:33:00Z" />
<meta name="mtime" content="2014-12-23T23:26:00Z" />
<meta name="category" content="Linear Algebra" />
<meta name="category" content="Articles" />
<meta name="unix:name" content="linear-algebra:homogeneous-coordinates" />
</head>
<body>
<h1>Introduction</h1>
<p>Since I already went to the trouble of <a href="/linear-algebra:normal-vectors-in-higher-dimensional-spaces">explaining all about normal vectors in higher dimensions</a>, it seems like a good idea to follow up with some of the maths that actually uses those normals. One of the more prominent examples of this is rendering pretty pictures of 4-cubes and similar constructs. But before we have a closer look at how to do these projections, there is one other prerequisite that is typically not explained all that well.</p>
<p>Remember that in geometry we have many different coordinate systems to represent points in a given space. For example, in 2D we commonly deal with cartesian coordinates, that is two numbers that represent the horizontal and vertical offsets from a point of origin. These two coordinates are typically called X and Y - or x<sub>0</sub> and x<sub>1</sub> when working with vectors. In 3D, we extend these coordinates with a third coordinate - Z or x<sub>2</sub> - representing depth. Beyond that we can keep adding x<sub>n</sub>s for each new dimension we introduce.</p>
<p>This works great when dealing with rectangular things - squares, cubes, tesseracts, etc. It doesn't work quite that well with round things, however. When we're primarily interested in circles, then in 2D we can use polar coordinates instead. Polar coordinates represent points on a plane as the distance from a pole and a polar angle. Again we have two coordinates, typically called r and theta. We can easily extend these coordinates to 3D and up as spherical coordinates, that is adding another angle for each new dimension. Geographic coordinates in latitude and longitude would be an actual example of spherical coordinates in 3D (the radial component is implicit in this case).</p>
<p>There are many other coordinate systems that may or may not work well for a given task. But just as we may choose our coordinate system based on the shapes we want to describe, we might also want to choose our coordinate system based on the calculations we want to perform. This is where homogeneous coordinates come in.</p>
<p>Please note that in the following we're implicitly assuming that we're dealing with an n-dimensional vector space over ℝ. The principles should also hold true for any other manifold. Also note that this is strictly from a computer scientist's point of view. It probably won't hold up against a mathematician's scrutiny.</p>
<h1>The Nitty Gritty - in 2D</h1>
<p>Homogeneous coordinates are very similar to typical, cartesian coordinates. The basic idea is that instead of specifying the exact offset from a point of origin, you specify ratios between the individual coordinates. Since the coordinate ratio of all points on a line through the origin is the same, we need to add an extra coordinate that serves as a scale. This extra coordinate allows us to uniquely identify which point on the line we are interested in.</p>
<p>As an example, let's assume we had a point P defined like this in typical cartesian coordinates:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>cartesian</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mn>1</mn></mtd></mtr>
<mtr><mtd><mn>2</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>To convert this point to homogeneous coordinates, we simply add a '1' as the scale.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>homogeneous</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mn>1</mn></mtd></mtr>
<mtr><mtd><mn>2</mn></mtd></mtr>
<mtr><mtd><mn>1</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>Notice that from their definition it follows that multiplying all of the coordinates with a constant other than 0 does not actually change the point that the coordinates refer to when using homogeneous coordinates.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>homogeneous</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi></mtd></mtr>
</mtable></mfenced>
<mo>|</mo>
<mi>c</mi>
<mo>≠</mo>
<mn>0</mn>
</mrow></math>
<p>This also implicitly tells us how to convert homogeneous coordinates back to cartesian coordinates: just divide all components by the last one, then discard that. If we wanted to scale a homogeneous vector like we do when multiplying a vector with cartesian coordinates with a constant, then what we may do instead is to divide the last coordinate by our constant.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi>P</mi><mo>⊗</mo><mi>c</mi>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
</mtable></mfenced>
<mtext>cartesian</mtext>
</munder>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mn>1</mn></mtd></mtr>
</mtable></mfenced>
<mtext>homogeneous</mtext>
</munder>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>d</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>d</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mfrac><mi>d</mi><mi>c</mi></mfrac></mtd></mtr>
</mtable></mfenced>
<mtext>homogeneous</mtext>
</munder>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mfrac><msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub><mi>c</mi></mfrac></mtd></mtr>
</mtable></mfenced>
<mtext>homogeneous</mtext>
</munder>
<mo>|</mo>
<mi>c</mi>
<mo>,</mo>
<mi>d</mi>
<mo>≠</mo>
<mn>0</mn>
</mrow></math>
<p>It seems counterintuitive at first that we need to divide the last coordinate instead of multiplying it, but that follows straight from the scaling behaviour we noticed earlier. To perform the analogous operation of adding two cartesian vectors, we first normalise the second vector to the same scale as the first one, then we add the other coordinates as usual.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi>P</mi><mo>⊕</mo><mi>R</mi>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
</mtable></mfenced>
<mtext>cartesian</mtext>
</munder>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>1</mi></mtd></mtr>
</mtable></mfenced>
<mtext>homogeneous</mtext>
</munder>
<mo>=</mo>
<munder>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd>
<msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>0</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>homogeneous</mtext><mn>0</mn></msub></msub>
<mo>×</mo>
<mfrac>
<msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub>
<msub><mi>R</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub>
</mfrac>
</mtd></mtr>
<mtr><mtd>
<msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>1</mn></msub></msub><mo>+</mo><msub><mi>R</mi><msub><mtext>homogeneous</mtext><mn>1</mn></msub></msub>
<mo>×</mo>
<mfrac>
<msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub>
<msub><mi>R</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub>
</mfrac>
</mtd></mtr>
<mtr><mtd><msub><mi>P</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub></mtd></mtr>
</mtable></mfenced>
<mtext>homogeneous</mtext>
</munder>
<mo>|</mo>
<msub><mi>R</mi><msub><mtext>homogeneous</mtext><mn>2</mn></msub></msub>
<mo>≠</mo>
<mn>0</mn>
</mrow></math>
<p>Seems complicated, right? It's a good thing that's not something you'll typically be doing with homogeneous coordinates as it were, but more on that later. The one thing we should take away from all this is that a larger scale variable means our vector with homogeneous coordinates will be closer to the origin, and smaller scale values mean that the point will be farther from the origin, thus closer to infinity. And this is one thing we can do with homogeneous coordinates that we can't do with most other coordinate systems: we can actually define points that are at an infinite distance from the origin by using a scale coordinate of 0.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>homogeneous, at infinity</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>p</mi><mn>0</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>This makes a lot of sense if you remember that the limit of a division approaches infinity as the divisor approaches zero.</p>
<p>So, as we can see, the common theme in all this is that homogeneous coordinates give us a way of adding an implicit division to our coordinates while still being fairly close to cartesian coordinates in principle.</p>
<h1>3D, 4D and up</h1>
<p>The homogeneous coordinates in 2D are easily extended to, say, 3D by following the same construction: take the cartesian coordinates and add a scale. Like this:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>3D, homogeneous, not at infinity</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>2</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi></mtd></mtr>
</mtable></mfenced>
<mo>|</mo>
<mi>c</mi>
<mo>≠</mo>
<mn>0</mn>
</mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>3D, homogeneous, at infinity</mtext></msub>
<mo>=</mo>
<mfenced><mtable displaystyle="true" equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>p</mi><mn>0</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>2</mn></msub></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>4D homogeneous coordinates and beyond are constructed the same way. A general form would be:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>n-D, homogeneous, not at infinity</mtext></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>0</mn></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mn>1</mn></msub></msub></mtd></mtr>
<mtr><mtd><mtext>...</mtext></mtd></mtr>
<mtr><mtd><mi>c</mi><mo>×</mo><msub><mi>P</mi><msub><mtext>cartesian</mtext><mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></msub></mtd></mtr>
<mtr><mtd><mi>c</mi></mtd></mtr>
</mtable></mfenced>
<mo>|</mo>
<mi>c</mi>
<mo>≠</mo>
<mn>0</mn>
</mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>P</mi><mtext>n-D, homogeneous, at infinity</mtext></msub>
<mo>=</mo>
<mfenced><mtable displaystyle="true" equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>p</mi><mn>0</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><mtext>...</mtext></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>The basic operations work like they do in the 2D case, I trust you'll figure those out. n-D homogeneous coordinates are rather useful when dealing with affine transformations. More on that in just a sec.</p>
<h1>Alternate Notation</h1>
<p>It is fairly common for (cartesian) vectors to be written as a tuple. The longer, matrix-y notation is quite bulky, so we often see a notation much like the following:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mfenced>
<msub><mi>p</mi><mn>0</mn></msub>
<msub><mi>p</mi><mn>1</mn></msub>
<msub><mi>p</mi><mn>2</mn></msub>
</mfenced>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>p</mi><mn>0</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>2</mn></msub></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>For homogeneous coordinates, there is a similar shorthand notation based on the notion that homogeneous coordinates primarily represent ratios between the coordinates:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>p</mi><mn>0</mn></msub>
<mo>:</mo>
<msub><mi>p</mi><mn>1</mn></msub>
<mo>:</mo>
<msub><mi>p</mi><mn>2</mn></msub>
<mo>=</mo>
<mfenced><mtable equalrows="true" columnalign="left">
<mtr><mtd><msub><mi>p</mi><mn>0</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>1</mn></msub></mtd></mtr>
<mtr><mtd><msub><mi>p</mi><mn>2</mn></msub></mtd></mtr>
<mtr><mtd><mn>1</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>I won't be using this shortcut notation here, but you'll probably find it in a lot of books on linear algebra.</p>
<h1>Typical Usage</h1>
<p>As promised earlier, let's have a quick look at why we're dealing with homogeneous coordinates. Remember how the first thing - or at least very close to the first thing - you learn in linear algebra is how to work with matrices. A few lectures into your typical course on that subject and you'll find that you can use matrix multiplications as an elegant way to tranform vectors. This gets slightly better once you realise that when working with anything that transforms vectors you're exceedingly likely to perform the same transformation to many distinct vectors. As matrix multiplications are associative, you can precompute a transformation matrix to use on a set of vectors in one step, then apply that transformation to all of them with a single matrix multiplication per vector. Depending on the kind of linear transformation you're doing this may save quite a bit of computing power, as expensive computations involving trigonometric functions, roots or similar may only need to be computed once when preparing the transformation matrix.</p>
<p>Of course, as a computer scientist you would further realise that while these matrix multiplications may perform a lot of seemingly unnecessary elementary multiplications and additions, which may or may not be expensive, they do have the advantage of being very easy to parallelise as each of the cells of a resulting matrix - or vector - can be computed without having to wait for the result of any other cell. If the size of the matrix is known at compile time, for example if the matrix is a template class in C++ with the size as template arguments, the compiler may be able to emit code that performs most of the calculations in parallel without the need for even more costly branching instructions.</p>
<p>So, back to the maths side of things, matrix multiplications are awesome and they let you perform many a typical transformation on vectors. And this is where homogeneous coordinates start to shine. Ordinarily if you were using cartesian coordinates, a matrix multiplication would only allow you to perform a linear transformation, such as rotating or stretching a vector. A very typical example is the identity matrix.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>I</mi><mtext>3D</mtext></msub>
<mo>=</mo>
<mfenced><mtable>
<mtr><mtd><mn>1</mn></mtd><mtd><mn>0</mn></mtd><mtd><mn>0</mn></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><mn>1</mn></mtd><mtd><mn>0</mn></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><mn>0</mn></mtd><mtd><mn>1</mn></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>When applied to a vector, the result is the same as the input. A somewhat more useful example is a so-called rotation matrix. Such a matrix that rotates a 3D point around the x-axis by an angle of α would look like this:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>R</mi><mtext>3D, x</mtext></msub>
<mo>=</mo>
<mfenced><mtable>
<mtr><mtd><mn>1</mn></mtd><mtd><mn>0</mn></mtd><mtd><mn>0</mn></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><mo>cos</mo><mi>α</mi></mtd><mtd><mo>-sin</mo><mi>α</mi></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><mo>sin</mo><mi>α</mi></mtd><mtd><mo>cos</mo><mi>α</mi></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>We can also scale vectors with a separate scale in each dimension.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msub><mi>S</mi><mtext>3D</mtext></msub>
<mo>=</mo>
<mfenced><mtable>
<mtr><mtd><msub><mi>s</mi><mn>0</mn></msub></mtd><mtd><mn>0</mn></mtd><mtd><mn>0</mn></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><msub><mi>s</mi><mn>1</mn></msub></mtd><mtd><mn>0</mn></mtd></mtr>
<mtr><mtd><mn>0</mn></mtd><mtd><mn>0</mn></mtd><mtd><msub><mi>s</mi><mn>2</mn></msub></mtd></mtr>
</mtable></mfenced>
</mrow></math>
<p>There are many other useful transformations that can be expressed as matrices. More important is that we can chain these transformations by multiplying several transformation matrices into one and then applying the resulting matrix to a vector.</p>
<p>However, one of the operations that cannot be expressed when working with cartesian coordinates is a translation. If, however, we extend the cartesian coordinates to homogeneous coordinates, we are then also able to express affine transformations including translations on our cartesian coordinates. To do that, we first need to extend whatever matrix we may have with a new row and column that is all zeroes except for a one in the bottom right cell.</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mfenced><mtable>
<mtr>
<mtd><msub><mi>m</mi><mrow><mn>0</mn><mo>,</mo><mn>0</mn></mrow></msub></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><msub><mi>m</mi><mrow><mn>0</mn><mo>,</mo><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd>
</mtr>
<mtr>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
</mtr>
<mtr>
<mtd><msub><mi>m</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn><mo>,</mo><mn>0</mn></mrow></msub></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><msub><mi>m</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn><mo>,</mo><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd>
</mtr>
</mtable></mfenced>
<mo>→</mo>
<mfenced><mtable>
<mtr>
<mtd><msub><mi>m</mi><mrow><mn>0</mn><mo>,</mo><mn>0</mn></mrow></msub></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><msub><mi>m</mi><mrow><mn>0</mn><mo>,</mo><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd>
<mtd><mn>0</mn></mtd>
</mtr>
<mtr>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
</mtr>
<mtr>
<mtd><msub><mi>m</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn><mo>,</mo><mn>0</mn></mrow></msub></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><msub><mi>m</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn><mo>,</mo><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd>
<mtd><mn>0</mn></mtd>
</mtr>
<mtr>
<mtd><mn>0</mn></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mn>0</mn></mtd>
<mtd><mn>1</mn></mtd>
</mtr>
</mtable></mfenced>
</mrow></math>
<p>This transforms our n x n matrix for use with n-dimensional cartesian coordinates into an equivalent (n+1) x (n+1) matrix for use with n-dimensional homogeneous coordinates. If we now wanted to translate an input vector by a vector in homogeneous coordinates, we would use the following matrix:</p>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mfenced><mtable>
<mtr>
<mtd><mn>1</mn></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mn>0</mn></mtd>
<mtd><msub><mi>v</mi><mn>0</mn></msub></mtd>
</mtr>
<mtr>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mtext>...</mtext></mtd>
</mtr>
<mtr>
<mtd><mn>0</mn></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mn>1</mn></mtd>
<mtd><msub><mi>v</mi><mrow><mi>n</mi><mo>-</mo><mn>1</mn></mrow></msub></mtd>
</mtr>
<mtr>
<mtd><mn>0</mn></mtd>
<mtd><mtext>...</mtext></mtd>
<mtd><mn>0</mn></mtd>
<mtd><msub><mi>v</mi><mi>n</mi></msub></mtd>
</mtr>
</mtable></mfenced>
</mrow></math>
<p>Note that the upper left part is the standard identity matrix for our n-dimensional space. If you look closely, you'll notice that this is using the same construction as the general matrix expansion, except that in the general case we expanded by the origin vector and this time we're expanding the identity matrix in particular with the vector v.</p>
<p>As we can still compose more complex transformations by multiplying matrices together, this means that this little trick now allows us to express affine transformations as a simple matrix multiplication. All we need to do is to expand our matrices, convert the input vector to homogeneous coordinates, apply the matrix and then convert the result vector back to cartesian coordinates.</p>
<p>Stay tuned for the next article on linear algebra, where I'll try to explain how n-dimensional perspective projections work.</p>
</body>
</html>