Cowyeet/parabeltest.py
2023-11-14 01:25:31 +01:00

118 lines
3.2 KiB
Python
Executable file

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, sys, termios, tty, time, random
from math import sin, cos, tan, sqrt
from time import sleep
# Randomly choose a cow
cows = "🐵🐒🦍🦧🐕🐯🦝🐩🐅🐴🐎🦄🦌🐗🐂🐃🐄🐪🐫🦙🦒🐹🦘🦡🐧🕊️🦅🦆🦉🐍🦎🐊🦜🦚🦩🐲🐉🦕"
cowlistlength = len(cows)
cow = cows[random.randint(0, cowlistlength - 1)]
cowlength = len(cow)
# Needed: find our screensize
termsize_xy = os.get_terminal_size()
xmax = termsize_xy[0] - 1
ymax = termsize_xy[1]
ymin = 0
xmin = 0
# pi is not defined by default
pi = 3.1416
deg2rad = pi / 180
# position cursor at x,y - where x=0 y=0 is the left lower corner like in mathematical diagrams
def curpos(x, y):
print("\033[%d;%dH" % (ymax - y, x), end="", flush=True)
# CTRL and ESC-codes for the used outputdevice. Probably a terminal.
clear, home, curoff, curon = "'\x1b[2J\x1b[H", "\x1b[H", "\x1b[?25l", "\x1b[?25h"
# some scales, we need
# not everything has to be recomputed in loops.
xsteps = xmax
x = 0
y = x**2
erde = 9.81
mond = 1.6
jupiter = 24
gravitation = erde
# in scala the resulting coordinates will be returned
scala = []
# used, if input is disabled
starthoehe = 0
startwinkel = 26
startgeschwindigkeit = 23
loeschen = "n"
# Be a friendly host, lets have a talk.
# comment out to speed up testing
starthoehe = float(input("Starthöhe in Metern, empf: 0-20: "))
startwinkel = float(input("Startwinkel 0° - 90°: ")) * deg2rad
startgeschwindigkeit = float(input("Startgeschwindigkeit Meter (zb 20) pro Sekunde: "))
loeschen = input("Kuh löschen? (y/n)")
####
# Calculate time of flight
flugdauer = startgeschwindigkeit * sin(startwinkel) / gravitation
# Calculate maximum height
hoehe = 0.5 * gravitation * flugdauer**2 + starthoehe
# calculate length of flight
wurfweite = (
startgeschwindigkeit
* cos(startwinkel)
* (startgeschwindigkeit * sin(startwinkel) + sqrt(startgeschwindigkeit**2 * sin(startwinkel) ** 2 + 2 * gravitation * starthoehe))
) / gravitation
# init screen and wait for userinput
print(clear, curoff)
print(
"xteps: ",
xsteps,
"\nwurfweite: ",
wurfweite,
"\nstartwinkel: ",
startwinkel / deg2rad,
"\nstartgeschwindigkeit: ",
startgeschwindigkeit,
"\nstarthoehe: ",
starthoehe,
"\nhoehe: ",
hoehe,
)
bla = input("CR please:")
# position cursor down left corner
curpos(1, ymax)
# cowlength is 2, if cow is an UTF8-Icon, otherwise 1
for x in range(1, xsteps, cowlength):
# the formula, which generates the y position for the corresponding x, shamelessly ripped from some schoolbook and modified.
yold = y
y = (-(gravitation / (2 * startgeschwindigkeit**2 * cos(startwinkel) ** 2)) * x**2) + (tan(startwinkel) * x + starthoehe)
# Ensure, that nothing has to be drawn outside the viewport
if y < ymax and y > ymin:
curpos(x, y)
print(cow, end="")
# Stash awa the coordinates into scala, which will be returned
scala += [x, int(y * 10 + 5) / 10]
time.sleep(0.03)
if loeschen == "y":
curpos(x - 2, yold)
print(" ", end="")
curpos(0, 0)
# print(scala)
sys.exit(curon + "Moooooooooooo.")