Reader small image

You're reading from  LaTeX Graphics with TikZ

Product typeBook
Published inJun 2023
PublisherPackt
ISBN-139781804618233
Edition1st Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Drawing Smooth Curves

In the first few chapters of this book, you learned about the TikZ tools for drawing lines, arrows, and shapes such as rectangles, circles, ellipses, and arcs. Now that you have advanced your TikZ skills, you are ready to tackle even more complex curves.

In this chapter, we will go through the following steps:

  • Manually creating a smooth curve through chosen points
  • Using a smooth plot to connect points
  • Specifying cubic Bézier curves
  • Using Bézier splines to connect given points
  • Using the Hobby algorithm for smoothly connecting points

As we explore each method, we will compare the results of different methods with the same reference curve so you can see how different techniques affect the outcome. This chapter focuses on creating freehand-like drawings of nicely rounded curves without exact parameters.

By the end of this chapter, you will be able to draw easy curves just like by hand with a pencil – smooth and...

Technical requirements

You can run and download the code of this chapter’s examples at https://tikz.org/chapter-12. On GitHub, you can find it at

https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/
main/12-drawing-smooth-curves
.

We will use the spline TikZ library, which you can download at https://github.com/stevecheckoway/tikzlibraryspline. Put it into your document folder or into your TeX distribution directory tree, where LaTeX can find it. Furthermore, we will use the hobby library included in regular LaTeX distributions.

Manually creating a smooth curve through chosen points

Our first goal is to draw a curve through several points that look round at any point. We will draw it similarly to a given curve as a second goal.

In the LaTeX Cookbook by Packt Publishing, in Chapter 10, Advanced Mathematics, there is a function plot that looks like the following:

Figure 12.1 – A sample curve without coordinates or parameters

Figure 12.1 – A sample curve without coordinates or parameters

If you don’t own the book, you can see that plot with code online at https://latex-cookbook
.net/function-plot
.

We will try to recreate this curve in the following steps:

  1. We will identify the coordinates of a few points of the curve.
  2. We will draw curve segments through these points to make it look like the original.
  3. We will adjust each segment’s start and end angle, as well as bending or the looseness of the curve, compile, look, and repeat until it looks as desired.

In the first step, we can include the source...

Using a smooth plot to connect points

TikZ can plot functions for us, either using a set of coordinate values or a mathematical parametrization. We will learn a lot about plotting in the next chapter; for now, let’s have just a first quick look at coordinate plots.

We will work with the set of points from the previous section. As said, we may need more points for more accuracy, so let’s look at Figure 12.2 again and choose two additional coordinates on the curve, such as two peak values (-1.3, 0.86) and (1.3, -0.86). TikZ can do a simple plot through all those points by the plot operation as follows:

\draw plot coordinates {
  (-3,-2.4) (-2,0.4) (-1.3,0.86) (-0.4,0.4)
  (0.4,-0.4) (1.3,-0.86) (2,-0.4) (3,2.4) };

These commands generate a sequence of straight linear segments:

Figure 12.5 – A plot through given coordinates

Figure 12.5 – A plot through given coordinates

That’s not smooth yet, but add the smooth keyword as an option to plot:

...

Specifying cubic Bézier curves

In the previous section, we saw that linear segments are not a good curve approximation. We could use quadratic curves and parabola segments to build rounder curves. Even better and more flexible are cubic curves. In computer graphics, so-called Bézier curves are used to approximate other curves, which are polynomial curves. Cubic Bézier curves are good enough and already complicated enough.

At the end of the chapter, in the Further reading section, you will get links to websites where you can read about the mathematics of Bézier curves. Here, we will look at them in a basic user approach, focusing only on the cubic curves that TikZ supports.

In TikZ, we can declare a curve from coordinates A to B with control points P and Q in the following way:

\draw (A) .. controls (P) and (Q) .. (B);

The curve starts in A in the direction toward P, which means that the line A to P is a tangent in A. Then, it ends in B coming from the...

Using Bézier splines to connect given points

The previous methods may be too laborious when we want to create a more complex curve defined by many points. It’s hard enough to find the control points for a desired Bézier curve by trial and error. And if we need a series of Bézier curve segments, called splines, it could be a nightmare.

Luckily, there’s the spline library. We can load it as follows:

\usetikzlibrary{spline}

Then we can specify end points as before and a spline through a set of coordinates that shall be passed through:

\draw[thick] (-3,-2.4)
  to[spline through={(-1.3,0.86)(1.3,-0.86)}] (3,2.4);

The library creates a path consisting of Bézier curve segments. It looks as follows, where I additionally plotted the used control points in gray:

Figure 12.8 – A curve with Bézier splines

Figure 12.8 – A curve with Bézier splines

Here, you can see that, for neighbor splines, the end and start control points are on a tangent...

Using the Hobby algorithm for smoothly connecting points

John Hobby, the creator of the MetaPost graphic language, developed an algorithm for drawing a curve through a given set of points. That’s similar to the previous section’s approach because it internally creates a list of cubic Bézier curves. The curves are parameterized in order to be joined together very smoothly. This provides very pleasing results. It’s not about perfect approximation; it’s about very smooth curves.

For us, it’s just another syntax with a different result. First, load the hobby library:

\usetikzlibrary{hobby}

Now, we set up a plot with start and end coordinates and two intermediate coordinates. We will take a few coordinates we used for Figure 12.5 to get a comparable result. The main difference is that we use hobby as a plot option:

\draw plot[hobby] coordinates { (-3,-2.4) (-1.3,0.86)
  (1.3,-0.86) (3,2.4)};

Compile it, and you get a curve...

Summary

In this chapter, you learned how to approximate curves that are complex or somehow randomly chosen or where we don’t know the mathematical parametrization.

And more importantly, you learned how to create smooth, elegant curves through a set of points you chose.

In the next chapter, we will talk much more about plotting functions, but at that time, we will deal with explicit mathematics.

Further reading

In the TikZ manual at https://texdoc.org/pkg/tikz, our topics are covered in Part III in the following sections:

https://en.wikipedia.org/wiki/B%C3%A9zier_curve in Wikipedia is a good starting point for reading about Bézier curves and finding further documents.

http://weitz.de/hobby is a JavaScript demonstration of the Hobby algorithm versus cubic splines, where you can define and move points by simple mouse clicks in a drawing.

The hobby library is explained at https://texdoc.org/pkg/hobby. For even more background, you can read the MetaPost manual at https://texdoc.org/pkg/metapost, particularly Section 4.2, Specifying Direction, Tension...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Graphics with TikZ
Published in: Jun 2023Publisher: PacktISBN-13: 9781804618233
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz