1. currentpoint
Returns the x and y coordinates of the current point.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| Level | Object |
|---|---|
1 |
|
0 |
|
1.3. Description
currentpoint returns the x and y coordinates of the current point in the graphics state (i.e., the trailing endpoint of the current path).
If the current point is undefined because the current path is empty, currentpoint executes the [nocurrentpoint] error.
The current point is reported in the user coordinate system. Points entered into a path are immediately converted to device coordinates by the current transformation matrix (CTM); existing points are not changed by subsequent modifications to the CTM. currentpoint computes the user space coordinate that corresponds to the current point according to the current value of the CTM. If a current point is set and then the CTM is changed, currentpoint will report a different position in user space than it did before.
1.5. Examples
newpath
100 150 moveto
currentpoint % Returns: 100 150
pop pop
newpath
100 100 moveto
200 200 lineto
currentpoint % Get endpoint
/y exch def
/x exch def
% Can now return to this point
50 50 lineto
x y lineto % Back to (200, 200)
100 100 moveto
currentpoint % 100 100
exch 50 add exch % Add 50 to x
lineto % Draw to (150, 100)
1.6. Common Use Cases
1.6.1. Implementing Relative Operations
/rmoveto_manual {
% dx dy
/dy exch def
/dx exch def
currentpoint
dy add exch
dx add exch
moveto
} def
100 100 moveto
50 30 rmoveto_manual % Moves to (150, 130)
1.6.2. Saving and Restoring Position
/savecurrentpoint {
currentpoint
/savedY exch def
/savedX exch def
} def
/restorecurrentpoint {
savedX savedY moveto
} def
100 100 moveto
savecurrentpoint
200 200 lineto
restorecurrentpoint % Back to (100, 100)
1.6.3. Computing Path Dimensions
/pathLength {
% Approximates total path length
/totalLength 0 def
{
/y1 exch def /x1 exch def
currentpoint
/y0 exch def /x0 exch def
x1 x0 sub dup mul y1 y0 sub dup mul add sqrt
totalLength add /totalLength exch def
x1 y1 moveto
} % line procedure
{} % curve procedure (simplified)
{} % move procedure
{} % close procedure
pathforall
totalLength
} def
1.6.4. Creating Custom Path Operations
/drawArrowhead {
% Draws arrowhead at current point
% Assumes path direction is known
gsave
currentpoint translate % Move origin to current point
% Draw arrowhead at origin
newpath
0 0 moveto
-10 5 lineto
-10 -5 lineto
closepath
fill
grestore
} def
100 100 moveto
200 200 lineto
drawArrowhead
1.7. Common Pitfalls
Requires Current Point - currentpoint fails if the path is empty or only contains moveto operations that were superseded.
|
newpath
currentpoint % Error: nocurrentpoint
| CTM Affects Results - The coordinates returned are in current user space. Changing the CTM changes the reported coordinates. |
100 100 moveto
currentpoint % Returns: 100 100
2 2 scale % Change CTM
currentpoint % Returns: 50 50 (in new user space)
Not Affected by gsave/grestore - The current point is part of the graphics state and is saved/restored by gsave/grestore.
|
100 100 moveto
gsave
200 200 lineto
currentpoint % Returns: 200 200
grestore
currentpoint % Returns: 100 100 (restored)
Use for Position-Dependent Drawing - currentpoint is essential for drawing elements that depend on the current path position, like arrowheads or labels.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Current path is empty (no current point defined) |
[ |
Operand stack is full (cannot push two values) |
[ |
Numeric overflow in coordinate transformation |
1.9. Implementation Notes
-
Returns coordinates in current user space
-
Coordinates are computed from device space using inverse CTM
-
May have rounding errors due to coordinate transformations
-
The current point exists only after path construction operations
-
Superseded
movetooperations don’t leave a current point
1.10. Coordinate Transformation
The current point is stored internally in device space. currentpoint performs this transformation:
device_point → CTM⁻¹ → user_point
This means:
matrix currentmatrix /ctm exch def
100 100 moveto % Set point in user space
currentpoint % Get it back
% Stack: 100 100 (approximately, may have rounding)