import streamlit as st import numpy as np from abc import ABC, abstractmethod import matplotlib.pyplot as plt class RhinoDisplacement(ABC): @abstractmethod def displacement(self, time): pass class AcceleratingRhino(RhinoDisplacement): def __init__(self, acceleration): self.acceleration = acceleration def displacement(self, time): return 0.5 * self.acceleration * time**2 class ConstantSpeedRhino(RhinoDisplacement): def __init__(self, speed): self.speed = speed def displacement(self, time): return self.speed * time class RhinoMeetingTime(ABC): @abstractmethod def calculate_meeting_time(self, acceleration, speed, distance): pass class CalculateMeetingTime(RhinoMeetingTime): def calculate_meeting_time(self, acceleration, speed, distance): a = 0.5 * acceleration b = speed c = -distance discriminant = b**2 - 4 * a * c if discriminant < 0: return None time = (-b + np.sqrt(discriminant)) / (2 * a) return time class RhinoDisplacementPlot(ABC): @abstractmethod def plot_displacement(self): pass class DisplacementVisualization(RhinoDisplacementPlot): def __init__(self, accelerating_rhino, constant_speed_rhino, meeting_time): self.accelerating_rhino = accelerating_rhino self.constant_speed_rhino = constant_speed_rhino self.meeting_time = meeting_time def plot_displacement(self): fig, ax = plt.subplots() times = np.linspace(0, self.meeting_time, 100) disp_acc = [self.accelerating_rhino.displacement(t) for t in times] disp_const = [self.constant_speed_rhino.displacement(t) for t in times] ax.plot(times, disp_acc, label='Accelerating Rhino') ax.plot(times, disp_const, label='Constant Speed Rhino') ax.annotate('Accelerating rhino = 400 m', (self.meeting_time, disp_acc[-1]), textcoords="offset points", xytext=(-60, 10), ha='center') ax.annotate('Constant speed Rhino = 1200 m', (self.meeting_time, disp_const[-1]), textcoords="offset points", xytext=(-80, -20), ha='center') ax.set_xlabel('Time (s)') ax.set_ylabel('Displacement (m)') ax.legend() return fig def main(): st.title("Rhino Meeting Displacement Calculator") st.image("rhinos_0.png", caption='Rhinos Charging Towards One Another 0') acceleration = 1/8 # "Acceleration of the first rhino (m/s^2)" speed = 15 # "Speed of the second rhino (m/s)" distance = 1600 # "Initial distance between the rhinos (m)" if st.button("Generate Rhinos Charging Towards One Another Motion Graph Problem"): st.write("Two rhinos initially 1600 m apart, begin running directly toward one another at the same time. One rhino uniformly accelerates from rest at 1/8 m/s^2, while the other rhino runs with a constant speed of 15 m/s. What is the net displacement of each rhino when they meet?") if st.button("Formulations Required For Solving Problem"): st.write("### Formulations") st.latex(r'AR: s_1(t) = \frac{1}{2}at^2') # accelerating rhino st.write("") st.latex(r'CSR: s_2(t) = vt') # constant speed rhino st.write("") st.latex(r'TDCBBR: 1600 meters') # total distance covered by both rhinos st.write("") st.latex(r'Utilize: ax^2 + bx + c = 0') st.write("") st.latex(r'a = \frac{1}{2}\frac{1}{8}t^2') st.write("") st.latex(r'b = 15 m/s') st.write("") st.latex(r'\frac{1}{16}t^2 + 15t = 1600') st.write("") st.latex(r'c= -1600') st.write("") st.latex(r'\Delta = b^2 - 4ac = 15^2 - 4\frac{1}{16}1600 = 225 + 400 = 625') st.write("") st.latex(r'Solve: t = \frac{-15+-\sqrt(625)}{\frac{1}{8}} = \frac{-15 +- 25}{\frac{1}{8}}') st.write("") st.latex(r'Psol: t = \frac{10}{\frac{1}{8}} = 80 seconds') # positive solution st.write("") st.latex(r's_1(t) = s_1(80) = \frac{1}{2}\frac{1}{8}80^2 = \frac{1}{16}6400 = 400 meters') # accelerating rhino AR s_1(t) st.write("") st.latex(r's_2(t) = s_2(80) = 15 * 80 = 1200 meters') # Constant speed rhino s_2(t) st.write("") if st.button("Calculate"): calc_meeting_time = CalculateMeetingTime() time = calc_meeting_time.calculate_meeting_time(acceleration, speed, distance) if time is not None: accelerating_rhino = AcceleratingRhino(acceleration) constant_speed_rhino = ConstantSpeedRhino(speed) disp_acc = accelerating_rhino.displacement(time) disp_const = constant_speed_rhino.displacement(time) st.write(f"Time to meet: {time:.2f} seconds") st.write(f"Displacement of accelerating rhino: {disp_acc:.2f} meters") st.write(f"Displacement of constant speed rhino: {disp_const:.2f} meters") visualization = DisplacementVisualization(accelerating_rhino, constant_speed_rhino, time) fig = visualization.plot_displacement() st.pyplot(fig) else: st.write("The rhinos will never meet under these conditions.") st.image("rhinos_1.png", caption='Rhinos Charging Towards One Another 1') if __name__ == "__main__": main()