To upload files, please first save the app
import streamlit as st
import matplotlib.pyplot as plt
# Clase Vector2D
class Vector2D:
def __init__(self, x, y):
self.x = x
self.y = y
def suma(self, other):
return Vector2D(self.x + other.x, self.y + other.y)
def resta(self, other):
return Vector2D(self.x - other.x, self.y - other.y)
def escalar(self, other):
return (self.x)*(other.x) + (self.y)*(other.y)
# Función para graficar
def graficar_vectores(v1, v2):
suma = v1.suma(v2)
resta = v1.resta(v2)
fig, ax = plt.subplots()
ax.quiver(0, 0, v1.x, v1.y, angles='xy', scale_units='xy', scale=1, color='r', label="v1")
ax.quiver(0, 0, v2.x, v2.y, angles='xy', scale_units='xy', scale=1, color='b', label="v2")
ax.quiver(0, 0, suma.x, suma.y, angles='xy', scale_units='xy', scale=1, color='g', label="v1 + v2")
ax.quiver(0, 0, resta.x, resta.y, angles='xy', scale_units='xy', scale=1, color='m', label="v1 - v2")
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
ax.grid()
ax.legend()
return fig
# Interfaz Streamlit
def main():
st.title("Operaciones con Vectores 2D")
st.subheader("Ingrese los componentes de los vectores")
x1 = st.number_input("x1", value=3.0)
y1 = st.number_input("y1", value=4.0)
x2 = st.number_input("x2", value=2.0)
y2 = st.number_input("y2", value=-1.0)
v1 = Vector2D(x1, y1)
v2 = Vector2D(x2, y2)
if st.button("Mostrar Gráfica"):
fig = graficar_vectores(v1, v2)
st.pyplot(fig)
producto_escalar = v1.escalar(v2)
st.write(f"Producto escalar (v1 · v2): `{producto_escalar}`")
if __name__ == "__main__":
main()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?