Skip to content

Instantly share code, notes, and snippets.

@espdev
Last active September 27, 2019 12:25
Show Gist options
  • Save espdev/e403fefb467a7ad8ffe19d041ee9e05d to your computer and use it in GitHub Desktop.
Save espdev/e403fefb467a7ad8ffe19d041ee9e05d to your computer and use it in GitHub Desktop.
Draw rectangular parallelepiped using the opposite vertices (diagonal)
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
# x y z
a = (1, 1, 1) # p1
b = (2, 2, 2) # p2
x, y, z = 0, 1, 2
vertices = [
# XZ
[(a[x], a[y], a[z]), (b[x], a[y], a[z]), (b[x], a[y], b[z]), (a[x], a[y], b[z])],
[(a[x], b[y], a[z]), (b[x], b[y], a[z]), (b[x], b[y], b[z]), (a[x], b[y], b[z])],
# YZ
[(a[x], a[y], a[z]), (a[x], b[y], a[z]), (a[x], b[y], b[z]), (a[x], a[y], b[z])],
[(b[x], a[y], a[z]), (b[x], b[y], a[z]), (b[x], b[y], b[z]), (b[x], a[y], b[z])],
# XY
[(a[x], a[y], a[z]), (b[x], a[y], a[z]), (b[x], b[y], a[z]), (a[x], b[y], a[z])],
[(a[x], a[y], b[z]), (b[x], a[y], b[z]), (b[x], b[y], b[z]), (a[x], b[y], b[z])],
]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([a[x], b[x]], [a[y], b[y]], [a[z], b[z]], '-r')
ax.add_collection3d(Poly3DCollection(
vertices, facecolors='cyan', linewidths=1, edgecolors='k', alpha=.2))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment