假设向量为v,平面法向量为n,则向量v在平面上的投影为:

proj = v - dot(v,n)*n/|n|^2

其中,dot(v,n)表示向量v和n的点积,|n|表示n的模长。

代码如下:

import numpy as np

def project_vector_onto_plane(v, n): # v: 1D numpy array representing the vector # n: 1D numpy array representing the normal vector of the plane dot_product = np.dot(v, n) norm_squared = np.dot(n, n) proj = v - dot_product*n/norm_squared return proj

Example usage

v = np.array([1, 2, 3]) n = np.array([0, 0, 1]) # the z-axis proj = project_vector_onto_plane(v, n) print(proj) # output: [1. 2. 0.

请用python实现一个向量到一个平面的投影

原文地址: https://www.cveoy.top/t/topic/cjsA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录