<h2>Math</h2>
<p>In math, there is a way how a path is to be followed, and that is usually counterclockwise:</p>
<p>Assuming a perimeter path of a circle with <span class="math-container">$r=1$</span> around <span class="math-container">$(2,2)$</span>, then the  path can be defined as <span class="math-container">$f(p)={{\cos(p)+2}\choose{\sin(p)+2}}$</span> - where <span class="math-container">$p$</span> is the path parameter, in this case an angle of 0 to 360°, and just increasing the angle rotates right hand around. If we had the same path but a different starting point, a shift by <span class="math-container">$\theta$</span>, then the path would read <span class="math-container">$f(p)={{\cos(p+\theta)+2}\choose{\sin(p+\theta)+2}}$</span>. So math is usually <em>counterclockwise</em>.</p>
<h2>Slicers</h2>
<p>Every slicer is applying math. As far as I can tell, any Slicer generates a perimeter path, which is always performed in the same way if sliced with the same settings. For one case look at this:
<a href="https://i.sstatic.net/nSlg7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nSlg7.png" alt="5 seconds of print sliced in cura" /></a>
Counterclockwise starting from a 7 o clock position in this case. However, other slicers or other objects on the printbed might use other engines, thus doing it not that way. They might go clockwise since solving a path with <span class="math-container">$p=0°\to360°$</span> and solving it <span class="math-container">$p=360°\to0°$</span> results in producing the exact same print, just opposed print direction of the perimeter.</p>
<p>As long as the perimeter of an object is solved as being done as one closed loop, the perimeter will need to have just one, prescribed direction. This direction will be clockwise or counterclockwise depending on how the slicer exactly solves its calculations. Since both directions are equally valid, it is a programmer's decision. A programmer might even prescribe clockwise or counterclockwise solution based on any factor they wanted. They might use layer number (for alternating directions) or a user setting or even an RNG, if they wanted to.</p>
<p>On the other hand, how the memory is operated and written can also result in the path and the math looking differently. Two examples:</p>
<ul>
<li>Solving the path correctly counterclockwise and putting the slicing into a FILO memory, resulting in a clockwise operation starting from the last solved point.</li>
<li>Solving counterclockwise and saving into FIFO, running counterclockwise.</li>
</ul>
<h2>Conclusion</h2>
<p>Slicers for 3D printing have a hard-coded way to choose the direction that is followed when producing G-code. Any and all perimeters will be printed, starting from some arbitrary point, into that direction. In the end, it is a choice of the <em>programmer</em> of the slicing engine that determines if the path is to be run down &quot;forward&quot; or &quot;backward&quot; in mathematical sense.</p>
<h2>Addendum</h2>
<p>Slicers are derived from CAM programming. CAM - computer assisted machining - takes into account one more thing when solving the tool path that is not relevant to a 3D printer: The direction of the fluting of the tool. In fact, this one will determine into which direction the path will give a better cut and changing the fluting should swap the path direction to ensure best results.</p>

CC BY-SA 4.0
2019-01-15T11:31:51.693Z
@Thetravellingfool Path theory is commonly taught in Analysis lectures and textbooks. In my case, it was the class Math for Physicists III. FIFO/FILO are data storage informatics (First in, first/last out). Cura I can only carefully observe.
Trish
CC BY-SA 4.0
2019-01-15T21:00:24.910Z
@Trish, The addendum is is a little misleading. The fluting "direction" has nothing to do with the path. That only matters to the spindle. Changing the path is only necessary if you are doing climb vs conventional milling. This relates to the quality of the cut, not the ability to cut.
user77232
CC BY-SA 4.0
2019-01-15T23:51:06.587Z
@user77232 good comment, fixed up.
Trish
CC BY-SA 4.0
2019-01-14T22:52:38.150Z
2019-01-15T23:50:43.617Z
2020-06-18T08:29:41.667Z
Trish
<p>While <a href="https://3dprinting.stackexchange.com/a/7972/8884">this</a> answer makes a valid attempt at answering the question, it is based on personal experience.</p>

<p>I went to the literature and directly to the source code in Cura to find the answer.  In the academic article "<a href="https://link.springer.com/article/10.1007/s00170-012-4706-y" rel="noreferrer">Identifying the Directions of a Set of 2D Contours for Additive Manufacturing Process Planning</a>", Volpato et al. describe several methods for identifying the arbitrary directions of each contour in each layer, and additionally identifying which contours were "internal" and which were "external".  I quote from the paper:</p>

<blockquote>
  <p>The information regarding contour
  direction, which is either clockwise (CW — internal) or
  counterclockwise (CCW — external), is needed for path
  planning for material processing.</p>
