1. closepath
Closes the current subpath by adding a straight line back to its starting point.
1.2. Stack Effects
| Level | Object |
|---|---|
(empty) |
No operands required |
| Level | Object |
|---|---|
(empty) |
No results |
1.3. Description
closepath closes the current subpath by appending a straight line segment connecting the current point to the subpath’s starting point—generally, the point most recently specified by moveto.
If the current subpath is already closed or the current path is empty, closepath does nothing.
1.5. Examples
newpath
100 100 moveto
200 100 lineto
150 200 lineto
closepath % Adds line from (150,200) to (100,100)
stroke
newpath
% First square
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath
% Second square (new subpath)
250 100 moveto
350 100 lineto
350 200 lineto
250 200 lineto
closepath
fill
newpath
200 200 50 0 360 arc
closepath % Not strictly necessary for arcs
fill
1.6. Common Use Cases
1.6.1. Creating Closed Shapes
/drawPolygon {
% array of points -> draws closed polygon
/points exch def
newpath
points 0 get points 1 get moveto
2 2 points length 1 sub {
/i exch def
points i get points i 1 add get lineto
} for
closepath
} def
[100 100 200 150 250 100 200 50] drawPolygon
fill
1.6.2. Ensuring Proper Fills
% Without closepath - gap at join
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
% No closepath
5 setlinewidth
stroke % Gap visible at corner
% With closepath - clean join
newpath
250 100 moveto
350 100 lineto
350 200 lineto
250 200 lineto
closepath
stroke % Clean miter join at corner
1.7. Common Pitfalls
Line Cap at Close Point - The line cap setting doesn’t affect the join where closepath connects. The line join setting applies instead.
|
2 setlinecap % Round caps
0 setlinejoin % Miter joins
newpath
100 100 moveto
200 100 lineto
closepath
stroke % Join at (100,100) is mitered, not rounded
Not Needed for All Shapes - Complete arcs (0 to 360) are automatically closed. Adding closepath doesn’t hurt, but it’s redundant.
|
newpath
200 200 50 0 360 arc % Already creates closed path
closepath % Redundant but harmless
Always Close Filled Paths - For clean fills and strokes, always use closepath to close shapes, even if the endpoints are very close.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
Path becomes too complex for implementation |
Note: closepath does not require a current point and will not generate a [nocurrentpoint] error.
1.9. Implementation Notes
-
Creates an explicit line segment in the path
-
The closing segment participates in line join calculations
-
Current point after
closepathis the subpath start point -
Subsequent path operations start a new subpath
-
Has no effect on empty paths or already-closed subpaths
-
Essential for proper fill and stroke behavior
1.10. Behavior Details
The behavior differs from simply using lineto back to the starting point:
% Using lineto
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
100 100 lineto % Back to start
% Next lineto extends from (100,100)
% Using closepath
newpath
100 100 moveto
200 100 lineto
200 200 lineto
100 200 lineto
closepath % Back to start, subpath closed
% Next lineto starts NEW subpath
1.11. Performance Considerations
-
Very lightweight operation
-
No computational overhead
-
Improves rendering quality for stroked paths
-
Required for correct fill behavior in most cases
1.12. See Also
-
newpath- Initialize empty path -
moveto- Start new subpath -
lineto- Add straight line segment -
arc- Add circular arc -
currentpoint- Get current point