This is a series about creating mathematical curves in BricsCAD (and AutoCAD).

Part 0, just hackish with a spreadsheet, result driven.

Part 1, the math, understanding ways to iterate.

Part 2, the coding, techniques available.

THIS ARTICLE IS UNDER CONSTRUCTION. YOUR POSITIVE INPUT IS APPRECIATED.

## Introduction

Mechanical engineers sometimes need mathematical curves. For example:

- A satellite dish is a basic parabola.
- A gear has the involute of a circle as its base.

These kind of curves are usually not directly supported in CAD systems. They must therefore be drawn as approximate. Read on.

A blunt approach is always possible, make sure the curve is made up of many line segments so that the CAD curve is little different from the theoretical curve. However, this has a number of disadvantages:

- Files can become large and therefore slow.
- CNC devices may have problems in the processing of very large amounts of segments.
- Intensive monitoring, calculating, of the curve is necessary to ensure the deviation is not too large.

I have to say that I’ve seen many implementations on the net and none was very clear when it came to using a proper algorithm. Without a suitable algorithm, the result will always be sloppy. This does not mean these solutions are not valuable, I just think there is something to win when we first start with the mathematics. Coding afterwards becomes like filling in a form. Feels good!

## What is required?

Programmers without mechanical engineering experience can make mistakes in the approach to the problem. To start the discussion, take notice of the following arguments:

- The curve should be smooth, sharp corners are not allowed, the change of direction (angle) determines the amount of nodes. And no polylines but splines or arcs. Counter arguments:
- There is no objection to use straight segments as long as there are enough. More straight lines, less calculations.
- If a radius of 1/1000 mm is composed of three line segments in steps of 30 degrees, then it will usually not be a problem. Defining one node per degree in that case is not smart.

It is no answer to the question, but it illustrates the complexity of the problem. Similarly, there are approaches whereby the length of the line segments of a curve is taken as a starting point. So what do we really want?

### What an engineer wants

The solution can be made incredibly complex, but a mechanical engineer wants one thing primarily: a controlled tolerance of shape. In other words, a maximum deviation – or less.

### What a CNC device wants

Not too much segments. Something to take into account when iterating points

## An example

A shaft having a diameter of 10 mm (R5, blue) has a tolerance of -0.1 mm (magenta). The circumference is not circular, but drawn as polyline with straight segments (red). The distance of the polyline to the circle can not be more than 10% of the tolerance. This is 0.01 mm. There are only 50 segments needed to define the circle.

If the tolerance is reduced further, there are more segments. If the value is not 0.01 mm but 0.001 mm, the circle can be described by 223 straight segments. Considering the precision, this is not bad. These are values which both CAD systems and CNC machines can handle.

## A rigid iteration method

### Achilles and the tortoise

One of Zeno’s paradoxes, closely related to the dichotomy paradox, is the race between Achilles and the tortoise. Quoted from Wikipedia:

In the paradox of Achilles and the tortoise, Achilles is in a footrace with the tortoise. Achilles allows the tortoise a head start of 100 meters, for example. Supposing that each racer starts running at some constant speed (one very fast and one very slow), then after some finite time, Achilles will have run 100 meters, bringing him to the tortoise’s starting point. During this time, the tortoise has run a much shorter distance, say, 10 meters. It will then take Achilles some further time to run that distance, by which time the tortoise will have advanced farther; and then more time still to reach this third point, while the tortoise moves ahead. Thus, whenever Achilles arrives somewhere the tortoise has been, he still has some distance to go before he can even reach the tortoise.

https://en.wikipedia.org/wiki/Zeno%27s_paradoxes#Paradoxes_of_motion

So Achilles never wins, it takes an infinite number of times to get past the tortoise. Loosely based on that is the convergent series:

### Real life application

(1)

We could use this approach in an iteration process. Based on the series, this is also true:

(2)

If you think about it, by using the following, choosing a + or – wisely, you can literally build any real from 0 to 1 by using those fractions only once:

(3)

So let’s see what it looks like.

Iteration to “n” can be done by conditional testing, for example a condition statement (cond …) in Lisp:

- If endpoint of line is more than “n” (+x), and unacceptable far from “n”, the next half length segment goes to the left.
- If endpoint of line is less than “n” (-x), and unacceptable far from “n”, the next half length segment goes to the right.
- If endpoint of line is within acceptable range, accept endpoint as answer.

## Useful theorems for approximating curves

Red: The theoretical curve. Left-bottom of triangle: Our point to check: .

### The convergence of error margin e to 0

When is entered in our function, the answer for is . Nice, but we want to know if is within an acceptable range, in order to approve point , or not.

Now here comes an interesting thing: and the more we iterate, the smaller part becomes. This is easy to explain, the more we “zoom in”, the straighter the red line. So:

**Important conclusion 1:**

(4)

Focus is now on . Knowing and , the value of is:

(5)

How do we get ?

Little bummer, we need to transform all used functions from to manually in order to retrieve . Or, use a numerical solution. Both solutions add a lot of complexity. Can we work around that?

### The fastest ingredient for calculating f

I would not have spent time writing this page if the answer was “no”. Read on. The point is, I like to “see” things in order to “better understand” things.

And that is not all, BricsCAD does have a reasonable proper SVG export facility, so you can enjoy the show too. So what is it? Look at the following:

- I’ve iterated an arc manually. Centre point 0,0 (1) start 10,0 (2) and end 0,10 (3).
- The magenta part:
- Line from 2 to 3. From middle 4 to 5 and from 4 to 6.
- Line from 5 to 6 and from 4 perpendicular to 7.
- Point 8 on intersecting of arc en line 4-7
- Conclusion: distance 4-8 is too large, so I iterate again starting at half x, between 3 and 4, being point 9.

- The blue part: Same procedure as magenta, not good enough, iterate the red part.
- The red part: Same procedure as blue. Hey! The result is fine, within limits..

You can’t see the details of blue and red. Here they are:

So again, what we want to know is the distance from point 4 to point 7. We do have the distance from 4 to 5. If we could say something about an angle like angle 4 5 7 or angle 5 4 7, we could solve.

And we can say something about it. Look at the baseline and the hypotenuse. Except for the first iteration, the angle between the baseline and the hypotenuse gets closer to 0 with each iteration. At the blue iteration, this angle is 2.78, at the red iteration, it is reduced to 1.46.

**Important conclusion 2:**

(6)

We can calculate the angle of the baseline based on start and end point. As a consequence of this theorem, we know the angle of the hypotenuse. Knowing b plus knowing the angle means we know f.

YESS!!! PROBLEM SOLVED!!!

And… I plan to implement this in a beautiful way. To be continued ðŸ˜‰