0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

立方体を平面で切った場合にできる断面図

Last updated at Posted at 2023-10-06
import numpy as np
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.art3d as art3d

tList = [0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]
axList = []
for i, t in enumerate(tList):

    if 0 <= t and t <=1:
        points = np.array([[0, 0, 0],
                          [1, 0, 0],
                          [1, 1, 0],
                          [0, 1, 0],
                          [0, 0, 1],
                          [1, 0, 1],
                          [1, 1, 1],
                          [0, 1, 1],
                          [t, 0, 0],
                          [0, t, 0],
                          [0, 0, t]])
        Z = points
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
 
        ax.add_line(art3d.Line3D([t, 0], [0, 0], [0, t], color='k'))
        ax.add_line(art3d.Line3D([0, 0], [0, t], [t, 0], color='k'))
        ax.add_line(art3d.Line3D([t, 0], [0, t], [0, 0], color='k'))
        axList.append(ax)
        
    elif 1 < t and t < 2:
       
        points = np.array([[0, 0, 0],
                          [1, 0, 0],
                          [1, 1, 0],
                          [0, 1, 0],
                          [0, 0, 1],
                          [1, 0, 1],
                          [1, 1, 1],
                          [0, 1, 1],
                          [t, 0, 0],
                          [0, t, 0],
                          [0, 0, t],
                          [t-1, 1, 0],
                          [1, t-1, 0],
                          [1, 0 , t-1],
                          [t- 1, 0, 1],
                          [0, t-1, 1],
                          [0, 1, t-1]])
        Z = points
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        ax.add_line(art3d.Line3D([t, 0], [0, 0], [0, t], color='c'))
        ax.add_line(art3d.Line3D([0, 0], [0, t], [t, 0], color='c'))
        ax.add_line(art3d.Line3D([t, 0], [0, t], [0, 0], color='c'))
        ax.add_line(art3d.Line3D([1, 1], [0, t - 1], [t - 1,  0 ], color='k'))
        ax.add_line(art3d.Line3D([1, t - 1], [t - 1, 1], [0,  0 ], color='k'))
        ax.add_line(art3d.Line3D([t - 1, 0], [1, 1], [0, t - 1], color='k'))
        ax.add_line(art3d.Line3D([0, 0], [1,  t - 1], [t - 1, 1], color='k'))
        ax.add_line(art3d.Line3D([0, t - 1], [t - 1,  0], [1, 1], color='k'))
        ax.add_line(art3d.Line3D([t - 1, 1], [0,  0], [1, t - 1], color='k'))
        axList.append(ax)

    elif 2 <= t and t <= 3:

        points = np.array([[0, 0, 0],
                           [1, 0, 0],
                           [1, 1, 0],
                           [0, 1, 0],
                           [0, 0, 1],
                           [1, 0, 1],
                           [1, 1, 1],
                           [0, 1, 1],
                           [t, 0, 0],
                           [0, t, 0],
                           [0, 0, t],
                           [1, t - 2, 1],
                           [1, 1, t - 2],
                           [t - 2, 1 , 1]])
        Z = points
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')

        ax.add_line(art3d.Line3D([t, 0], [0, 0], [0, t], color='c'))
        ax.add_line(art3d.Line3D([0, 0], [0, t], [t, 0], color='c'))
        ax.add_line(art3d.Line3D([t, 0], [0, t], [0, 0], color='c'))
        ax.add_line(art3d.Line3D([1, 1], [t - 2, 1], [1,  t - 2], color='k'))
        ax.add_line(art3d.Line3D([1, t - 2], [1, 1], [t - 2, 1], color='k'))
        ax.add_line(art3d.Line3D([t - 2, 1], [1, t - 2], [1, 1], color='k'))
        axList.append(ax)

    axList[i].scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])

    verts = [[Z[0],Z[1],Z[2],Z[3]],
             [Z[4],Z[5],Z[6],Z[7]],
             [Z[0],Z[1],Z[5],Z[4]],
             [Z[2],Z[3],Z[7],Z[6]],
             [Z[1],Z[2],Z[6],Z[5]],
             [Z[4],Z[7],Z[3],Z[0]]]

    axList[i].add_collection3d(Poly3DCollection(verts, facecolors='white', linewidths=1, edgecolors='r', alpha=.20))
    axList[i].set_title("t = {}".format(t))
    axList[i].view_init(elev=30, azim=45)

plt.show()

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?