Skip to content

Commit

Permalink
Update visualize.py
Browse files Browse the repository at this point in the history
  • Loading branch information
docxology committed Mar 17, 2024
1 parent f448b9b commit a5955ff
Showing 1 changed file with 47 additions and 45 deletions.
92 changes: 47 additions & 45 deletions visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,54 @@
import os
import sys

def plot_polyhedron(vertices, faces, title="Polyhedron Visualization", save=False, output_folder="images", file_name="polyhedron.png"):
"""
Enhances the visualization of a polyhedron given its vertices and faces by improving aesthetics and adding vertex labels.
Optionally saves the plot as a PNG file.
:param vertices: A list of tuples, each representing the x, y, z coordinates of a vertex.
:param faces: A list of tuples, each representing indices of vertices that form a face.
:param title: Title of the plot.
:param save: Boolean indicating whether to save the plot as a PNG file.
:param output_folder: The folder where the PNG file will be saved. Defaults to 'images'.
:param file_name: Name of the file to save the plot as. Defaults to 'polyhedron.png'.
"""
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

vtx = [[vertices[i] for i in face] for face in faces]

poly = Poly3DCollection(vtx, facecolors='skyblue', linewidths=0.5, edgecolors='darkblue', alpha=0.5)
ax.add_collection3d(poly)

for i, (x, y, z) in enumerate(vertices):
ax.scatter(x, y, z, color="darkred", s=100, edgecolors='black', zorder=5)
ax.text(x, y, z, f' V{i+1} ({x}, {y}, {z})', color='black')

ax.set_xlabel('X Axis', fontsize=12)
ax.set_ylabel('Y Axis', fontsize=12)
ax.set_zlabel('Z Axis', fontsize=12)

ax.set_xlim([min(v[0] for v in vertices)-1, max(v[0] for v in vertices)+1])
ax.set_ylim([min(v[1] for v in vertices)-1, max(v[1] for v in vertices)+1])
ax.set_zlim([min(v[2] for v in vertices)-1, max(v[2] for v in vertices)+1])

plt.title(title, fontsize=14, fontweight='bold')
plt.tight_layout()

if save:
if not os.path.exists(output_folder):
os.makedirs(output_folder)
plt.savefig(f"{output_folder}/{file_name}")
else:
plt.show()
plt.close()
class PolyhedronPlotter:
def __init__(self, output_folder="images"):
self.output_folder = output_folder

def plot_polyhedron(self, vertices, faces, title="Polyhedron Visualization", save=False, file_name="polyhedron.png"):
"""
Enhances the visualization of a polyhedron given its vertices and faces by improving aesthetics and adding vertex labels.
Optionally saves the plot as a PNG file.
:param vertices: A list of tuples, each representing the x, y, z coordinates of a vertex.
:param faces: A list of tuples, each representing indices of vertices that form a face.
:param title: Title of the plot.
:param save: Boolean indicating whether to save the plot as a PNG file.
:param file_name: Name of the file to save the plot as. Defaults to 'polyhedron.png'.
"""
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

vtx = [[vertices[i] for i in face] for face in faces]

poly = Poly3DCollection(vtx, facecolors='skyblue', linewidths=0.5, edgecolors='darkblue', alpha=0.5)
ax.add_collection3d(poly)

for i, (x, y, z) in enumerate(vertices):
ax.scatter(x, y, z, color="darkred", s=100, edgecolors='black', zorder=5)
ax.text(x, y, z, f' V{i+1} ({x}, {y}, {z})', color='black')

ax.set_xlabel('X Axis', fontsize=12)
ax.set_ylabel('Y Axis', fontsize=12)
ax.set_zlabel('Z Axis', fontsize=12)

ax.set_xlim([min(v[0] for v in vertices)-1, max(v[0] for v in vertices)+1])
ax.set_ylim([min(v[1] for v in vertices)-1, max(v[1] for v in vertices)+1])
ax.set_zlim([min(v[2] for v in vertices)-1, max(v[2] for v in vertices)+1])

plt.title(title, fontsize=14, fontweight='bold')
plt.tight_layout()

if save:
if not os.path.exists(self.output_folder):
os.makedirs(self.output_folder)
plt.savefig(f"{self.output_folder}/{file_name}")
else:
plt.show()
plt.close(fig)

if __name__ == "__main__":
# Example usage via command line
# Ensure that vertices and faces are passed correctly
plotter = PolyhedronPlotter()
polyhedron_list = [
{
"vertices": [(0, 0, 0), (1, 0, 0), (0, 1, 0), (0, 0, 1)],
Expand All @@ -65,4 +67,4 @@ def plot_polyhedron(vertices, faces, title="Polyhedron Visualization", save=Fals
}
]
for polyhedron in polyhedron_list:
plot_polyhedron(polyhedron["vertices"], polyhedron["faces"], title=polyhedron["title"], save=True, file_name=polyhedron["file_name"])
plotter.plot_polyhedron(polyhedron["vertices"], polyhedron["faces"], title=polyhedron["title"], save=True, file_name=polyhedron["file_name"])

0 comments on commit a5955ff

Please sign in to comment.