![](https://blogimg.goo.ne.jp/user_image/5f/d0/56e144bb9861364a268682a4e806fec5.jpg)
以下はPydroidで動作する、座標入力で四面体を描画するGUIプログラムです。matplotlibを使用して3Dグラフィックスを表示します。
```python
import tkinter as tk
from tkinter import ttk, messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
class TetrahedronDrawer:
def __init__(self, root):
self.root = root
self.root.title("3D Tetrahedron Drawer")
self.root.geometry("800x600")
# 座標入力用変数
self.vertices = [
[tk.DoubleVar(value=0.0) for _ in range(3)] for _ in range(4)
]
# グラフ表示用フレーム
self.figure = plt.figure(figsize=(6, 6))
self.ax = self.figure.add_subplot(111, projection='3d')
# GUIコンポーネントの初期化
self.create_widgets()
def create_widgets(self):
# 入力パネル
input_frame = ttk.LabelFrame(self.root, text="頂点座標入力")
input_frame.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.Y)
# 頂点入力フィールド
for i in range(4):
ttk.Label(input_frame, text=f"頂点 {i+1}:").grid(row=i, column=0, padx=5, pady=2)
for j in range(3):
entry = ttk.Entry(input_frame, textvariable=self.vertices[i][j], width=8)
entry.grid(row=i, column=j+1, padx=2, pady=2)
entry.bind("<FocusOut>", self.validate_input)
# 座標のデフォルト値設定
default_coords = [
[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
]
for i in range(4):
for j in range(3):
self.vertices[i][j].set(default_coords[i][j])
# 制御ボタン
btn_frame = ttk.Frame(input_frame)
btn_frame.grid(row=4, column=0, columnspan=4, pady=10)
ttk.Button(btn_frame, text="描画", command=self.plot).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="リセット", command=self.reset).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="終了", command=self.root.quit).pack(side=tk.LEFT, padx=5)
# グラフ表示領域
graph_frame = ttk.Frame(self.root)
graph_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
self.canvas = FigureCanvasTkAgg(self.figure, master=graph_frame)
self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
def validate_input(self, event):
"""入力値の検証"""
for i in range(4):
for j in range(3):
try:
float(self.vertices[i][j].get())
except ValueError:
messagebox.showerror("エラー", "数値を入力してください")
event.widget.focus_set()
return
def get_coords(self):
"""座標値の取得"""
return [
[self.vertices[i][j].get() for j in range(3)]
for i in range(4)
]
def plot(self):
"""四面体の描画"""
try:
self.ax.clear()
coords = self.get_coords()
# 頂点のプロット
for i, (x, y, z) in enumerate(coords):
self.ax.scatter(x, y, z, color='r', s=50)
self.ax.text(x, y, z, f'V{i+1}', fontsize=12)
# 辺の描画
edges = [
(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)
]
for start, end in edges:
line = [
[coords[start][0], coords[end][0]],
[coords[start][1], coords[end][1]],
[coords[start][2], coords[end][2]]
]
self.ax.plot(*line, color='b', lw=1)
# 面の描画
faces = [
[coords[0], coords[1], coords[2]],
[coords[0], coords[1], coords[3]],
[coords[0], coords[2], coords[3]],
[coords[1], coords[2], coords[3]]
]
self.ax.add_collection3d(Poly3DCollection(
faces, facecolors='cyan', linewidths=1, alpha=0.3
))
# グラフ設定
self.ax.set_xlabel('X軸')
self.ax.set_ylabel('Y軸')
self.ax.set_zlabel('Z軸')
self.ax.set_title('3D Tetrahedron')
self.ax.set_box_aspect([1,1,1])
self.canvas.draw()
except Exception as e:
messagebox.showerror("エラー", f"描画に失敗しました:\n{str(e)}")
def reset(self):
"""入力値のリセット"""
default_coords = [
[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]
]
for i in range(4):
for j in range(3):
self.vertices[i][j].set(default_coords[i][j])
self.plot()
if __name__ == "__main__":
root = tk.Tk()
app = TetrahedronDrawer(root)
root.mainloop()
```
### 主な機能
1. 4つの頂点座標を入力可能なフォーム
2. リアルタイムの3D描画表示
3. 基本操作ボタン(描画・リセット・終了)
4. 入力値の自動検証
5. デフォルトの正四面体座標設定
### 使い方
1. 各頂点のX,Y,Z座標を入力
2. 「描画」ボタンをクリック
3. 3Dグラフをマウスで回転して確認
4. 「リセット」でデフォルト値に戻る
### 動作要件
- Pydroidで実行する場合、matplotlibとnumpyのインストールが必要
- 3Dグラフは指で回転操作可能
- 入力値は自動で数値チェックが行われます
### 注意事項
- 非常に複雑な形状を入力すると描画が崩れる場合があります
- 座標値は適切な範囲で入力してください(例:-10~10)
- Androidデバイスによっては描画処理に時間がかかる場合があります
このプログラムは実際の3D座標系で四面体を可視化し、インタラクティブに操作できるGUIを提供します。
![](https://blogimg.goo.ne.jp/user_image/41/d5/593c8ad89ecbd3f6297e9ad7bfbaeda2.jpg?1739056774)