1. Koch Curve
The Swedish mathematician Helge von Koch proposed the Koch curve in his 1904 paper “On a continuous curve without tangents, constructible from elementary geometry.” It is described as follows:
- Specify the length of a line segment \(l\) (this can be thought of as iteration 0)
- Divide this segment into three equal parts, construct an equilateral triangle with the middle segment as its base, then remove the base
- Repeat step 2 for every edge of the curve generated in step 2 (each operation is called one iteration)
The length of the resulting set figure is: $$L=l*(\frac{4}{3})^{N}$$, where N is the number of iterations.
1.2 Drawing method:
- If N=0, just draw a straight line of length L
- If N=1 (the first iteration), draw a segment of length L/3; turn the pen left 60 degrees and draw a segment of length L/3; turn the pen right 120 degrees and draw a segment of length L/3; then turn the pen left 60 degrees and draw a segment of length L/3

- If n>1, the n-th iteration is equivalent to: n-1 iterations; turn the pen left 60 degrees; n-1 iterations; turn the pen right 120 degrees; n-1 iterations; turn the pen left 60 degrees; n-1 iterations.
1.3 Python code implementation
| |
1.4 The figures drawn
Below are the Koch curves generated with n=3, length=300 and n=4, length=400 respectively


2. Julia Set
2.1 Drawing method
As mentioned in the previous post, click here to go
- Set the initial values p, q, the maximum number of iterations N, the figure size a, b, and the number of colors used K. Note here that the modulus of c is always less than 2. It can be proved that when the modulus of c is greater than 2, the iteration will necessarily diverge to infinity.
- Set the boundary value of the region \( M\ge max(2,\sqrt{p^2+q^2}) \)
- Divide the region \(R=[-M,M]\times[-M,M]\) into an \(a\times b\) grid, and use each grid point as the initial value (\(x_0,y_0\)). Iterate using the formula after the substitution above. If \({x_n}^2+{y_n}^2\le M^2 \) holds for all \(n \le N\), set pixel \((i, j)\) to this color. If from some step n onward \({x_n}^2+{y_n}^2\ge M^2 \), set pixel \((i, j)\) to a different color.
2.2 Python code implementation
| |
2.3 The figures drawn
Parameters: p=0.285 q=0.01 N=200 M=100 a=2.0 b=2.0 step=0.005 (left figure)
Parameters: p=0.45 q=-0.1428 N=200 M=100 a=2.0 b=2.0 step=0.005 (right figure)


There are also other initial values of c that produce very beautiful patterns, for example:
c = -0.70176+-0.3842j
c = -0.835+-0.2321j
c = -0.8+0.156j
c = 0.285
3. Mandelbrot Set
Mathematical definition:
$$f_c(z) = z^2+c$$
The Mandelbrot set is the set of sequences for which the iteration of the function \(f_c(z)\), at z=0 with respect to the complex number c=x+yi, does not diverge.
The simplest way to draw the Mandelbrot set is to use escape time. Escape time refers to the number of iterations performed, within a specified range M and up to a finite count N, before the value leaves the region M. Different numbers of iterations are drawn in different colors.
- Set the maximum number of iterations, N
- Set the initial value of \(z_0\)
- Set the escape radius R, usually 2
3.1 Drawing method
3.2 Python implementation
| |
3.3 The figures drawn
The figure shows the image generated using the parameters: x0=0 y0=0 zoom=1.0 N=100 R=2 a=4.0 b=3.0 step=0.005. The difference is that they use quadratic and cubic power iterations respectively.


Finally, you can also use the ImageMagick tool to turn the generated images into an animated GIF.
| |

