Function Curves
Explore how to draw mathematical function curves with Pycairo by calculating points along the curve and connecting them to form smooth polylines. Understand scaling and translating coordinates to fit the canvas and practice controlling curve smoothness by adjusting point density.
We'll cover the following...
Function curves
Sometimes, we might want to draw a curve based on a mathematical function. Here is an example of a function:
y = f(x):
y = math.sin(10*x) * math.exp(-x/2)
This function is a decaying sine wave. It represents a simple damped oscillation. An example of this is the sound created when we pluck a guitar string, which starts off loud and gradually fades away. Don’t worry too much about the details of the function. We are only using it because it looks quite nice.
This is a graph of the function, created using Pycairo:
Intuition
How is this graph plotted in Pycairo? Well, it is actually quite simple. We choose a series of values of x and calculate f(x) for each value. This gives us a set of points that lie on the ...