Line CompositeΒΆ

../../_images/loop_spiral.png
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors
import s3dlib.surface as s3d
import s3dlib.cmap_utilities as cmu

#.. loop and spiral lines

# 1. Define function to examine .....................................
def loop(t) :
    Rm = 0.2     # main radius
    Rl = 0.6     # loop radius
    nL = 12      # number of loops
    tilt = 0.2   # loop tilt

    theta = 2*np.pi*t + np.pi/2
    phi = 2*np.pi*nL*t
    psi = tilt*np.pi*np.sin(phi)

    r = Rm + Rl*np.cos(phi) 
    z = Rl*np.sin(phi)
    x = r*np.sin( theta + psi )
    y = r*np.cos( theta + psi )

    return x,y,z

def spiral(t) :
    N = 12      # number of rotations
    R = np.ones(len(t))
    theta = 2*np.pi*N*t
    phi = np.pi*t
    return s3d.SphericalSurface.coor_convert([R,theta,phi],True)

def radDir(xyz) :
    return -s3d.SphericalSurface.coor_convert(xyz)[0]

# 2. Setup and map line .............................................
rez = 9
cy = colors.hsv_to_rgb([0.55,1,1])
mb = 'indigo'
BlToBk = cmu.hsv_cmap_gradient( [0.55,1,1,1], [0.55,1,0,0] )
mBlToBk = cmu.mirrored_cmap(BlToBk)

line1 = s3d.ParametricLine(rez,loop,lw=2)
line1.map_cmap_from_op( radDir  )

line2 = s3d.ParametricLine(rez,spiral,lw=2)
line2.map_cmap_from_sequence(mBlToBk)

line3 = line2 + line1
line3.transform(rotate=s3d.eulerRot(0,-30))

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

fig = plt.figure(figsize=plt.figaspect(1),facecolor='k')
info = str(line1) +'\n'+ str(line2) +'\n' + str(line3)
fig.text(0.975,0.975,info, ha='right', va='top', color='lightskyblue', 
    fontsize='smaller', multialignment='right')

ax = plt.axes(projection='3d',facecolor='k', aspect='equal')
ax.set_axis_off()
minmax = (-0.8,0.8)
ax.set(xlim=minmax, ylim=minmax, zlim=minmax )
ax.view_init(elev=45)

ax.add_collection3d(line3.shade(0.5))
ax.add_collection3d(line3.get_transformAxis(lenmult=1.25,color=(mb,mb,cy),negaxis=True))

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