diff --git a/README.md b/README.md new file mode 100644 index 0000000..b4c3c93 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Math-scripts +Python examples for beginners + +Scripts for solve / investigate mathematical exercises +by python3 scripts. + +Inspired by Dima |Matan| Mikhailov + +https://www.youtube.com/channel/UCocsNgfW9tGLP8zktA6CtRg diff --git a/moving_Sierpinski_triangle.py b/moving_Sierpinski_triangle.py index c084fcd..06a3cb2 100644 --- a/moving_Sierpinski_triangle.py +++ b/moving_Sierpinski_triangle.py @@ -1,7 +1,27 @@ +#!/usr/bin/python3 + +filling_char = 'A'; # Symbol for draw Serpinskiy triangle +fps_forward = 30; # frame-per-second / animation speed +fps_backward = 30; # frame-per-second / animation speed + import time +import os + +# detect terminal sizes +try: + # Since python 3.3 this method accessible + ts = os.get_terminal_size() # get terminal size (in char-places) + height = ts.lines - 1 # height of serp-3angle = ht-1 + width = int(ts.columns / 3) # width of movement base = 1/3 of tw + # 1/3 is safe value; true mathematics can try calculate more beautiful +except: + width = 26; # ~ 80/3 + height = 24; # std DOS/CMD h/w values - safe mode or old python + +# create Serpinsky triangle a = [[1]] -for i in range(73): +for i in range(height): b = [1] for j in range(len(a[-1]) - 1): b.append(a[-1][j] + a[-1][j + 1]) @@ -12,17 +32,21 @@ if a[i][j] % 2 == 0: a[i][j] = ' ' else: - a[i][j] = 'A' + a[i][j] = filling_char d = ' '.join(a[i]) + + +# draw triangle with loop animation while 1: - for j in range(165): - time.sleep(.02) + for j in range(width): # forward (left-to-right move) + time.sleep(1/fps_forward) for i in range(len(a)): f = ' '.join(a[i]) e = (len(d) - len(f)) // 2 print(j * ' ' + e * ' ' + f + e * ' ') - for j in range(165, 0, -1): - time.sleep(.02) + + for j in range(width, 0, -1): # backward (right-to-left move) + time.sleep(1/fps_backward) for i in range(len(a)): f = ' '.join(a[i]) e = (len(d) - len(f)) // 2