# 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 $a x^2 + b x + c$) 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 $(x(t), y(t))$ for $t$ between $0$ and $1$.

$\begin{cases} x(t) = a_x t^3 + b_x t^2 + c_x t + d_x \\ y(t) = a_y t^3 + b_y t^2 + c_y t + d_y \end{cases}$

Don't worries too much about those equations. The only interesting fact is that we hve 8 parameters ($a_x$, $a_y$; $b_x$, ..., $d_y$). And instead of manipulate those parameters, we will consiter 4 special points:

• the starting point (when $t=0$)
• the arrival point (when $t=1$)
• 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.

## Modifying parameters

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:

1. It's simpler
2. 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 $d$ the distance between them.

• The red edge use a fix distance $D=1.6$ between the starting point ann the starting direction point.
• The green edge use a proportional distance $D= 0.5 \cdot d$ .

You're probably convinced now that having a fixed $D$ 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 $D=\alpha \cdot d$ with $\alpha$ the number you want sounds a good idea.

But which value for $\alpha$? We could pick $1$, $\frac{1}{2}$, $\pi$, $\phi$, or any other random value. But TikZ team had a nice idea for the default:

$\alpha=\frac{4}{3}\left(1 - \frac{1}{\sqrt{2}}\right) \approx 0.39$. I let you try to see why this value is a bit special.

alpha: 0.1

angle: 30°

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 $\ell$ calles looseness such that $d = 0.39 \cdot \ell \cdot D$.

So for, $\ell=1$ 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.

looseness: 1

angle: 30°

## Self loops

And for edges looping on the same node?

We keep the same parameters, let just modify their values.

You can define the starting angle $\theta_{out}$ and the arriving angle $\theta_{in}$. But event easier: just define the angle of the loop $\theta$ and then derive $\theta_{in}=\theta+15°$ et $\theta_{out}=theta-15°$.

Loops need more looseness so we will use $\ell=8$.

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 $d_{min}$. If the distance $d$ between those two point is less than $d_{min}$, we will use $d_{min}$ instead of $d$ to compute $D$.

## Conclusion

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 $\theta_{in}$, $\theta_{out}$, and looseness $\ell$ is more user friendly.

## For fun

Those curves are beautiful 😍 So just for pleasure here are curves with various values of $\alpha$. Have fun :)

nb alpha: 10

angle: 30°