python - How to draw a cylinder using matplotlib along length of point (x1,y1) and (x2,y2) with specified radius? -


i draw cylinder using matplotlib along length of point (x1,y1) , (x2,y2) specified radius r. please let me know how this.

just fun, i'm going generalize axis (x0, y0, z0) (x1, y1, z1). set z0 , z1 0 if want axis in xy plane.

you can find vector equation axis pretty finding unit vector in same direction axis, adding p0 , scaling along length of axis. can find coordinates of circle x = x0 + cos(theta) * r , y = y0 + sin(theta) * r, circles aren't in xy plane, we're going need make our own axes unit vectors perpendicular axis of cylinder , each other , xyz coordinates that. used site me figure out: http://mathforum.org/library/drmath/view/51734.html. here's code:

import numpy np matplotlib import pyplot plt mpl_toolkits.mplot3d import axes3d scipy.linalg import norm  fig = plt.figure() ax = fig.add_subplot(111, projection='3d') origin = np.array([0, 0, 0]) #axis , radius p0 = np.array([1, 3, 2]) p1 = np.array([8, 5, 9]) r = 5 #vector in direction of axis v = p1 - p0 #find magnitude of vector mag = norm(v) #unit vector in direction of axis v = v / mag #make vector not in same direction v not_v = np.array([1, 0, 0]) if (v == not_v).all():     not_v = np.array([0, 1, 0]) #make vector perpendicular v n1 = np.cross(v, not_v) #normalize n1 n1 /= norm(n1) #make unit vector perpendicular v , n1 n2 = np.cross(v, n1) #surface ranges on t 0 length of axis , 0 2*pi t = np.linspace(0, mag, 100) theta = np.linspace(0, 2 * np.pi, 100) #use meshgrid make 2d arrays t, theta = np.meshgrid(t, theta) #generate coordinates surface x, y, z = [p0[i] + v[i] * t + r * np.sin(theta) * n1[i] + r * np.cos(theta) * n2[i] in [0, 1, 2]] ax.plot_surface(x, y, z) #plot axis ax.plot(*zip(p0, p1), color = 'red') ax.set_xlim(0, 10) ax.set_ylim(0, 10) ax.set_zlim(0, 10) plt.show() 

figure of 3d cylinder surface


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -