add format.py

This commit is contained in:
Pavel 2021-11-01 19:28:29 +01:00
parent 21feff7264
commit b37a418bd8
1 changed files with 55 additions and 0 deletions

55
alrys/format.py Normal file
View File

@ -0,0 +1,55 @@
import sys
from math import atan2, hypot, pi, cos, sin
from alryslib import *
if len(sys.argv) != 2:
print('Usage: python.exe test.py input_file')
sys.exit(1)
buffer = ''
try:
with open(sys.argv[1], 'r') as f:
buffer = f.read()
except IOError:
print('Cannot open %s' % sys.argv[1])
sys.exit(1)
steps = []
for e in buffer.split():
try:
steps.append(float(e))
except:
pass
print('''from math import atan2, sqrt, pi, cos, sin
from alryslib import *
deplacements = [''')
for i in range(8, len(steps) - 3, 4):
x, a, y, b = steps[i:i + 4]
print((' %7.3f, %2d, %6.3f, %2d,' % (x, a, y, b)).replace('.000', ' '))
print(''']
position = [47.5, 43.5, 0]
index = 0
def mon_itineraire():
global deplacements, position, index
x, a, y, b = deplacements[index:index + 4]
x += a * 1e-14
y += b * 1e-14
a = atan2(y - position[1], x - position[0]) * 180.0 / pi - position[2]
l = sqrt((x - position[0])**2 + (y - position[1])**2)
a_droite(a)
en_avant(l)
position[2] += a
position[0] += l * cos(position[2] * pi / 180.0)
position[1] += l * sin(position[2] * pi / 180.0)
index += 4
return index < len(deplacements) - 3 or action_clavier()
print(aller_selon(mon_itineraire))''')