# How to write arcs directly in G-code?

How can I write G-code for a triangle without sharp tips?

I want to generate the corners manually, rather than using a slicer to generate them, just to know how it is done.

• your question is extremely terse. Do you want to generate the corners manually or do you want to understand how a slicer maces them? How a CAD does them?! – Trish Jul 4 '19 at 17:18
• With lots and lots of tiny segments. – towe Jul 5 '19 at 6:23
• @towe True, even if you use G2/G3 – 0scar Jul 5 '19 at 11:40

Marlin has G2 (clockwise arc) and G3 (counterclockwise arc) commands that could be used to do this. You can find detailed documentation for the command here.

Basically, you can use

G2 R1 X5 Y5

to draw a (clockwise) arc from the current position to $$(X,Y)=(5,5)$$ with a radius of $$1$$.

So, your rounded triangle could be drawn with 3 straight line moves and 3 arc moves. Figuring out the exact coordinates for each move would be a quite challenging geometry exercise, as you'd need to know where the straight line portion of each side ends and the rounded portion starts.

• it's an easy exercise with a CAD though – Trish Jul 5 '19 at 8:29
• Do note that in the firmware support for G2/G3 must be enabled in the firmware, e.g. in Marlin by definition of #define ARC_SUPPORT in the Configuration_adv.h file. Also, shouldn't your printer board must have a sufficient capable processor to process the calculations for this option? – 0scar Jul 5 '19 at 9:05
• @0scar circles are rather usual in CNC commands and most printer boards can double for those. I assume you might want to use the IJ argument instead of the R Argument though. – Trish Jul 5 '19 at 9:07
• @Trish The question specifies the G-code must be created "manually". I'm not sure if using CAD still counts as manual. – Tom van der Zanden Jul 6 '19 at 6:54

First, convert the given measurements into a sketch...

## G-code shenanigans

we actually have the printer do circles.. let's plot that out...

Using that, it's easy to write the G-code using the Documentation for G1 and G2. You'll have to add the E values to extrude something along the paths, but your sketch would turn into this path:

G92 X0 Y0 ; the current position is now (0,0) on the XY
G90 ;Abolute mode for everything...
M83 ;...but for the E-argument, so you can just put the length into the extrusions that are to be done
G0 X10.66 Y2
G2 R5 X6.33 Y9.5 ; Alternate: G3 I0 J5 X6.33 Y9.5
G1 X45.66 Y77.638
G2 R5 X54.33 Y77.638 ; Alternate: G3 I4.33 Y-2.5 X54.33 Y77.638
G1 93.67 Y9.5
G2 R5 X89.33 Y2 ; Alternate: G3 I-4.33 Y-2.5 X89.33 Y2
G1 X10.66 Y2
G0 X0 Y0


This code has to be prefixed by a move to where you want to start the pattern and will not know if you move it off the build plate, so keep 100 mm X and 87 mm in Y of the allowable build plate. It will end exactly where you started it.

## Iterative approach

In many uses of g-code, rounded corners are actually n-gons with a very high number n. then we only need G1 and can easily calculate the length of the stretches and fill in the G1. We need to iterate down to somewhat circular...

Let's start iterating with n=3 aka a triangle, which gives a direct line over the corner gives this:

going to n=6 (hexagon) follows the curve a lot better...

going to n=12 looks almost round on a larger scale...

and when we reach n=24, we are pretty close to the circle..

And as we go above n=6, we also get easier math for the corners, as we always get the same lengths of movement along X and Y just swapped around due to symmetry.

With all these stretches defined, we could start to work in relative coordinates easily, again without E, and only for the bottom left corner:

G0 X10.66 Y2
G1 X-1.294 Y0.17
G1 X-1.206 Y0.5
G1 X-1.036 Y0.795
G1 X-0.795 X1.036
G1 X-0.5 Y1.206
G1 X-0.17 Y1.294
G1 X0.17 Y1.294
G1 X0.5 Y1.036
...

• Killer explanation ... love the graphics, which completely help the understanding. +1 – Pᴀᴜʟsᴛᴇʀ2 Jul 5 '19 at 16:50
• Beautifully illustrated, but it is responsive to the question The question seemed to be about writing arcs in g-code, rather than how one could simulate arcs with line segments. – cmm Jul 11 '19 at 14:45