Skip to content

Instantly share code, notes, and snippets.

@kazucmpt
Last active November 12, 2019 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazucmpt/1411604142478e21478d734a7f3751cf to your computer and use it in GitHub Desktop.
Save kazucmpt/1411604142478e21478d734a7f3751cf to your computer and use it in GitHub Desktop.
import math
import matplotlib.pyplot as plt
Pi = math.pi
max_depth = 10
save_img_fname = "DragonCurve_depth{}.png".format(max_depth)
def get_rotate_coordinate(x, y, Cx, Cy, theta=Pi/2):
newx = x*math.cos(theta)-y*math.sin(theta)+Cx-Cx*math.cos(theta)+Cy*math.sin(theta)
newy = x*math.sin(theta)+y*math.cos(theta)+Cy-Cx*math.sin(theta)-Cy*math.cos(theta)
return newx, newy
def draw(xs, ys):
plt.plot(xs,ys)
plt.axis("off")
plt.savefig(save_img_fname)
plt.show()
def main():
# Initial Condition
xs = [1, 0]
ys = [1, 1]
for depth in range(max_depth):
for k in range(2**depth):
newx, newy = get_rotate_coordinate(xs[2**depth-k-1], ys[2**depth-k-1], Cx=xs[2**depth], Cy=ys[2**depth])
xs.append(newx)
ys.append(newy)
print("depth:{} length:{}".format(depth, len(xs)-1))
print("drawing..")
draw(xs, ys)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment