varun500's picture
Update app.py
76bad62
raw
history blame
No virus
1.37 kB
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
def plot_polygon(edges, vertices):
if len(edges) != len(vertices):
st.warning("Number of edges and vertices should be the same.")
return
fig, ax = plt.subplots()
ax.set_aspect('equal', adjustable='box')
ax.set_title('Polygon Plot')
ax.grid(True)
edges = np.array(edges)
vertices = np.array(vertices)
for i in range(len(edges)):
ax.plot([vertices[i][0], vertices[(i + 1) % len(edges)][0]],
[vertices[i][1], vertices[(i + 1) % len(edges)][1]], 'b-')
ax.scatter(vertices[:, 0], vertices[:, 1], color='red')
plt.xlim(np.min(vertices[:, 0]) - 1, np.max(vertices[:, 0]) + 1)
plt.ylim(np.min(vertices[:, 1]) - 1, np.max(vertices[:, 1]) + 1)
st.pyplot(fig)
def main():
st.title("Polygon Plotter")
st.write("Enter the number of edges and vertices for the polygon, then input the coordinates.")
num_edges = st.number_input("Number of Edges:", min_value=3, step=1)
vertices = []
for i in range(num_edges):
x = st.number_input(f"Vertex {i + 1} - x:", key=f"x{i}")
y = st.number_input(f"Vertex {i + 1} - y:", key=f"y{i}")
vertices.append((x, y))
if st.button("Plot Polygon"):
plot_polygon(range(num_edges), vertices)
if __name__ == "__main__":
main()