repl.it
Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import svgwrite
from svgwrite.path import Path

LIMIT = 120
STEP_PX = 7

def recaman_steps():
    current = 0
    abs_step = 1
    visited = set()

    while True:
        visited.add(current)
        if current - abs_step >= 0 and current - abs_step not in visited:
            step = -abs_step
        else:
            step = abs_step

        yield step

        current += step
        abs_step += 1


def toggle_angle_direction(direction):
    return '+' if direction == '-' else '-'


def main():
    dwg = svgwrite.Drawing(filename='recaman.svg', debug=True)
    arc = Path(d='M 0 {}'.format(STEP_PX * LIMIT // 2), fill='white', stroke='green')

    i = 0
    prev_step = 0
    angle_direction = '+'
    for step in recaman_steps():
        i += 1
        if i == LIMIT:
            break

        if step * prev_step > 0:
            angle_direction = toggle_angle_direction(angle_direction)

        arc.push_arc((STEP_PX * step, 0), 0, 1, True, angle_direction)
        prev_step = step

    dwg.add(arc)
    dwg.save()


if __name__ == '__main__':
    main()