Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
Created August 30, 2024 15:17
Show Gist options
  • Save PM2Ring/68e7fccb2602cc14eb13d856ead48689 to your computer and use it in GitHub Desktop.
Save PM2Ring/68e7fccb2602cc14eb13d856ead48689 to your computer and use it in GitHub Desktop.
Tensegrity twisted prism in 3D, using SageMath
# Tensegrity twisted prism. PM 2Ring 2024.08.31
def ball(radius, **kwargs):
var('u,v')
kw = dict(plot_points=40) | kwargs
return spherical_plot3d(radius, (u, 0, 2*pi), (v, 0, pi), **kw)
def axis_matrix(W):
a, b, c = W
if abs(c) == 1:
return identity_matrix(3) * sgn(c)
U = vector([b, -a, 0]).normalized()
V = W.cross_product(U)
return matrix([U, V, W]).T
def cyl(V0, V1, radius=1, **kwargs):
W = V1 - V0
wnorm = W.norm()
M = axis_matrix(W / wnorm)
func = V0 + M * vector([radius * cos(u), radius * sin(u), v])
kw = dict(plot_points=(40, 2)) | kwargs
return parametric_plot3d(func, (u, 0, 2*pi), (v, 0, wnorm), **kw)
def circ(q):
sn, cs = n(2 * pi * q).sincos()
return vector((cs, sn, 0))
@interact
def main(num=3, twist=1/3,
height=3.0r, c_rad=0.10r, t_rad=0.05r,
c_color="#0a0", t_color="#b00",
dark=True, perspective=True, auto_update=False):
Z = vector((0, 0, height))
pos_lo = [circ(i / num) for i in range(num)]
pos_hi = [circ((i + twist) / num) + Z for i in range(num)]
P = Graphics()
b = ball(radius=c_rad, color=c_color)
P += sum(b.translate(p) for p in pos_lo + pos_hi)
P += sum(cyl(*pq, radius=t_rad, color=t_color)
for pq in zip(pos_lo, pos_lo[1:]+pos_lo[:1]))
P += sum(cyl(*pq, radius=t_rad, color=t_color)
for pq in zip(pos_hi, pos_hi[1:]+pos_hi[:1]))
P += sum(cyl(*pq, radius=t_rad, color=t_color)
for pq in zip(pos_lo, pos_hi))
P += sum(cyl(*pq, radius=c_rad, color=c_color)
for pq in zip(pos_lo, pos_hi[1:]+pos_hi[:1]))
theme = "dark" if dark else "light"
projection = "perspective" if perspective else "orthographic"
kw = dict(frame=False, theme=theme, projection=projection, online=True)
P.show(**kw)
P.save("tensegrity.html", **kw)
@PM2Ring
Copy link
Author

PM2Ring commented Aug 30, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment