HypercubeΒΆ

../../_images/hypercube1.png

Documentation on Hypercube surface can be found in Wikipedia where this function definition is located.

import numpy as np
import matplotlib.pyplot as plt
import s3dlib.surface as s3d
import matplotlib.colors as mc

#.. Hypercube
# 1. Define function to examine ....................................

# setup vertices ......
innerSize=0.5
backext = np.array( [ [-1,-1, 1], [-1, 1, 1], [-1, 1,-1], [-1,-1,-1] ] )
frntext = np.multiply(backext,[-1,1,1])
backinn = np.multiply(backext,innerSize)
frntinn = np.multiply(backinn,[-1,1,1])
temp = np.array([ backext, frntext, backinn, frntinn ])
v = np.reshape( temp,[-1,3])

# setup faces ......
extf = np.array([ [0,1,2,3], [1,2,6,5], [0,3,7,4], [4,7,6,5], [0,4,5,1], [3,2,6,7] ])
intf = np.add(extf,8)
edgf = [ [ 0, 8, 9, 1], [ 1, 9,13, 5], [ 5,13,12, 4], [ 4,12, 8, 0], 
         [ 3,11,10, 2], [ 2,10,14, 6], [ 6,14,15, 7], [ 7,15,11, 3], 
         [ 0, 3,11, 8], [ 1, 9,10, 2], [ 5, 6,14,13], [ 4,12,15, 7] ] 
f = np.concatenate((extf, intf, edgf), axis=0)

# 2. Setup and map surface .........................................
color = mc.to_rgba('C0',0.2)

surface = s3d.Surface3DCollection(v,f,color=color)

# 3. Construct figure, add surface plot ............................

fig = plt.figure(figsize=plt.figaspect(1))
ax = plt.axes(projection='3d', aspect='equal')
minmax = (-1.2,1.2)
ax.set(xlim=minmax, ylim=minmax, zlim=minmax)
ax.set_title('4D-Hypercube\n'+str(surface))
ax.set_axis_off()
ax.view_init(20,-60)

ax.add_collection3d(surface.shade(0.5,direction= [1,1,1], isAbs=True))

fig.tight_layout(pad=0)
plt.show()