Skip to content

Instantly share code, notes, and snippets.

@kinnala
Created February 18, 2022 09:33
Show Gist options
  • Save kinnala/f26223d065d556180901cd0ae42e4ae9 to your computer and use it in GitHub Desktop.
Save kinnala/f26223d065d556180901cd0ae42e4ae9 to your computer and use it in GitHub Desktop.
Dragon curve
# The equations are from: https://en.wikipedia.org/wiki/Dragon_curve
# I start with a line segment (0, 0) to (1, 0) and map the endpoints through f1 and f2.
# Then I have two segments (0, 0) to (0.5, 0.5) and (0.5, 0.5) to (1, 0).
# The endpoints are repeatedly mapped in the for-loop.
import matplotlib.pyplot as plt
import numpy as np
def f1(x):
return 1. / np.sqrt(2) * (
np.array([[np.cos(np.pi/4), -np.sin(np.pi/4)],
[np.sin(np.pi/4), np.cos(np.pi/4)]])
).dot(x)
def f2(x):
return 1. / np.sqrt(2) * (
np.array([[np.cos(3*np.pi/4), -np.sin(3*np.pi/4)],
[np.sin(3*np.pi/4), np.cos(3*np.pi/4)]])
).dot(x) + np.array([[1], [0]])
segments = [np.array([[1, 0], [0, 0]])]
for itr in range(10):
nsegments = []
for seg in segments:
nsegments.append(f1(seg))
nsegments.append(f2(seg))
segments = nsegments
plt.plot(np.array(segments)[:, 0, :].T, np.array(segments)[:, 1, :].T, 'k-')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment