gradio/demo/kinematics_blocks/run.py

38 lines
882 B
Python
Raw Normal View History

2022-03-22 06:53:08 +08:00
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
def plot(v, a):
g = 9.81
2022-03-25 14:03:47 +08:00
theta = a / 180 * 3.14
2022-03-22 06:53:08 +08:00
tmax = ((2 * v) * np.sin(theta)) / g
2022-03-25 14:03:47 +08:00
timemat = tmax * np.linspace(0, 1, 40)[:, None]
x = (v * timemat) * np.cos(theta)
y = ((v * timemat) * np.sin(theta)) - ((0.5 * g) * (timemat**2))
2022-03-22 06:53:08 +08:00
fig = plt.figure()
2022-03-25 14:03:47 +08:00
plt.scatter(x=x, y=y, marker=".")
2022-03-22 06:53:08 +08:00
plt.xlim(0, 100)
plt.ylim(0, 60)
return fig
2022-03-25 14:03:47 +08:00
demo = gr.Blocks()
2022-03-22 06:53:08 +08:00
with demo:
2022-03-25 14:03:47 +08:00
gr.Markdown(
"Let's do some kinematics! Choose the speed and angle to see the trajectory."
)
2022-03-22 06:53:08 +08:00
with gr.Row():
2022-03-25 14:03:47 +08:00
speed = gr.Slider(25, min=1, max=30, label="Speed")
2022-03-22 06:53:08 +08:00
angle = gr.Slider(45, min=0, max=90, label="Angle")
output = gr.Image(type="plot")
btn = gr.Button("Run")
btn.click(plot, [speed, angle], output)
if __name__ == "__main__":
demo.launch()