import numpy as np
import matplotlib.pyplot as plt

# Konstante Erdbeschleunigung
g = 9.81  # m/s^2

# Anfangswerte für den waagerechten Wurf
x0 = 0.0     # Startposition x in m
y0 = 20.0    # Starthöhe in m
v0 = 10.0    # Anfangsgeschwindigkeit in m/s (rein horizontal)

# Optional: Benutzereingabe
# x0 = float(input("Startposition x0 in m: "))
# y0 = float(input("Starthöhe y0 in m: "))
# v0 = float(input("Horizontale Startgeschwindigkeit v0 in m/s: "))

# Bewegungsgleichungen
def x(t):
    """Weg in x-Richtung"""
    return x0 + v0 * t

def y(t):
    """Weg in y-Richtung"""
    return y0 - 0.5 * g * t**2

def vx(t):
    """Geschwindigkeit in x-Richtung (konstant)"""
    return v0 * np.ones_like(t)

def vy(t):
    """Geschwindigkeit in y-Richtung"""
    return -g * t

def ax(t):
    """Beschleunigung in x-Richtung (null)"""
    return np.zeros_like(t)

def ay(t):
    """Beschleunigung in y-Richtung (konstant -g)"""
    return -g * np.ones_like(t)

# Flugzeit bis zum Boden: y(t_f) = 0
# y0 - 0.5 * g * t_f^2 = 0  ->  t_f = sqrt(2*y0/g)
if y0 <= 0:
    raise ValueError("y0 muss > 0 sein, damit der waagerechte Wurf Sinn ergibt.")

t_flight = np.sqrt(2 * y0 / g)

# Zeitvektor
t = np.linspace(0, t_flight, 400)

# Werte berechnen
x_t = x(t)
y_t = y(t)
vx_t = vx(t)
vy_t = vy(t)
ax_t = ax(t)
ay_t = ay(t)

# Plot 1: Weg-Zeit-Funktionen (x(t) und y(t))
plt.figure()
plt.plot(t, x_t, label="x(t)")
plt.plot(t, y_t, label="y(t)")
plt.xlabel("Zeit t [s]")
plt.ylabel("Weg [m]")
plt.title("Waagerechter Wurf: Weg-Zeit-Funktionen")
plt.legend()
plt.grid(True)

# Plot 2: Geschwindigkeit-Zeit-Funktionen (vx(t) und vy(t))
plt.figure()
plt.plot(t, vx_t, label="v_x(t)")
plt.plot(t, vy_t, label="v_y(t)")
plt.xlabel("Zeit t [s]")
plt.ylabel("Geschwindigkeit [m/s]")
plt.title("Waagerechter Wurf: Geschwindigkeit-Zeit-Funktionen")
plt.legend()
plt.grid(True)

# Plot 3: Beschleunigung-Zeit-Funktionen (ax(t) und ay(t))
plt.figure()
plt.plot(t, ax_t, label="a_x(t)")
plt.plot(t, ay_t, label="a_y(t)")
plt.xlabel("Zeit t [s]")
plt.ylabel("Beschleunigung [m/s^2]")
plt.title("Waagerechter Wurf: Beschleunigung-Zeit-Funktionen")
plt.legend()
plt.grid(True)

# Plot 4: Bahnkurve y(x)
plt.figure()
plt.plot(x_t, y_t)
plt.xlabel("x [m]")
plt.ylabel("y [m]")
plt.title("Waagerechter Wurf: Bahnkurve y(x)")
plt.grid(True)

plt.show()
