How LaTeX plots edges
In computer science, there are two types of books: Those using TikZ of graphs, and others First one have often harmonious figures such as the following one.
Which begs the question, what's there secret? How to the manage to plot nice edges?
It's like magic. As soon as you know the trick it feels too simple.
You've been warned.
The basic equation
Here start the deception. like most of the plotting tools, TikZ uses cubic Bezier.
You don't know what it is? If you remember your math classes about 2nd degree polynomial (the one with ) it's nearly the same. Except that here we will go one step further with a power 3 (that's where"cubic" comes from).
The line will be point fo coordinate for between and .
Don't worries too much about those equations. The only interesting fact is that we hve 8 parameters (, ; , ..., ). And instead of manipulate those parameters, we will consiter 4 special points:
- the starting point (when )
- the arrival point (when )
- the starting direction
- the arrival direction
The best is to play with those points in the next example. You can try to create the edge from A to B. Having a nice one is not that easy.
One of the problems to create edge is the coordinate system.
Placing a point is not super easy. Should it be slightly on the left, right. Are they both aligned?
With TikZ you should not place start/end points by yourself. You're controlling the angle between the center of the nodes and their control points. It's the computer that will ensure the starting point is exactly on the border of the node.
By default, an edge has and angle of 30° between it's control point, and the straight line between the tow nodes.
Controlling this angle has two main advantages:
- It's simpler
- It allows consistency if you move nodes.
With fixed angles. We now need to define the distance between the starting point and the starting direction.
An interesting would be that plots stay the same when the distance between nodes varies. The edge should have the same shape is the distance between A and B is 1, 10, or 100.
Here is an experiment of two edges. You can move nodes A and B to test different values of the distance between them.
- The red edge use a fix distance between the starting point ann the starting direction point.
- The green edge use a proportional distance .
You're probably convinced now that having a fixed is a bad idea. For nodes close to each over, the edge will do some loops. Whereas proportional distance keep consistent shape while varying the distance. So with the number you want sounds a good idea.
But which value for ? We could pick , , , , or any other random value. But TikZ team had a nice idea for the default:
. I let you try to see why this value is a bit special.
You got it?
When the difference between the start and end angle is 90° (so 45° on both side of the edge) you got the best approximation of a circle. You can find the computation of this constant in an blog post from Spencer Mortensen about circle approximation.
That's smart because it's easier to use. Drawing a circle by hand is not that easy. Our eye is good at spotting littl imperfection on the circle. So instead of having to keep in mind this constant, it's the default one.
And if you don't want it? you can change! There is an additional parameter calles looseness such that .
So for, you have a perfect circle. If you want more looseness, you can increase it up to infinity. And for straighter lines, you can decrease it up to 0, which is a straight line.
And for edges looping on the same node?
We keep the same parameters, let just modify their values.
You can define the starting angle and the arriving angle . But event easier: just define the angle of the loop and then derive et .
Loops need more looseness so we will use .
But the distance between starting point and ending point is of course very small, because they are on the same node. The proposed solution is to defined a minimal distance . If the distance between those two point is less than , we will use instead of to compute .
Maths of this article are not supper complex. But it's interesting to see that for building nice tool you don't necessarily need fancy math. Sometime choosing a system that is well adapted for the use case, and with nice default configuration can be the best solution.
You can define Bezier cubic curves with 8 numbers, or 4 points. But in that case defining angles , , and looseness is more user friendly.
Those curves are beautiful 😍 So just for pleasure here are curves with various values of . Have fun :)
nb alpha: 10