Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
1.4 kB
1
Indexable
Never
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def loft_between_polylines(polyline1, polyline2):
    num_vertices1 = len(polyline1)
    num_vertices2 = len(polyline2)
    
    # Create a parameter that goes from 0 to 1 for each polyline
    t1 = np.linspace(0, 1, num_vertices1)
    t2 = np.linspace(0, 1, num_vertices2)
    
    # Interpolate each polyline separately
    interpolated_vertices1 = np.array([(1 - t) * vertex1 for t, vertex1 in zip(t1, polyline1)])
    interpolated_vertices2 = np.array([t * vertex2 for t, vertex2 in zip(t2, polyline2)])
    
    # Combine the interpolated vertices to create the lofted shape
    lofted_shape = interpolated_vertices1 + interpolated_vertices2
    
    return lofted_shape

# Define two example polylines as lists of vertices
polyline1 = np.array([(0, 0, 0), (1, 2, 1), (2, 3, 2), (3, 2, 3)])
polyline2 = np.array([(0, 0, 5), (1, 1, 6), (2, 2, 7)])

# Perform lofting between the polylines
interpolated_vertices = loft_between_polylines(polyline1, polyline2)

# Plot the result in 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(*polyline1.T, marker='o', label='Polyline 1')
ax.plot(*polyline2.T, marker='o', label='Polyline 2')
ax.plot(*interpolated_vertices.T, marker='o', label='Interpolated')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.show()