I have a CNC mill and a desire to make circuit boards. Naturally, I need to create tool paths for these circuit boards and cut them out with my mill.

I have created a JavaScript application to create SVG images of the circuit traces and landings, but now I need to determine the path around all traces and landings that intersect each other. Since all of my traces are poly lines, and my landings are either circles or rectangles, it’s super easy to just add width to the SVG objects and get paths around each trace or landing individually. Now I need to string together intersecting paths and cut out the parts I don’t want. This will require knowing the intersection points of lines with line, lines with arcs, and probably arcs with arcs. Let’s get started.
Line to Line
Our saga begins with the parameterization of a humble line,
Here the points are
I’ll set
Now let’s use this general line to describe a line segment from
Through the magic of 2D vectors, this is actually a system of two equations and two unknowns,
If we solve for just one of the variables, let’s say
Line to Arc
At first glance intersecting an arc with anything seems like it should be more difficult than just a couple of lines. But, while this problem isn’t quite as straightforward, it isn’t pear shaped either.
The easiest way to think of intersecting a line and an arc is to think of the points on a line that are some fixed distance away from the center of a circle. If we define a line like above, then we’re just looking for the value of
Knowing the sum of squares definition of vector length and a bit of off-screen algebra yields a friendly quadratic to solve.
If the solutions for
To start with, let’s barrow from the line to line problem and say that any solution for °
. Conveniently, one option has a positive vertical component to the vector, and the other has a negative vertical component.
Now, I could just be content with that, and move on to comparing the angles, but I feel like asking a computer to calculate inverse cosines is inefficient somehow, especially since all I care about is putting the start, end, and intersection points in order. The
But, why stop there? I don’t care about the angle values being from 0 to 360. Why not make it simple and multiply everything by
Ah, that’s better. Now we can easily calculate values for the beginning of the arc (
Arc to Arc
Intersecting arcs is more geometric than intersecting lines, with a linear algebra twist at the end. Effectively, we draw a triangle and align it to the vector between the centers of each arc. After finding the intersection points, we use the same angle estimation technique as in the line to arc section to make sure the points are on the actual arcs.
Let’s start with that triangle. In space, we’re dealing with the two triangles with a common base that runs between the centers of each arc (
With this set up, finding the location of
Which solves nicely down to:
Now, this triangle is aligned horizontally, but we need it to be aligned to the actual points
As you can see, the first column of the matrix is just the vector °
. This gives both potential intersection points. Now we have only to determine if they are actually on the arcs we started with.
The first hurdle to an intersection point being on an arc is for the two circles, on which the arcs are based, to actually touch. This comes clear when calculating the location of
With these angles and knowledge about which direction each arc travels from start to finish (clockwise or counter-clockwise), we can then decide if an intersection point is on both arcs.
Conclusions
This has all been great fun. These three intersection cases give opportunity to play with parameterized functions, linear algebra, geometry, and a hack-and-slash form of trigonometry. Unfortunately, the next step on this quest to create tool paths would require that I write a program to take a bunch of closed paths and splices together all of the paths that intersect, while deleting all segments of those paths that are completely inside other paths. The task isn’t super difficult, but it turns out Eagle CAD has a free hobbyist version and there’s an add-on that generates tool paths from Eagle CAD files. I’m going to use that.