- 1. concat
- 1.1. Syntax
- 1.2. Stack Effects
- 1.3. Description
- 1.4. PostScript Level
- 1.5. Examples
- 1.6. Common Use Cases
- 1.7. Common Pitfalls
- 1.8. Error Conditions
- 1.9. Implementation Notes
- 1.10. Matrix Mathematics
- 1.11. Transformation Components
- 1.12. Decomposition Example
- 1.13. Performance Considerations
- 1.14. Relationship to Other Operators
- 1.15. Building Transformation Matrices
- 1.16. See Also
1. concat
Concatenates a matrix with the current transformation matrix.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
(empty) |
No results |
1.3. Description
concat concatenates matrix with the current transformation matrix (CTM). Precisely, concat replaces the CTM by matrix × CTM.
The effect is to define a new user space whose coordinates are transformed into the former user space according to matrix. This allows arbitrary transformations including combinations of translation, scaling, rotation, and skewing.
The matrix operand must be a 6-element array of numbers representing a transformation matrix in the form:
[a b c d tx ty]
This matrix transforms coordinates according to:
x' = a×x + c×y + tx y' = b×x + d×y + ty
1.5. Examples
% Translation matrix
[1 0 0 1 100 200] concat
% Equivalent to: 100 200 translate
% Scale by 2, rotate 45 degrees
[1.414 1.414 -1.414 1.414 0 0] concat
% Approximately equivalent to: 2 2 scale 45 rotate
% Horizontal shear
[1 0 0.5 1 0 0] concat
newpath
0 0 moveto
100 0 lineto
100 100 lineto
0 100 lineto
closepath
stroke % Draws parallelogram
1.6. Common Use Cases
1.6.1. Building Complex Transformations
% Build transformation step by step
matrix % Start with identity
50 100 translate % Add translation
2 2 scale % Add scaling
45 rotate % Add rotation
concat % Apply to CTM
1.6.2. Applying Precomputed Matrices
% Store transformation for reuse
/myTransform [2 0 0 2 100 100] def
% Use it multiple times
gsave
myTransform concat
% Draw content
grestore
gsave
myTransform concat
% Draw more content
grestore
1.7. Common Pitfalls
| Matrix Format - The matrix must be exactly 6 elements in the correct order [a b c d tx ty]. |
% Wrong: only 4 elements
[2 0 0 2] concat % Error!
% Wrong: wrong order
[100 200 2 0 0 2] concat % Incorrect transformation
% Right:
[2 0 0 2 100 200] concat
| Identity Matrix - Concatenating the identity matrix has no effect. |
[1 0 0 1 0 0] concat % Does nothing
| Order of Operations - Matrix multiplication is not commutative. Order matters! |
% These produce different results:
[2 0 0 2 0 0] concat % Scale first
[1 0 0 1 100 100] concat % Then translate
[1 0 0 1 100 100] concat % Translate first
[2 0 0 2 0 0] concat % Then scale
% Clearer:
100 200 translate
2 2 scale
% Less clear:
[2 0 0 2 100 200] concat
| Build Matrices Separately - Build transformation matrices separately for clarity: |
matrix % Start with identity
100 200 translate % Add translation
45 rotate % Add rotation
/myMatrix exch def % Save result
myMatrix concat % Apply it
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
|
[ |
No operand on stack |
[ |
Operand is not an array, or array elements are not all numbers |
1.9. Implementation Notes
-
The matrix operand is not modified by
concat -
Very large or very small matrix values may cause precision loss
-
Degenerate matrices (determinant = 0) create non-invertible transformations
-
The operation is equivalent to: CTM' = matrix × CTM
1.10. Matrix Mathematics
Given a transformation matrix M:
M = [a b c d tx ty]
And the current CTM:
CTM = [a₀ b₀ c₀ d₀ tx₀ ty₀]
concat computes:
CTM' = M × CTM
= [a b c d tx ty] × [a₀ b₀ c₀ d₀ tx₀ ty₀]
= [a×a₀+b×c₀ a×b₀+b×d₀
c×a₀+d×c₀ c×b₀+d×d₀
tx×a₀+ty×c₀+tx₀ tx×b₀+ty×d₀+ty₀]
1.11. Transformation Components
A general transformation matrix can represent:
| Component | Matrix Elements |
|---|---|
Scaling |
|
Rotation |
When a=d=cos(θ) and b=-c=sin(θ) |
Shearing |
|
Translation |
|
Reflection |
Negative |
1.12. Decomposition Example
% Complex transformation
/complexMatrix [1.414 1.414 -1.414 1.414 100 200] def
% This is approximately equivalent to:
100 200 translate % Translation (tx=100, ty=200)
45 rotate % Rotation (θ=45°)
2 2 scale % Scaling (sx=sy=√2≈1.414)
1.14. Relationship to Other Operators
% These are equivalent:
% Using individual operators:
100 200 translate
2 2 scale
45 rotate
% Using concat:
matrix
100 200 translate
2 2 scale
45 rotate
concat
% Direct concat (requires computed matrix):
[1.414 1.414 -2.828 1.414 100 200] concat
1.15. Building Transformation Matrices
% Create a reusable transformation
/buildTransform {
% angle sx sy tx ty buildTransform -> matrix
matrix
5 -1 roll translate % Apply translation
4 1 roll scale % Apply scaling
rotate % Apply rotation
} def
% Use it:
45 2 3 100 200 buildTransform concat
1.16. See Also
-
translate- Move origin -
scale- Change unit size -
rotate- Rotate axes -
concatmatrix- Multiply two matrices -
setmatrix- Replace CTM directly -
currentmatrix- Get current CTM -
matrix- Create identity matrix -
gsave- Save graphics state -
grestore- Restore graphics state