Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- lr2rl = str.maketrans({'L': 'R', 'R': 'L'})
- def dragon_curve(n):
- if n == 0:
- return ""
- return dragon_curve(n - 1) + "L" + dragon_curve(n - 1)[::-1].translate(lr2rl)
- def turtle_graphics(f, x, y, step, turns):
- dx, dy = step, 0
- for turn in turns + "F":
- svg_line = f'<line x1="{x}" y1="{y}" x2="{x + dx}" y2="{y + dy}" stroke="black" stroke-width="3"/>'
- print(svg_line, file=f)
- x += dx; y += dy
- matrix = moves[turn]
- dx, dy = matrix[0] * dx + matrix[1] * dy, matrix[2] * dx + matrix[3] * dy
- def random_turn(dx, dy):
- alpha = random.random() * 2 * math.pi
- return dx * math.cos(alpha) + dy * math.sin(alpha), -dx * math.sin(alpha) + dy * math.cos(alpha)
- moves = {
- 'F': (1, 0, 0, 1),
- 'L': (0, 1, -1, 0),
- 'R': (0, -1, 1, 0),
- 'M': (math.cos(math.pi / 16), math.sin(math.pi / 16), -math.sin(math.pi / 16), math.cos(math.pi / 16))
- }
- f = open("generated.svg", "w")
- print("<!-- Generated -->", file=f)
- # os.system("generated.svg")
- hei = 800
- wid = 800
- print('<svg xmlns="http://www.w3.org/2000/svg" height="' + str(hei) + 'px" width="' + str(wid) + 'px">', file=f)
- turns = dragon_curve(13) # "FFFFLRFFFFMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
- print(turns)
- turtle_graphics(f, wid // 2, hei // 2, 3, turns)
- print('</svg>', file=f)
- f.close()
- /*
- 1 L 1 v←
- 2 L 10 ←
- 3 R 11 v
- 4 L 100 ←
- 5 L 101 v
- 6 R 110
- 7 R 111 v
- 8 L 1000 ←
- 9 L 1001 v
- 10 L 1010
- 11 R 1011 v
- 12 R 1100
- 13 L 1101 v
- 14 R 1110
- 15 R 1111 v
- 16 L 10000 ←
- 17 L 10001 v
- 18 L 10010
- 19 R 10011 v
- 20 L 10100
- 21 L 10101 v
- LLRLLRRLLL
- 10010110101110000 x
- 10010110101101111 x - 1
- 00000000000011111 x xor (x - 1)
- 00000000000100000 x xor (x - 1) + 1
- 00000000000100000 x and (x xor (x - 1) + 1) является ли 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement