Find perpendicular to given vector (Velocity) in 3D <python>
Question:
I have a object A move with Velocity (v1, v2, v3) in 3D space.
Object position is (px,py,pz)
Now i want to add certain particles around object A (in radius dis) on plane which perpendicular to its Velocity direction.
I find something call “cross product” but seen that no use in this case.
Anyone can help?
I’m new to python and don’t really know how to crack it.
Answers:
The plane perpendicular to a vector ⟨A, B, C⟩ has the general equation Ax + By + Cz + K = 0.
The equation of the plane is:
v1*(x-px) + v2*(y-py) + v3*(z-pz) = 0
When you know (x,y)
you can find z
and so on.
Example:
z = pz – (v1*(x-px) + v2*(y-py))/v3
Lets say we have a point p1, and we want to build a circle of points around it with radius r so that all points on the circle are orthogonal to a vector n.. here is a working example
p1 = np.array([-21.03181359, 4.54876345, 19.26943601])
n = np.array([-0.06592715, 0.00713031, -0.26809672])
n = n / np.linalg.norm(n) # normalise n
r = 0.5
x = np.array([1,0,0]).astype(np.float64) # take a random vector of magnitude 1
x -= x.dot(n) * n / np.linalg.norm(n)**2 # make it orthogonal to n
x /= np.linalg.norm(x) # normalize
# find first point on circle (x1).
# currently it has magnitude of 1, so we multiply it by the r
x1 = p1 + (x*r)
# vector from lumen centre to first circle point
p1x1 = x1 - p1
def rotation_matrix(axis, theta):
"""
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
"""
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2.0)
b, c, d = -axis * math.sin(theta / 2.0)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
# rotate the vector p1x1 around the axis n with angle theta
circle = []
for theta in range(0,360,6):
circle_i = np.dot(rotation_matrix(n, np.deg2rad(theta)), p1x1)
circle.append(circle_i+p1)
ax = axes3d.Axes3D(plt.figure(figsize=(10,10)))
ax.scatter3D(*np.array(circle).T, s=10, c='red')
ax.scatter3D(*p1.T, s=10, c='black')
ax.set_xlabel('X', size=40)
ax.set_ylabel('Y', size=40)
ax.set_zlabel('Z', size=40)
ax.set_xlim(-19,-22)
ax.set_ylim(2,5)
ax.set_zlim(18,21)
I have a object A move with Velocity (v1, v2, v3) in 3D space.
Object position is (px,py,pz)
Now i want to add certain particles around object A (in radius dis) on plane which perpendicular to its Velocity direction.
I find something call “cross product” but seen that no use in this case.
Anyone can help?
I’m new to python and don’t really know how to crack it.
The plane perpendicular to a vector ⟨A, B, C⟩ has the general equation Ax + By + Cz + K = 0.
The equation of the plane is:
v1*(x-px) + v2*(y-py) + v3*(z-pz) = 0
When you know (x,y)
you can find z
and so on.
Example:
z = pz – (v1*(x-px) + v2*(y-py))/v3
Lets say we have a point p1, and we want to build a circle of points around it with radius r so that all points on the circle are orthogonal to a vector n.. here is a working example
p1 = np.array([-21.03181359, 4.54876345, 19.26943601])
n = np.array([-0.06592715, 0.00713031, -0.26809672])
n = n / np.linalg.norm(n) # normalise n
r = 0.5
x = np.array([1,0,0]).astype(np.float64) # take a random vector of magnitude 1
x -= x.dot(n) * n / np.linalg.norm(n)**2 # make it orthogonal to n
x /= np.linalg.norm(x) # normalize
# find first point on circle (x1).
# currently it has magnitude of 1, so we multiply it by the r
x1 = p1 + (x*r)
# vector from lumen centre to first circle point
p1x1 = x1 - p1
def rotation_matrix(axis, theta):
"""
Return the rotation matrix associated with counterclockwise rotation about
the given axis by theta radians.
"""
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2.0)
b, c, d = -axis * math.sin(theta / 2.0)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array([[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc]])
# rotate the vector p1x1 around the axis n with angle theta
circle = []
for theta in range(0,360,6):
circle_i = np.dot(rotation_matrix(n, np.deg2rad(theta)), p1x1)
circle.append(circle_i+p1)
ax = axes3d.Axes3D(plt.figure(figsize=(10,10)))
ax.scatter3D(*np.array(circle).T, s=10, c='red')
ax.scatter3D(*p1.T, s=10, c='black')
ax.set_xlabel('X', size=40)
ax.set_ylabel('Y', size=40)
ax.set_zlabel('Z', size=40)
ax.set_xlim(-19,-22)
ax.set_ylim(2,5)
ax.set_zlim(18,21)