Topologia / src /streamlit_app.py
relativus's picture
Update src/streamlit_app.py
75c9dea verified
import numpy as np
import streamlit as st
import plotly.graph_objects as go
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
# ==============================
# Часть 1: 3D-тетраэдр
# ==============================
st.title("Тетраэдр и его развёртка")
# Вершины тетраэдра (регулярный тетраэдр)
vertices = np.array([
[1, 1, 1], # Вершина 1
[-1, -1, 1], # Вершина 2
[-1, 1, -1], # Вершина 3
[1, -1, -1] # Вершина 4
])
# Грани тетраэдра (связи между вершинами)
faces = [
[0, 1, 2],
[0, 1, 3],
[0, 2, 3],
[1, 2, 3]
]
# Создание 3D-графика
fig_3d = go.Figure()
# Добавление точек
fig_3d.add_trace(go.Scatter3d(
x=vertices[:, 0],
y=vertices[:, 1],
z=vertices[:, 2],
mode='markers+text',
marker=dict(size=5, color='red'),
text=['1', '2', '3', '4'],
textposition="top center"
))
# Добавление граней
for face in faces:
x = vertices[face, 0]
y = vertices[face, 1]
z = vertices[face, 2]
fig_3d.add_trace(go.Mesh3d(
x=x, y=y, z=z,
opacity=0.3,
color='lightblue'
))
# Скрыть невидимую грань (линия 2-3)
fig_3d.add_trace(go.Scatter3d(
x=[vertices[1, 0], vertices[2, 0]],
y=[vertices[1, 1], vertices[2, 1]],
z=[vertices[1, 2], vertices[2, 2]],
mode='lines',
line=dict(color='blue', dash='dot')
))
fig_3d.update_layout(
scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'),
title="3D-тетраэдр",
margin=dict(l=0, r=0, b=0, t=40)
)
# ==============================
# Часть 2: Развертка тетраэдра (сетка)
# ==============================
fig_2d, ax = plt.subplots(figsize=(6, 6))
# Координаты вершин для развёртки
points = np.array([
[0, 0], # A
[2, 0], # B
[1, np.sqrt(3)], # C
[0.5, np.sqrt(3)/2], # D
[1.5, np.sqrt(3)/2] # E
])
# Построение треугольников
triangles = [
Polygon([points[0], points[1], points[2]], closed=True, fill=False, edgecolor='blue', linewidth=2),
Polygon([points[0], points[2], points[3]], closed=True, fill=False, edgecolor='blue', linewidth=2),
Polygon([points[1], points[2], points[4]], closed=True, fill=False, edgecolor='blue', linewidth=2),
Polygon([points[1], points[4], points[0]], closed=True, fill=False, edgecolor='blue', linewidth=2)
]
# Добавление меток к вершинам
labels = ['1', '2', '3', '4', '4']
for i, point in enumerate(points):
ax.text(point[0], point[1], labels[i], ha='center', va='center', fontsize=12, color='red')
# Настройка осей
ax.set_aspect('equal')
ax.axis('off')
# ==============================
# Вывод графиков
# ==============================
col1, col2 = st.columns(2)
with col1:
st.subheader("3D-тетраэдр")
st.plotly_chart(fig_3d)
with col2:
st.subheader("Развёртка тетраэдра")
st.pyplot(fig_2d)