File size: 1,372 Bytes
1f7ba69
76bad62
 
1f7ba69
76bad62
 
 
 
1f7ba69
76bad62
 
 
 
1f7ba69
76bad62
 
1f7ba69
76bad62
 
 
1f7ba69
76bad62
1f7ba69
76bad62
 
1f7ba69
76bad62
1f7ba69
76bad62
 
 
1f7ba69
76bad62
 
1f7ba69
76bad62
 
 
 
1f7ba69
76bad62
 
1f7ba69
76bad62
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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()