Cowyeet/physics/parabelfunc.py

129 lines
3.6 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os, sys, termios, tty, time, random
from math import sin, cos, tan, sqrt
from time import sleep
2023-12-29 20:00:39 +01:00
# pi is not defined by default
pi = 3.1416
deg2rad = pi / 180
2023-11-19 18:53:07 +01:00
########################################################################################
2023-12-29 20:31:52 +01:00
def berechneflugbahn(xmax, ymax, steps, startwinkel, startgeschwindigkeit, xmin=0, ymin=0, starthoehe=0, gravitation=9.81, xstep=1):
x = 0
y = x**2
2023-12-29 20:00:39 +01:00
# in scala the resulting coordinates will be returned
scala = []
2023-12-29 20:00:39 +01:00
2023-11-17 21:16:33 +01:00
startwinkel = startwinkel * deg2rad
2023-11-20 14:49:07 +01:00
# Calculate time of flight, actually not used
flugdauer = startgeschwindigkeit * sin(startwinkel) / gravitation
# Calculate maximum height
hoehe = 0.5 * gravitation * flugdauer**2 + starthoehe
2023-11-20 14:49:07 +01:00
# calculate length of flight, actually not used
wurfweite = (
startgeschwindigkeit
* cos(startwinkel)
* (startgeschwindigkeit * sin(startwinkel) + sqrt(startgeschwindigkeit**2 * sin(startwinkel) ** 2 + 2 * gravitation * starthoehe))
) / gravitation
2023-12-29 20:31:52 +01:00
first_run = True
# cowlength is 2, if cow is an UTF8-Icon, otherwise 1
2023-12-29 20:31:52 +01:00
for x in range(xmin, steps - 1, xstep):
# the formula, which generates the y position for the corresponding x, shamelessly ripped from some schoolbook and modified.
y = (-(gravitation / (2 * startgeschwindigkeit**2 * cos(startwinkel) ** 2)) * x**2) + (tan(startwinkel) * x + starthoehe)
# Ensure, that nothing has to be drawn outside the viewport
2023-12-29 20:31:52 +01:00
if x >= xmax:
x = xmax
if y >= ymax:
y = ymax
2023-12-29 19:01:37 +01:00
elif y <= ymin:
2023-12-29 20:31:52 +01:00
y = ymin
# Stash away the coordinates into scala, which will be returned
scala += [x, int((y * 100 + 50) / 100)]
if y == ymin and not first_run:
2023-12-29 20:00:39 +01:00
break
2023-12-29 19:01:37 +01:00
2023-12-29 20:31:52 +01:00
first_run = False
return scala
2023-11-19 18:53:07 +01:00
#############################################################################################
# berechneflugbahn(cow, xmin, xmax, ymin, ymax, startwinkel, startgeschwindigkeit, starthoehe):
2023-11-20 14:49:07 +01:00
# some useful control
clear, home, curoff, curon = "'\x1b[2J\x1b[H", "\x1b[H", "\x1b[?25l", "\x1b[?25h"
2023-12-29 18:54:39 +01:00
2023-12-29 20:00:39 +01:00
g_erde = 9.81 # gravitation examples
mond = 1.6
jupiter = 24
2023-11-19 17:29:23 +01:00
# set position of cursor
def curpos(x, y):
print("\033[%d;%dH" % (ymax - y, x), end="", flush=True)
2023-11-17 21:16:33 +01:00
2023-11-19 17:29:23 +01:00
2023-12-29 18:54:39 +01:00
if __name__ == "__main__":
# Randomly choose a cow
cows = "🐵🐒🦍🦧🐕🐯🦝🐩🐅🐴🐎🦄🦌🐗🐂🐃🐄🐪🐫🦙🦒🐹🦘🦡🐧🕊️ 🦅🦆🦉🐍🦎🐊🦜🦚🦩🐲🐉🦕"
cowlistlength = len(cows)
cow = cows[random.randint(0, cowlistlength - 1)]
2023-12-29 18:54:39 +01:00
# Needed: find our screensize. We are in textmode here
termsize_xy = os.get_terminal_size()
### I m p o r t a n t p a r a m e t e r s ###
# X-Resolution of the display
xmax = termsize_xy[0] - 1
2023-12-29 20:31:52 +01:00
xsteps = xmax * 2
2023-12-29 18:54:39 +01:00
ymax = termsize_xy[1]
ymin = 0
xmin = 0
startwinkel = 34
2023-12-29 20:31:52 +01:00
startgeschwindigkeit = 60
2023-12-29 18:54:39 +01:00
starthoehe = 0
schlafzeit = 0.05
###
2023-12-29 20:00:39 +01:00
# needed for erasing old position
2023-12-29 18:54:39 +01:00
xold = xmin
yold = ymin
x = xmin
y = xmin
# Call the function, which calculates the coordinates)
2023-12-29 20:31:52 +01:00
ergebnis = berechneflugbahn(xmax, ymax, xsteps, startwinkel, startgeschwindigkeit, starthoehe, gravitation=g_erde, xstep=2)
2023-12-29 18:54:39 +01:00
print(curoff)
# here we draw the cow
for count in range(xmin, len(ergebnis), 2):
xold, yold = x, y
x, y = ergebnis[count], ergebnis[count + 1]
curpos(x, y)
print(cow, end="")
sleep(schlafzeit)
curpos(xold, yold)
print("☁️ ", end="")
2023-12-29 20:00:39 +01:00
sleep(2)
2023-12-29 18:54:39 +01:00
2023-12-29 20:00:39 +01:00
sys.exit(home + curon)