[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Draw a Fibonacci spiral in Python

[Drawing a Fibonacci spiral in Python]

My previous post Draw Fibonacci squares in Python shows how to draw Fibonacci squares. A Fibonacci spiral uses quarter circles drawn through those squares to make a spiral. Each section of the spiral is a quarter circle that connects a Fibonacci square's opposite corners as shown in the picture at the top of this post.

Spiral Arcs

This example's code is the same as the previous example's except for one small section. Inside the program's loop that draws the Fibonacci squares, the following code draws the spiral's arcs.

# Draw the spiral's arc. # Get a square twice as wide and tall as this one. v01 = (square[1][0] - square[0][0], square[1][1] - square[0][1]) v12 = (square[2][0] - square[1][0], square[2][1] - square[1][1]) p0 = (square[0][0] - v01[0], square[0][1]- v01[1]) p1 = (p0[0] + 2 * v01[0], p0[1] + 2 * v01[1]) p2 = (p1[0] + 2 * v12[0], p1[1] + 2 * v12[1]) p3 = (p2[0] - 2 * v01[0], p2[1] - 2 * v01[1]) xmin, ymin, xmax, ymax = polygon_bounds((p0, p1, p2, p3)) # Draw the arc. self.canvas.create_arc(xmin, ymin, xmax, ymax, start=angle, extent=90, style=tk.ARC, fill='', outline='purple', width=2)

The arc is a quarter of a circle with center at one of the square's corners and having radius equal to the square's side length. For example, look at the square with size 13 in the picture at the top of this post. Its arc has center on the square's lower left corner and has radius equal to the square's side length so the arc passes through the square's upper left and lower right corners.

The code finds the vectors v01 pointing from the square's first vertex to its second vertex. It also finds vector 12 pointing form the square's second vertex to its third vertex.

The program then uses those vectors and the square's vertices to find the vertices of the square that bounds the arc's circle. That's a square twice as tall and twice as wide as the original square and sharing that square's upper right corner.

The bounding square's points may not be in the order required by the Canvas widget's create_arc method, so the program calls polygon_bounds (described next) to get the square's coordinate bounds. The method then uses create_arc to draw the arc.

polygon_bounds

The following polygon_bounds helper function takes as input a list of points given as X/Y coordinate pairs. It returns the minimum and maximum X and Y coordinates of those points.

def polygon_bounds(polygon): '''Return the polygon's minimum and maximum X and Y coordinates.''' xmin = xmax = polygon[0][0] ymin = ymax = polygon[0][1] for point in polygon: if point[0] < xmin: xmin = point[0] if point[0] > xmax: xmax = point[0] if point[1] < ymin: ymin = point[1] if point[1] > ymax: ymax = point[1] return xmin, ymin, xmax, ymax

This code initializes its xmin, ymin, xmax, and ymax values to the coordinates of the first point. It then loops through the points and updates the minimum and maximum values as needed. When it's done, it returns the minimums and maximums that it found.

Conclusion

[A Fibonacci spiral drawn in Python] That's all there is to it. The program works much as the previous example did except it also draws each square's arc. There are a few other details, like the way it lets you skip drawing the Fibonacci squares, but you can download the program to see those details.

If you have a good eye, you may notice that there's something slightly off about the spiral. This is a bit more noticeable in the picture on the right which doesn't draw the Fibonacci squares.

The spiral's arcs meet smoothly with each tangent to those beside it, but their radii of curvature jump abruptly where they meet. Basically the spiral is smooth and its first derivative is smooth because the arcs are tangent where they meet, but the spiral's second derivative is a step function. That's different from most other types of spirals where the curve's second derivative is also smooth.

Download the example to experiment with it.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.