我試圖在第一步中找到一種通過已知向量擬合直線的方法。第二步應該是找到這條線和所示平面之間的交點。我想要穿過一條線的向量用黑色橢圓標記,并在代碼中命名為 vector_3。
目標是在藍色矢量路徑圍繞 x 軸或 y 軸旋轉時調整 vector_3 的長度,以獲得始終在平面處結束的新矢量。
這是代碼(減少到現在相關的內容)和結果圖:
import numpy as np
import matplotlib.pyplot as plt
# Import 6D-Pose
translation_6D_pose = np.array([100, 0, 0, 1])
rotation_6D_pose = np.array([0, 0, 0])
# Definition of vectors
origin = np.array([0, 0, 0])
vector_1 = np.array([0, 100, 10, 1])
vector_2 = np.array([-100, 0, 50, 1])
vector_3 = np.array([0, 100, 0, 1])
vector_4 = np.array([0, 0, 60, 1])
X, Y, Z = origin
U, V, W, _ = translation_6D_pose
A, B, C, _ = vector_1
D, E, F, _ = vector_2
G, H, I, _ = vector_3
J, K, L, _ = vector_4
# Plot figure:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.set_xlim([-50, 200])
ax.set_ylim([-50, 200])
ax.set_zlim([-50, 200])
# Make plane:
xx, yy = np.meshgrid(range(-100, 100), range(0, 200))
z = xx*0 60
ax.plot_surface(xx, yy, z, alpha=0.2)
# Plot vectors:
ax.quiver(X, Y, Z, U, V, W)
ax.quiver(U, V, W, A, B, C)
ax.quiver(U A, V B, W C, D, E, F)
ax.quiver(X, Y, Z, G, H, I, color="r")
ax.quiver(G, H, I, J, K, L, color="r")
plt.show()

uj5u.com熱心網友回復:
要通過矢量繪制一條線,您只需創建與矢量對齊的點并繪制它們。要找到交集,您可以使用intersectionsympy 中的函式。請注意,在您的情況下,您也可以直接計算它。你的飛機很簡單,z=60所以計算非常簡單。
有關我使用 sympy 以使答案更通用的示例,請參見下面的代碼:
import numpy as np
import matplotlib.pyplot as plt
from sympy import Point3D, Line3D, Plane
# Import 6D-Pose
translation_6D_pose = np.array([100, 0, 0, 1])
rotation_6D_pose = np.array([0, 0, 0])
# Definition of vectors
origin = np.array([0, 0, 0])
vector_1 = np.array([0, 100, 10, 1])
vector_2 = np.array([-100, 0, 50, 1])
vector_3 = np.array([0, 100, 0, 1])
vector_4 = np.array([0, 0, 60, 1])
X, Y, Z = origin
U, V, W, _ = translation_6D_pose
A, B, C, _ = vector_1
D, E, F, _ = vector_2
G, H, I, _ = vector_3
J, K, L, _ = vector_4
# Plot figure:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
ax.set_xlim([-50, 200])
ax.set_ylim([-50, 200])
ax.set_zlim([-50, 200])
# Make plane:
xx, yy = np.meshgrid(range(-100, 100), range(0, 200))
z = xx*0 60
ax.plot_surface(xx, yy, z, alpha=0.2)
# Plot vectors:
ax.quiver(X, Y, Z, U, V, W)
ax.quiver(U, V, W, A, B, C)
ax.quiver(U A, V B, W C, D, E, F)
x=[U A k*D for k in range(10)]
y=[V B k*E for k in range(10)]
z=[W C k*F for k in range(10)]
ax.quiver(X, Y, Z, G, H, I, color="r")
ax.quiver(G, H, I, J, K, L, color="r")
#Computing intersection
plane = Plane(Point3D(0, 0, 60),(50,0,60),(0,50,60))
line = Line3D(Point3D(U A, V B, W C), Point3D(U A 10*D, V B 10*E, W C 10*F))
inter=plane.intersection(line)
#PLotting intersection
ax.scatter(float(inter[0][0]),float(inter[0][1]),float(inter[0][2]),color='tab:green',marker='x',s=100,label='intersection')
#PLotting line
ax.plot(x,y,z,label='line')
plt.legend()
plt.show()
輸出給出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/387298.html
標籤:Python 麻木的 matplotlib 向量 3d
下一篇:編輯csv列
