Spinning Cube

In [1]:
from numpy import array, pi, cos, sin, sqrt
import ipyvolume as ipv

Static cube

In [2]:
def static_cube():
    sqrt3 = sqrt(3)
    r = sqrt(2/3)
    p1 = [0, 0, 0]
    p2 = [0, 0, sqrt3]
    p3, p4, p5 = [[r*cos(a*2*pi/3), r*sin(a*2*pi/3), sqrt3/3] for a in range(3)]
    p6, p7, p8 = [[r*cos(a*2*pi/3+pi/3), r*sin(a*2*pi/3+pi/3), sqrt3*2/3] for a in range(3)]
    points = [p1, p2, p3, p4, p5, p6, p7, p8]
    x, y, z = list(zip(*points))
    triangles = [[0, 2, 3], [0, 3, 4], [0, 2, 4],
                 [1, 5, 6], [1, 6, 7], [1, 5, 7],
                 [2, 3, 5], [3, 4, 6], [2, 4, 7],
                 [5, 6, 3], [6, 7, 4], [5, 7, 2]]
    return x, y, z, triangles

ipv.figure()
x, y, z, triangles = static_cube()
ipv.style.use('minimal')
ipv.plot_trisurf(x, z, y, triangles=triangles)
ipv.show()

Spinning cube

In [3]:
def spinning_cube(n=24):
    assert(n % 6 == 0)
    sqrt3 = sqrt(3)
    r = sqrt(2/3)
    x = array([[0, 0, *[r*cos(a*2*pi/3+t*2*pi/n) for a in range(3)], *[r*cos(a*2*pi/3+pi/3+t*2*pi/n) for a in range(3)]] for t in range(n)])
    y = array([[0, 0, *[r*sin(a*2*pi/3+t*2*pi/n) for a in range(3)], *[r*sin(a*2*pi/3+pi/3+t*2*pi/n) for a in range(3)]] for t in range(n)])
    z = array([0, sqrt3, sqrt3/3, sqrt3/3, sqrt3/3, sqrt3*2/3, sqrt3*2/3, sqrt3*2/3])
    triangles = [[0, 2, 3], [0, 3, 4], [0, 2, 4],
                 [1, 5, 6], [1, 6, 7], [1, 5, 7],
                 [2, 3, 5], [3, 4, 6], [2, 4, 7],
                 [5, 6, 3], [6, 7, 4], [5, 7, 2]]
    return x, y, z, triangles

ipv.figure()
ipv.style.use('minimal')
x, y, z, triangles = spinning_cube(72)
s = ipv.plot_trisurf(x, z, y, triangles=triangles)
ipv.animation_control(s, interval=100)
ipv.show()
# ipv.movie('spinning_cube.gif', fps=20, frames=20*4,)

Spinning cube volume

In [4]:
def spinning_cube_volume(n=24):
    assert(n % 6 == 0)
    sqrt3 = sqrt(3)
    r = sqrt(2/3)
    p1 = [0, 0, 0]
    p2 = [0, 0, sqrt3]
    poly1 = [[r*cos(a*2*pi/n), r*sin(a*2*pi/n), sqrt3/3] for a in range(n)]
    poly2 = [[r*cos(a*2*pi/n+pi/3), r*sin(a*2*pi/n+pi/3), sqrt3*2/3] for a in range(n)]
    points = [p1, p2, *poly1, *poly2]
    x, y, z = list(zip(*points))
    triangles1 = [[0, i+2, (i+1)%n+2] for i in range(n)]
    triangles2 = [[1, i+n+2, (i+1)%n+2+n] for i in range(n)]
    triangles3 = [[i+2, (i+1)%n+2, i+2+n] for i in range(n)]
    triangles4 = [[i+2+n, (i+1)%n+2+n, (i+1)%n+2] for i in range(n)]
    triangles = triangles1 + triangles2 + triangles3 + triangles4
    return x, y, z, triangles

ipv.figure()
ipv.style.use('minimal')
x, y, z, triangles = spinning_cube_volume(72)
ipv.plot_trisurf(x, z, y, triangles=triangles)
ipv.show()

Spinning Cube STL

In [5]:
import ipyvolume as ipv
from stl import mesh

ipv.figure()
ipv.style.use('minimal')
m = mesh.Mesh.from_file('Spinning_the_Cube/files/SpinningCube50mm_60_whole.stl')
ipv.plot_mesh(m.x, m.y, m.z)
ipv.show()

Article en fran├žais : Le cube en rotation - Francis Wolinski