</blockquote>

<p>They go on to explain the importance of identifying which contours are external, and which are internal, such that the path planning algorithm can later determine where infill should be placed.  Infill is placed internal to any external contours, and external to any internal contours.  </p>

<blockquote>
  <p>When the normal vectors in STL models
  are assumed to be correct, a simple way to identify whether
  a 2D contour is CW or CCW is to analyze the vector (cross)
  product between a normal vector and a vector obtained from
  two vertices of the facet.</p>
</blockquote>

<p>This assumes the slicer has already determined intersection points between slicing planes and the STL file, and has sorted those intersection points into closed-contours.  This initial intersection point gathering and contour construction leads to an arbitrary directionality:</p>

<blockquote>
  <p>As any line segment of a contour can be the first in the
  sequence when the segments are connected, its orientation
  will dictate the direction of the contour. Hence, the 2D contours
  formed are classified randomly, and an external contour, for
  example, might be assigned a CW or CCW direction. Therefore, this step is unable to correctly identify the directions of the contours generated.</p>
  
  <p>The ray-tracing method, which is actually based on the
  point-in-polygon test, determines which contours are
  contained by others, and the orientation of each contour is
  then alternated between CCW and CW, the outermost contours being oriented CCW.</p>
</blockquote>

<p>So, the default directionality of a closed contour generated by a slicing program for FDM additive manufacturing turns out to be CCW based on cross products described above (and based on additional methods outlined in the paper).  Of course the standard directionality of a PRINTED contour does not HAVE to be this way, it appears to be a standard adopted by the AM community.  However, when a model produces contours inside of contours, the arbitrary directionality of those contours is determined, and then alternated from outside to inside, starting with CCW.  </p>

<p>As confirmation, according to a simple comment in the CURA source code:</p>

<pre><code>/*!
 * Outer polygons should be counter-clockwise,
 * inner hole polygons should be clockwise.
 * (When negative X is to the left and negative Y is downward.)
 */
</code></pre>

CC BY-SA 4.0
2019-01-15T23:52:48.560Z
would you please add the source code line and the pages you quote? Otherwise perfect answer
Trish
CC BY-SA 4.0
2019-01-15T19:54:48.083Z
2019-01-15T23:49:32.693Z
2019-01-15T23:49:32.693Z
Thetravellingfool
<p>I understand how slicer programs create sets of closed-loop polygons to print on a layer-by-layer basis.  For a given closed loop polygon which needs to be printed, the tool path generator will know the coordinates and how those coordinates are connected to each other, such that traversing a set of segments in that order will bring the extruder head back to the first coordinate to complete the closed loop.</p>

<p>My question is: By what mechanism does the tool path generator decide which direction to traverse the closed loop?  As it is a loop, that loop could be printed "clockwise" or "counter-clockwise", as it were.  Any details, and links to further explanations of how some of the big-name slicer programs determine this is much appreciated.</p>

<slicing>
How Do Tool Path Algorithms Decide Which Direction to Print a Closed-Loop Polygon
Thetravellingfool
# How Do Tool Path Algorithms Decide Which Direction to Print a Closed-Loop Polygon

(Asked by: username_0 on 2019-01-14)

I understand how slicer programs create sets of closed-loop polygons to print on a layer-by-layer basis. For a given closed loop polygon which needs to be printed, the tool path generator will know the coordinates and how those coordinates are connected to each other, such that traversing a set of segments in that order will bring the extruder head back to the first coordinate to complete the closed loop.

My question is: By what mechanism does the tool path generator decide which direction to traverse the closed loop? As it is a loop, that loop could be printed "clockwise" or "counter-clockwise", as it were. Any details, and links to further explanations of how some of the big-name slicer programs determine this is much appreciated.

**Tags:** slicing

---

## 2 answers

### Answer by username_0 on 2019-01-15. Score: 6

While this answer makes a valid attempt at answering the question, it is based on personal experience.
I went to the literature and directly to the source code in Cura to find the answer.  In the academic article "Identifying the Directions of a Set of 2D Contours for Additive Manufacturing Process Planning", Volpato et al. describe several methods for identifying the arbitrary directions of each contour in each layer, and additionally identifying which contours were "internal" and which were "external".  I quote from the paper:

The information regarding contour
  direction, which is either clockwise (CW — internal) or
  counterclockwise (CCW — external), is needed for path
  planning for material processing.

They go on to explain the importance of identifying which contours are external, and which are internal, such that the path planning algorithm can later determine where infill should be placed.  Infill is placed internal to any external contours, and external to any internal contours.  

When the normal vectors in STL models
  are assumed to be correct, a simple way to identify whether
  a 2D contour is CW or CCW is to analyze the vector (cross)
  product between a normal vector and a vector obtained from
  two vertices of the facet.

