我正在尝试在原点(0,0,0)到特定点之间生成向量。如何将数学转换为代码?
编辑:
抱歉,不清楚。
假设我有一个位于3d坐标系中的点o(0,0,0)和点p(3,6,8)。
如何计算从点o到点p的方向的向量?
答案 0 :(得分:1)
您的问题尚不清楚,但这是一个如何绘制一些3D向量的示例
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
vectors=np.array( [ [0,0,1,1,-2,0], [0,0,2,1,1,0],[0,0,3,2,1,0],[0,0,4,0.5,0.7,0]])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for vector in vectors:
v = np.array([vector[3],vector[4],vector[5]])
vlength=np.linalg.norm(v)
ax.quiver(vector[0],vector[1],vector[2],vector[3],vector[4],vector[5],
pivot='tail',length=vlength,arrow_length_ratio=0.3/vlength)
ax.set_xlim([-4,4])
ax.set_ylim([-4,4])
ax.set_zlim([0,4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
答案 1 :(得分:0)
我和其他人一样不清楚。据我了解,您想要的只是一个向量,它将把您从一个点带到另一个点。这只是基本的向量加法,非常简单。我做到了,因此它适用于任何尺寸,而不仅仅是第三个尺寸。
def multiDimenDist(point1,point2):
#find the difference between the two points, its really the same as below
deltaVals = [point2[dimension]-point1[dimension] for dimension in range(len(point1))]
runningSquared = 0
#because the pythagarom theorm works for any dimension we can just use that
for coOrd in deltaVals:
runningSquared += coOrd**2
return runningSquared**(1/2)
def findVec(point1,point2,unitSphere = False):
#setting unitSphere to True will make the vector scaled down to a sphere with a radius one, instead of it's orginal length
finalVector = [0 for coOrd in point1]
for dimension, coOrd in enumerate(point1):
#finding total differnce for that co-ordinate(x,y,z...)
deltaCoOrd = point2[dimension]-coOrd
#adding total difference
finalVector[dimension] = deltaCoOrd
if unitSphere:
totalDist = multiDimenDist(point1,point2)
unitVector =[]
for dimen in finalVector:
unitVector.append( dimen/totalDist)
return unitVector
else:
return finalVector
编辑,忘记了用法:
vector = findVec([0,0,0],[3,6,8])# you can also set unitCircle to True
>>[3,6,8]