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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
\section{Coordinate and Canvas Transformations}
\subsection{Overview}
\pgfname\ offers two different ways of scaling, shifting, and rotating
(these operations are generally known as \emph{transformations})
graphics: You can apply \emph{coordinate transformations} to all
coordinates and you can apply \emph{canvas transformations} to the
canvas on which you draw. (The names ``coordinate'' and ``canvas''
transformations are not standard, I introduce them only for the
purposes of this manual.)
The difference is the following:
\begin{itemize}
\item
As the name ``coordinate transformation'' suggests, coordinate
transformations apply only to coordinates. For example, when you
specify a coordinate like |\pgfpoint{1cm}{2cm}| and you wish to
``use'' this coordinate---for example as an argument to a
|\pgfpathmoveto| command---then the coordinate transformation matrix
is applied to the coordinate, resulting in a new
coordinate. Continuing the example, if the current coordinate
transformation is ``scale by a factor of two,'' the coordinate
|\pgfpoint{1cm}{2cm}| actually designates the point
$(2\mathrm{cm},4\mathrm{cm})$.
Note that coordinate transformations apply \emph{only} to
coordinates. They do not apply to, say, line width or shadings or
text.
\item
The effect of a ``canvas transformation'' like ``scale by a factor
of two'' can be imagined as follows: You first draw your picture on
a ``rubber canvas'' normally. Then, once you are done, the whole
canvas is transformed, in this case stretched by a factor of
two. In the resulting image \emph{everything} will be larger: Text,
lines, coordinates, and shadings.
\end{itemize}
In many cases, it is preferable that you use coordinate
transformations and not canvas transformations. When canvas
transformations are used, \pgfname\ looses track of the coordinates of
nodes and shapes. Also, canvas transformations often cause undesirable
effects like changing text size. For these reasons, \pgfname\ makes it
easy to setup the coordinate transformation, but a bit harder to
change the canvas transformation.
\subsection{Coordinate Transformations}
\subsubsection{How PGF Keeps Track of the Coordinate Transformation
Matrix}
\pgfname\ has an internal coordinate transformation matrix. This
matrix is applied to coordinates ``in certain situations.'' This means
that the matrix is not always applied to every coordinate ``no matter
what.'' Rather, \pgfname\ tries to be reasonably smart at when and how
this matrix should be applied. The most prominent examples are the
path construction commands, which apply the coordinate transformation
matrix to their inputs.
The coordinate transformation matrix consists of four numbers $a$,
$b$, $c$, and $d$, and two dimensions $s$ and $t$. When the coordinate
transformation matrix is applied to a coordinate $(x,y)$ the new
coordinate $(ax+by+s,cx+dy+t)$ results. For more details on how
transformation matrices work in general, please see, for example, the
\textsc{pdf} or PostScript reference or a textbook on computer
graphics.
The coordinate transformation matrix is equal to the identity matrix
at the beginning. More precisely, $a=1$, $b=0$, $c=0$, $d=1$,
$s=0\mathrm{pt}$, and $t=0\mathrm{pt}$.
The different coordinate transformation commands will modify the
matrix by concatenating it with another transformation matrix. This
way the effect of applying several transformation commands will
\emph{accumulate}.
The coordinate transformation matrix is local to the current \TeX\
group (unlike the canvas transformation matrix, which is local to the
current |{pgfscope}|). Thus, the effect of adding a coordinate
transformation to the coordinate transformation matrix will last only
till the end of the current \TeX\ group.
\subsubsection{Commands for Relative Coordinate Transformations}
The following commands add a basic coordinate transformation to the
current coordinate transformation matrix. For all commands, the
transformation is applied \emph{in addition} to any previous
coordinate transformations.
\begin{command}{\pgftransformshift\marg{point}}
Shifts coordinates by \meta{point}.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformshift{\pgfpoint{1cm}{1cm}}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformxshift\marg{dimensions}}
Shifts coordinates by \meta{dimension} along the $x$-axis.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformxshift{.5cm}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformyshift\marg{dimensions}}
Like |\pgftransformxshift|, only for the $y$-axis.
\end{command}
\begin{command}{\pgftransformscale\marg{factor}}
Scales coordinates by \meta{factor}.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformscale{.75}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformxscale\marg{factor}}
Scales coordinates by \meta{factor} in the $x$-direction.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformxscale{.75}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformyscale\marg{factor}}
Like |\pgftransformxscale|, only for the $y$-axis.
\end{command}
\begin{command}{\pgftransformxslant\marg{factor}}
Slants coordinates by \meta{factor} in the $x$-direction. Here, a
factor of |1| means $45^\circ$.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformxslant{.5}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformyslant\marg{factor}}
Slants coordinates by \meta{factor} in the $y$-direction.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformyslant{-1}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformrotate\marg{degrees}}
Rotates coordinates counterclockwise by \meta{degrees}.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformrotate{30}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformtriangle\marg{a}\marg{b}\marg{c}}
This command transforms the coordinate system in such a way that the
triangle given by the points \meta{a}, \meta{b} and \meta{c} lies at
the coordinates $(0,0)$, $(1\mathrm{pt},0\mathrm{pt})$ and
$(0\mathrm{pt},1\mathrm{pt})$.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformtriangle
{\pgfpoint{1cm}{0cm}}
{\pgfpoint{0cm}{2cm}}
{\pgfpoint{3cm}{1cm}}
\draw (0,0) -- (1pt,0pt) -- (0pt,1pt) -- cycle;
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformcm\marg{a}\marg{b}\marg{c}\marg{d}\marg{point}}
Applies the transformation matrix given by $a$, $b$, $c$, and $d$
and the shift \meta{point} to coordinates (in addition to any
previous transformations already in force).
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformcm{1}{1}{0}{1}{\pgfpoint{.25cm}{.25cm}}
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformarrow\marg{start}\marg{end}}
Shift coordinates to the end of the line going from \meta{start}
to \meta{end} with the correct rotation.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (3,1);
\pgftransformarrow{\pgfpointorigin}{\pgfpoint{3cm}{1cm}}
\pgftext{tip}
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformlineattime\marg{time}\marg{start}\marg{end}}
Shifts coordinates by a specific point on a line at a specific
time. The point by which the coordinate is shifted is calculated by
calling |\pgfpointlineattime|, see
Section~\ref{section-pointsattime}.
In addition to shifting the coordinate, a rotation \emph{may} also
be applied. Whether this is the case depends on whether the \TeX\ if
|\ifpgfslopedattime| is set to true or not.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1);
\pgftransformlineattime{.25}{\pgfpointorigin}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) -- (2,1);
\pgfslopedattimetrue
\pgftransformlineattime{.25}{\pgfpointorigin}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
If |\ifpgfslopedattime| is true, another \TeX\ |\if| is important:
|\ifpgfallowupsidedowattime|. If this is false, \pgfname\ will
ensure that the rotation is done in such a way that text is never
``upside down.''
There is another \TeX\ if that influences this command. If you set
|\ifpgfresetnontranslationattime| to true, then, between
shifting the coordinate and (possibly) rotating/sloping the
coordinate, the command |\pgftransformresetnontranslations| is
called. See the description of this command for details.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformscale{1.5}
\draw (0,0) -- (2,1);
\pgfslopedattimetrue
\pgfresetnontranslationattimefalse
\pgftransformlineattime{.25}{\pgfpointorigin}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformscale{1.5}
\draw (0,0) -- (2,1);
\pgfslopedattimetrue
\pgfresetnontranslationattimetrue
\pgftransformlineattime{.25}{\pgfpointorigin}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformcurveattime\marg{time}\marg{start}\marg{first
support}\marg{second support}\marg{end}}
Shifts coordinates by a specific point on a curve at a specific
time, see Section~\ref{section-pointsattime} once more.
As for the line-at-time transformation command, |\ifpgfslopedattime|
decides whether an additional rotation should be applied. Again, the
value of |\ifpgfallowupsidedowattime| is also considered.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) .. controls (0,2) and (1,2) .. (2,1);
\pgftransformcurveattime{.25}{\pgfpointorigin}
{\pgfpoint{0cm}{2cm}}{\pgfpoint{1cm}{2cm}}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\draw (0,0) .. controls (0,2) and (1,2) .. (2,1);
\pgfslopedattimetrue
\pgftransformcurveattime{.25}{\pgfpointorigin}
{\pgfpoint{0cm}{2cm}}{\pgfpoint{1cm}{2cm}}{\pgfpoint{2cm}{1cm}}
\pgftext{Hi!}
\end{tikzpicture}
\end{codeexample}
The value of |\ifpgfresetnontranslationsattime| is also taken into account.
\end{command}
{
\let\ifpgfslopedattime=\relax
\begin{textoken}{\ifpgfslopedattime}
Decides whether the ``at time'' transformation commands also
rotate coordinates or not.
\end{textoken}
}
{
\let\ifpgfallowupsidedowattime=\relax
\begin{textoken}{\ifpgfallowupsidedowattime}
Decides whether the ``at time'' transformation commands should
allow the rotation be down in such a way that ``upside-down text''
can result.
\end{textoken}
}
{
\let\ifpgfresetnontranslationsattime=\relax
\begin{textoken}{\ifpgfresetnontranslationsattime}
Decides whether the ``at time'' transformation commands should
reset the non-translations between shifting and rotating.
\end{textoken}
}
\subsubsection{Commands for Absolute Coordinate Transformations}
The coordinate transformation commands introduced up to now are always
applied in addition to any previous transformations. In contrast, the
commands presented in the following can be used to change the
transformation matrix ``absolutely.'' Note that this is, in general,
dangerous and will often produce unexpected effects. You should use
these commands only if you really know what you are doing.
\begin{command}{\pgftransformreset}
Resets the coordinate transformation matrix to the identity
matrix. Thus, once this command is given no transformations are
applied till the end of the scope.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformrotate{30}
\draw (0,0) -- (2,1) -- (1,0);
\pgftransformreset
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransformresetnontranslations}
This command sets the $a$, $b$, $c$, and $d$ part of the coordinate
transformation matrix to $a=1$, $b=0$, $c=0$, and $d=1$. However,
the current shifting of the matrix is not modified.
The effect of this command is that any rotation/scaling/slanting is
undone in the current \TeX\ group, but the origin is not ``moved
back.''
This command is mostly useful directly before a |\pgftext| command
to ensure that the text is not scaled or rotated.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformscale{2}
\pgftransformrotate{30}
\pgftransformxshift{1cm}
{\color{red}\pgftext{rotated}}
\pgftransformresetnontranslations
\pgftext{shifted only}
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgftransforminvert}
Replaces the coordinate transformation matrix by a coordinate
transformation matrix that ``exactly undoes the original
transformation.'' For example, if the original transformation was
``scale by 2 and then shift right by 1cm'' the new one is ``shift
left by 1cm and then scale by $1/2$.''
This command will produce an error if the determinant of
the matrix is too small, that is, if the matrix is near-singular.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgftransformrotate{30}
\draw (0,0) -- (2,1) -- (1,0);
\pgftransforminvert
\draw[red] (0,0) -- (2,1) -- (1,0);
\end{tikzpicture}
\end{codeexample}
\end{command}
\subsubsection{Saving and Restoring the Coordinate Transformation
Matrix}
There are two commands for saving and restoring coordinate
transformation matrices.
\begin{command}{\pgfgettransform\marg{macro}}
This command will (locally) define \meta{macro} to a representation
of the current coordinate transformation matrix. This matrix can
later on be reinstalled using |\pgfsettransform|.
\end{command}
\begin{command}{\pgfsettransform\marg{macro}}
Reinstalls a coordinate transformation matrix that was previously
saved using |\pgfgettransform|.
\end{command}
\subsection{Canvas Transformations}
The canvas transformation matrix is not managed by \pgfname, but by
the output format like \pdf\ or PostScript. All the \pgfname\ does is
to call appropriate low-level |\pgfsys@| commands to change the canvas
transformation matrix.
Unlike coordinate transformations, canvas transformations apply to
``everything,'' including images, text, shadings, line thickness, and
so on. The idea is that a canvas transformation really stretches and
deforms the canvas after the graphic is finished.
Unlike coordinate transformations, canvas transformations are local to
the current |{pgfscope}|, not to the current \TeX\ group. This is due
to the fact that they are managed by the backend driver, not by \TeX\
or \pgfname.
Unlike the coordinate transformation matrix, it is not possible to
``reset'' the canvas transformation matrix. The only way to change it
is to concatenate it with another canvas transformation matrix or to
end the current |{pgfscope}|.
Unlike coordinate transformations, \pgfname\ does not ``keep track''
of canvas transformations. In particular, it will not be able to
correctly save the coordinates of shapes or nodes when a canvas
transformation is used.
\pgfname\ does not offer a whole set of special commands for modifying
the canvas transformation matrix. Instead, different commands allow
you to concatenate the canvas transformation matrix with a coordinate
transformation matrix (and there are numerous commands for specifying
a coordinate transformation, see the previous section).
\begin{command}{\pgflowlevelsynccm}
This command concatenates the canvas transformation matrix with the
current coordinate transformation matrix. Afterward, the coordinate
transformation matrix is reset.
The effect of this command is to ``synchronize'' the coordinate
transformation matrix and the canvas transformation matrix. All
transformations that were previously applied by the coordinate
transformations matrix are now applied by the canvas transformation
matrix.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgfsetlinewidth{1pt}
\pgftransformscale{5}
\draw (0,0) -- (0.4,.2);
\pgftransformxshift{0.2cm}
\pgflowlevelsynccm
\draw[red] (0,0) -- (0.4,.2);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgflowlevel\marg{transformation code}}
This command concatenates the canvas transformation matrix with the
coordinate transformation specified by \meta{transformation code}.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgfsetlinewidth{1pt}
\pgflowlevel{\pgftransformscale{5}}
\draw (0,0) -- (0.4,.2);
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{command}{\pgflowlevelobj\marg{transformation code}\marg{code}}
This command creates a local |{pgfscope}|. Inside this scope,
|\pgflowlevel| is first called with the argument
\meta{transformation code}, then the \meta{code} is inserted.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgfsetlinewidth{1pt}
\pgflowlevelobj{\pgftransformscale{5}} {\draw (0,0) -- (0.4,.2);}
\pgflowlevelobj{\pgftransformxshift{-1cm}}{\draw (0,0) -- (0.4,.2);}
\end{tikzpicture}
\end{codeexample}
\end{command}
\begin{environment}{{pgflowlevelscope}\marg{transformation code}}
This environment first surrounds the \meta{environment contents} by
a |{pgfscope}|. Then it calls |\pgflowlevel| with the argument
\meta{transformation code}.
\begin{codeexample}[]
\begin{tikzpicture}
\draw[help lines] (0,0) grid (3,2);
\pgfsetlinewidth{1pt}
\begin{pgflowlevelscope}{\pgftransformscale{5}}
\draw (0,0) -- (0.4,.2);
\end{pgflowlevelscope}
\begin{pgflowlevelscope}{\pgftransformxshift{-1cm}}
\draw (0,0) -- (0.4,.2);
\end{pgflowlevelscope}
\end{tikzpicture}
\end{codeexample}
\end{environment}
\begin{plainenvironment}{{pgflowlevelscope}\marg{transformation code}}
Plain \TeX\ version of the environment.
\end{plainenvironment}
\begin{contextenvironment}{{pgflowlevelscope}\marg{transformation code}}
Con\TeX t version of the environment.
\end{contextenvironment}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "pgfmanual"
%%% End:
|