Created
February 19, 2024 13:51
-
-
Save lmarcondes/9106329b1ecb6a8b56ae3969a77554bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import sys | |
from typing import Optional | |
class LaunchConfig: | |
def __init__( | |
self, | |
theta: float, | |
total_length: float, | |
initial_height: float, | |
final_height: float, | |
) -> None: | |
self.theta_degrees = theta | |
self.theta = math.radians(theta) | |
self.total_length = total_length | |
self.initial_height = initial_height | |
self.final_height = final_height | |
def __str__(self) -> str: | |
msg = f"""LaunchConfig( | |
theta={self.theta_degrees}, | |
total_length={self.total_length}, | |
initial_height={self.initial_height}, | |
final_height={self.final_height} | |
)""" | |
return msg | |
class ChunkinPunkin: | |
G = 9.81 # m/s^2 | |
CONVERSION = 3.6 | |
def _get_alpha(self, config: LaunchConfig) -> float: | |
alpha = ( | |
math.tan(config.theta) * config.total_length | |
+ config.initial_height | |
- config.final_height | |
) | |
return alpha | |
def _get_numerator(self, config: LaunchConfig) -> float: | |
return config.total_length**2 * self.G | |
def _get_denominator(self, config: LaunchConfig) -> float: | |
return 2 * math.cos(config.theta) ** 2 * self._get_alpha(config) | |
def get_needed_speed(self, config: LaunchConfig) -> float: | |
v_square = self._get_numerator(config) / self._get_denominator(config) | |
v = math.sqrt(v_square) | |
return v | |
def get_flight_time( | |
self, config: LaunchConfig, launch_speed: Optional[float] = None | |
) -> float: | |
if launch_speed is None: | |
launch_speed = self.get_needed_speed(config) | |
t = config.total_length / (launch_speed * math.cos(config.theta)) | |
return t | |
@classmethod | |
def to_kmh(cls, speed: float) -> float: | |
converted_speed = cls.CONVERSION * speed | |
return converted_speed | |
def report(self, config: LaunchConfig) -> None: | |
speed = self.get_needed_speed(config) | |
speed_kmh = self.to_kmh(speed) | |
flight_time = self.get_flight_time(config, speed) | |
print(config) | |
print("Launch speed =", round(speed, 2), "m/s", round(speed_kmh, 2), "km/h") | |
print("Flight time =", round(flight_time, 2), "s") | |
if __name__ == "__main__": | |
args = sys.argv | |
(length, initial_height, final_height) = ( | |
float(args[1]), | |
float(args[2]), | |
float(args[3]), | |
) | |
print("Arguments: ", args[1:]) | |
thetas = [15.0, 30.0, 45.0, 60.0, 75.0, 80.0, 85.0, 90.0] | |
calculator = ChunkinPunkin() | |
for theta in thetas: | |
config = LaunchConfig(theta, length, initial_height, final_height) | |
calculator.report(config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment