- 1. transform
- 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. Transformation Formula
- 1.11. Coordinate Spaces
- 1.12. Relationship to Path Construction
- 1.13. Performance Considerations
- 1.14. See Also
1. transform
Transforms coordinates from user space to device space.
1.2. Stack Effects
| Level | Object |
|---|---|
1 |
|
0 |
|
| Level | Object |
|---|---|
1 |
|
0 |
|
1.3. Description
With no matrix operand, transform transforms the user space coordinate (x, y) by the current transformation matrix (CTM) to produce the corresponding device space coordinate (x', y').
If the matrix operand is supplied, transform transforms (x, y) by matrix rather than by the CTM.
This operator is useful for converting user space coordinates (the ones used in path construction) to device space coordinates (actual pixel positions).
1.5. Examples
initmatrix
100 200 transform
% → 100.0 200.0 (no transformation)
72 72 scale
1 1 transform
% → 72.0 72.0 (1 inch in each direction)
/m [2 0 0 2 100 100] def
50 50 m transform
% → 200.0 200.0
% (50×2 + 100, 50×2 + 100)
% Where will this point appear on device?
100 100 translate
2 2 scale
50 75 transform
% → 200.0 250.0
% (50×2 + 100, 75×2 + 100)
1.6. Common Use Cases
1.6.1. Converting to Device Coordinates
% Find device position of a user point
/userX 100 def
/userY 200 def
userX userY transform
/deviceY exch def
/deviceX exch def
% Now have device coordinates
1.6.2. Computing Bounding Boxes
% Transform all corners of a rectangle
/transformRect { % x y width height -> x' y' x'' y''
% Transform all four corners
4 dict begin
/h exch def /w exch def /y exch def /x exch def
x y transform
x w add y transform
x y h add transform
x w add y h add transform
end
} def
1.6.3. Hit Testing
% Check if device point hits user space rectangle
/deviceX 250 def
/deviceY 300 def
% Transform to user space
deviceX deviceY itransform
/userY exch def
/userX exch def
% Check if in rectangle
userX 0 ge userX 100 le and
userY 0 ge userY 100 le and
{ (Hit!) } { (Miss) } ifelse print
1.7. Common Pitfalls
Position-Dependent - transform includes translation. Use dtransform for distances.
|
% For point transformation:
100 100 transform % Correct
% For distance/vector transformation:
100 100 dtransform % Correct (ignores translation)
100 100 transform % Wrong (includes translation)
| CTM Changes - The result depends on the current CTM at the time of execution. |
100 100 transform % Result A
2 2 scale
100 100 transform % Result B (different from A)
Use for Debugging - transform helps understand coordinate mappings:
|
% See where user coordinates map to
/showTransform {
gsave
dup exch dup transform
(User: ) print exch ==only ( ) print ==only
( -> Device: ) print exch ==only ( ) print ==
grestore
} def
100 100 showTransform
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Matrix operand does not have exactly 6 elements |
[ |
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
-
Transformation is a simple matrix multiplication
-
Very fast operation
-
No rounding or approximation (exact computation)
-
The inverse operation is
itransform
1.10. Transformation Formula
For CTM = [a b c d tx ty]:
x' = a×x + c×y + tx y' = b×x + d×y + ty
Example with CTM = [2 0 0 2 100 100]:
(50, 50) transforms to: x' = 2×50 + 0×50 + 100 = 200 y' = 0×50 + 2×50 + 100 = 200 → (200, 200)
1.11. Coordinate Spaces
PostScript uses multiple coordinate spaces:
| Space | Description |
|---|---|
User Space |
Coordinate system for path construction (what you specify) |
Device Space |
Actual pixel coordinates on output device |
CTM |
Transforms user space to device space |
% User space point
100 200
% Transform to device space
transform
% Result is device space coordinate
% (actual pixel position)
1.12. Relationship to Path Construction
% Path construction uses implicit transform
100 100 moveto % Internally calls: 100 100 transform
% Explicit transformation
100 100 transform moveto % Wrong! Transforms twice
% Path coordinates are automatically transformed
% by CTM during construction
1.13. Performance Considerations
-
Very fast operation (simple arithmetic)
-
No memory allocation
-
Can be called frequently without concern
-
Inline computation, no function call overhead
1.14. See Also
-
itransform- Inverse coordinate transformation (device to user) -
dtransform- Distance vector transformation -
idtransform- Inverse distance transformation -
currentmatrix- Get current CTM -
concat- Modify CTM