This assumes the slicer has already determined intersection points between slicing planes and the STL file, and has sorted those intersection points into closed-contours.  This initial intersection point gathering and contour construction leads to an arbitrary directionality:

As any line segment of a contour can be the first in the
  sequence when the segments are connected, its orientation
  will dictate the direction of the contour. Hence, the 2D contours
  formed are classified randomly, and an external contour, for
  example, might be assigned a CW or CCW direction. Therefore, this step is unable to correctly identify the directions of the contours generated.
The ray-tracing method, which is actually based on the
  point-in-polygon test, determines which contours are
  contained by others, and the orientation of each contour is
  then alternated between CCW and CW, the outermost contours being oriented CCW.

So, the default directionality of a closed contour generated by a slicing program for FDM additive manufacturing turns out to be CCW based on cross products described above (and based on additional methods outlined in the paper).  Of course the standard directionality of a PRINTED contour does not HAVE to be this way, it appears to be a standard adopted by the AM community.  However, when a model produces contours inside of contours, the arbitrary directionality of those contours is determined, and then alternated from outside to inside, starting with CCW.  
As confirmation, according to a simple comment in the CURA source code:
/*!
 * Outer polygons should be counter-clockwise,
 * inner hole polygons should be clockwise.
 * (When negative X is to the left and negative Y is downward.)
 */

> **Comments:**
> **username_1**: would you please add the source code line and the pages you quote? Otherwise perfect answer  

---

### Answer by username_1 on 2019-01-14. Score: 4

## Math

In math, there is a way how a path is to be followed, and that is usually counterclockwise:

Assuming a perimeter path of a circle with $r=1$ around $(2,2)$, then the path can be defined as $f(p)={{\cos(p)+2}\choose{\sin(p)+2}}$ - where $p$ is the path parameter, in this case an angle of 0 to 360°, and just increasing the angle rotates right hand around. If we had the same path but a different starting point, a shift by $\theta$, then the path would read $f(p)={{\cos(p+\theta)+2}\choose{\sin(p+\theta)+2}}$. So math is usually *counterclockwise*.

## Slicers

Every slicer is applying math. As far as I can tell, any Slicer generates a perimeter path, which is always performed in the same way if sliced with the same settings. For one case look at this:
[![5 seconds of print sliced in cura](https://i.sstatic.net/nSlg7.png)](https://i.sstatic.net/nSlg7.png)
Counterclockwise starting from a 7 o clock position in this case. However, other slicers or other objects on the printbed might use other engines, thus doing it not that way. They might go clockwise since solving a path with $p=0°\to360°$ and solving it $p=360°\to0°$ results in producing the exact same print, just opposed print direction of the perimeter.

As long as the perimeter of an object is solved as being done as one closed loop, the perimeter will need to have just one, prescribed direction. This direction will be clockwise or counterclockwise depending on how the slicer exactly solves its calculations. Since both directions are equally valid, it is a programmer's decision. A programmer might even prescribe clockwise or counterclockwise solution based on any factor they wanted. They might use layer number (for alternating directions) or a user setting or even an RNG, if they wanted to.

On the other hand, how the memory is operated and written can also result in the path and the math looking differently. Two examples:

* Solving the path correctly counterclockwise and putting the slicing into a FILO memory, resulting in a clockwise operation starting from the last solved point.
* Solving counterclockwise and saving into FIFO, running counterclockwise.

## Conclusion

Slicers for 3D printing have a hard-coded way to choose the direction that is followed when producing G-code. Any and all perimeters will be printed, starting from some arbitrary point, into that direction. In the end, it is a choice of the *programmer* of the slicing engine that determines if the path is to be run down "forward" or "backward" in mathematical sense.

## Addendum

Slicers are derived from CAM programming. CAM - computer assisted machining - takes into account one more thing when solving the tool path that is not relevant to a 3D printer: The direction of the fluting of the tool. In fact, this one will determine into which direction the path will give a better cut and changing the fluting should swap the path direction to ensure best results.

> **Comments:**
> **username_1**: @Thetravellingfool Path theory is commonly taught in Analysis lectures and textbooks. In my case, it was the class Math for Physicists III. FIFO/FILO are data storage informatics (First in, first/last out). Cura I can only carefully observe.  
> **username_2**: @Trish, The addendum is is a little misleading. The fluting "direction" has nothing to do with the path. That only matters to the spindle. Changing the path is only necessary if you are doing climb vs conventional milling. This relates to the quality of the cut, not the ability to cut.  
> **username_1**: @user77232 good comment, fixed up.  

---

