1. translate
Moves the origin of the user coordinate system.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
(empty) |
No results |
| Level | Object |
|---|---|
0 |
|
1.3. Description
With no matrix operand, translate builds a temporary transformation matrix T and concatenates it with the current transformation matrix (CTM). Precisely, translate replaces the CTM by T × CTM.
The effect is to move the origin of the user coordinate system by tx units in the x direction and ty units in the y direction relative to the former user coordinate system. The sizes of the x and y units and the orientation of the axes are unchanged.
If the matrix operand is supplied, translate replaces the value of matrix by T and pushes the modified matrix back on the operand stack. In this case, translate does not affect the CTM.
The translation matrix T has the form:
[1 0 0 1 tx ty]
This transforms coordinates according to:
x' = x + tx y' = y + ty
1.5. Examples
100 200 translate % Move origin to (100, 200)
0 0 moveto % Now at (100, 200) in original coordinates
50 50 lineto % Line to (150, 250) in original coordinates
stroke
% Center a 200x100 rectangle on letter page (612x792)
306 396 translate % Move to center
-100 -50 translate % Offset by half size
0 0 moveto
200 0 lineto
200 100 lineto
0 100 lineto
closepath
stroke
50 50 translate % Origin now at (50, 50)
100 100 translate % Origin now at (150, 150)
% Translations accumulate
1.6. Common Use Cases
1.6.1. Positioning Graphics
/drawBox {
% Draw a 100x100 box at current origin
newpath
0 0 moveto
100 0 lineto
100 100 lineto
0 100 lineto
closepath
stroke
} def
100 100 translate
drawBox % Box at (100, 100)
150 0 translate
drawBox % Box at (250, 100)
1.7. Common Pitfalls
Coordinate System, Not Content - translate moves the coordinate system, not the content. Drawing at (0, 0) after translation draws at the new origin location.
|
100 100 moveto
50 50 lineto
stroke
100 100 translate % Moves coordinate system
100 100 moveto % Actually at (200, 200) in original space
150 150 lineto % Actually at (250, 250) in original space
stroke
50 50 translate
100 100 translate % Origin is now at (150, 150), not (100, 100)
% Better approach:
gsave
100 100 translate
% Draw content
grestore
% Original coordinate system restored
Readable Layout Code - Use translate to establish drawing origins, making code more readable:
|
% Clear intent: position then draw
200 300 translate
/drawLogo load exec
% Less clear: pass coordinates everywhere
200 300 /drawLogo load exec
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Resulting matrix values exceed implementation limits |
[ |
Fewer than 2 operands on stack (first form) or fewer than 3 (second form) |
[ |
Operands are not numbers, or matrix operand is not an array |
1.9. Implementation Notes
-
Translation is one of the fastest transformations
-
The CTM is stored in device space, so very large translation values may lose precision
-
Translations do not affect line width, dash patterns, or other graphics state parameters
-
The transformation matrix is applied during path construction, not during painting
1.10. Matrix Mathematics
The translation matrix for translate by (tx, ty) is:
T = [1 0 0 1 tx ty]
Concatenating with the CTM:
CTM' = T × CTM
= [1 0 0 1 tx ty] × [a b c d e f]
= [a b c d (a×tx + c×ty + e) (b×tx + d×ty + f)]
1.11. Performance Considerations
-
Very lightweight operation
-
No path flattening or other expensive operations
-
Can be used frequently without performance concerns
-
Matrix operations are optimized in most implementations