import{_ as e,c as t,j as s,a4 as a,o as i}from"./chunks/framework.DpC1ZpOZ.js";const z=JSON.parse('{"title":"mbcp.mp_math.vector","description":"","frontmatter":{"title":"mbcp.mp_math.vector","lastUpdated":false},"headers":[],"relativePath":"api/mp_math/vector.md","filePath":"zh/api/mp_math/vector.md"}'),l={name:"api/mp_math/vector.md"},h=a('
mbcp.mp_math.vector
本模块定义了3维向量的类Vector3,以及一些常用的向量。
Vector3
__init__(self, x: float, y: float, z: float)
说明: 3维向量
参数:
- x (
float
): x轴分量- y (
float
): y轴分量- z (
float
): z轴分量
def __init__(self, x: float, y: float, z: float):\n self.x = x\n self.y = y\n self.z = z
approx(self, other: Vector3, epsilon: float = APPROX) -> bool
说明: 判断两个向量是否近似相等。
参数:
返回: bool
: 是否近似相等
def approx(self, other: 'Vector3', epsilon: float=APPROX) -> bool:\n return all([abs(self.x - other.x) < epsilon, abs(self.y - other.y) < epsilon, abs(self.z - other.z) < epsilon])
cal_angle(self, other: Vector3) -> AnyAngle
说明: 计算两个向量之间的夹角。
',16),n={class:"tip custom-block"},r=s("p",{class:"custom-block-title"},"TIP",-1),p=s("p",null,"向量夹角计算公式:",-1),o={tabindex:"0",class:"MathJax",jax:"SVG",display:"true",style:{direction:"ltr",display:"block","text-align":"center",margin:"1em 0",position:"relative"}},d={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-2.17ex"},xmlns:"http://www.w3.org/2000/svg",width:"21.491ex",height:"5.206ex",role:"img",focusable:"false",viewBox:"0 -1342 9499 2301","aria-hidden":"true"},k=a('参数:
- other (
Vector3
): 另一个向量
返回: AnyAngle
: 夹角
def cal_angle(self, other: 'Vector3') -> 'AnyAngle':\n return AnyAngle(math.acos(self @ other / (self.length * other.length)), is_radian=True)
cross(self, other: Vector3) -> Vector3
说明: 向量积 叉乘:v1 x v2 -> v3
',6),m={class:"tip custom-block"},c=s("p",{class:"custom-block-title"},"TIP",-1),y=s("p",null,"叉乘运算法则为:",-1),E={tabindex:"0",class:"MathJax",jax:"SVG",display:"true",style:{direction:"ltr",display:"block","text-align":"center",margin:"1em 0",position:"relative"}},u={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-0.667ex"},xmlns:"http://www.w3.org/2000/svg",width:"70.883ex",height:"2.364ex",role:"img",focusable:"false",viewBox:"0 -750 31330.3 1045","aria-hidden":"true"},b=a('参数:
- other (
Vector3
): 另一个向量
返回: Vector3
: 叉乘结果
def cross(self, other: 'Vector3') -> 'Vector3':\n return Vector3(self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, self.x * other.y - self.y * other.x)
is_approx_parallel(self, other: Vector3, epsilon: float = APPROX) -> bool
说明: 判断两个向量是否近似平行。
参数:
返回: bool
: 是否近似平行
def is_approx_parallel(self, other: 'Vector3', epsilon: float=APPROX) -> bool:\n return self.cross(other).length < epsilon
is_parallel(self, other: Vector3) -> bool
说明: 判断两个向量是否平行。
参数:
- other (
Vector3
): 另一个向量
返回: bool
: 是否平行
def is_parallel(self, other: 'Vector3') -> bool:\n return self.cross(other).approx(zero_vector3)
normalize(self)
说明: 将向量归一化。
自体归一化,不返回值。
def normalize(self):\n length = self.length\n self.x /= length\n self.y /= length\n self.z /= length
np_array(self) -> np.ndarray
返回: np.ndarray
: numpy数组
@property\ndef np_array(self) -> 'np.ndarray':\n return np.array([self.x, self.y, self.z])
length(self) -> float
说明: 向量的模。
返回: float
: 模
@property\ndef length(self) -> float:\n return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
unit(self) -> Vector3
说明: 获取该向量的单位向量。
返回: Vector3
: 单位向量
@property\ndef unit(self) -> 'Vector3':\n return self / self.length
__abs__(self)
def __abs__(self):\n return self.length
@overload
self + other: Vector3 => Vector3
@overload\ndef __add__(self, other: 'Vector3') -> 'Vector3':\n ...
@overload
self + other: Point3 => Point3
@overload\ndef __add__(self, other: 'Point3') -> 'Point3':\n ...
self + other
说明: V + P -> P
V + V -> V
参数:
def __add__(self, other):\n if isinstance(other, Vector3):\n return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)\n elif isinstance(other, Point3):\n return Point3(self.x + other.x, self.y + other.y, self.z + other.z)\n else:\n raise TypeError(f"unsupported operand type(s) for +: 'Vector3' and '{type(other)}'")
self == other
说明: 判断两个向量是否相等。
参数:
- other (
Vector3
): 另一个向量
返回: bool
: 是否相等
def __eq__(self, other):\n return approx(self.x, other.x) and approx(self.y, other.y) and approx(self.z, other.z)
self + other: Point3 => Point3
说明: P + V -> P
别去点那边实现了。
参数:
- other (
Point3
): 另一个点
返回: Point3
: 新的点
def __radd__(self, other: 'Point3') -> 'Point3':\n return Point3(self.x + other.x, self.y + other.y, self.z + other.z)
@overload
self - other: Vector3 => Vector3
@overload\ndef __sub__(self, other: 'Vector3') -> 'Vector3':\n ...
@overload
self - other: Point3 => Point3
@overload\ndef __sub__(self, other: 'Point3') -> 'Point3':\n ...
self - other
说明: V - P -> P
V - V -> V
参数:
def __sub__(self, other):\n if isinstance(other, Vector3):\n return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)\n elif isinstance(other, Point3):\n return Point3(self.x - other.x, self.y - other.y, self.z - other.z)\n else:\n raise TypeError(f'unsupported operand type(s) for -: "Vector3" and "{type(other)}"')
self - other: Point3
说明: P - V -> P
参数:
- other (
Point3
): 另一个点
返回: Point3
: 新的点
def __rsub__(self, other: 'Point3'):\n if isinstance(other, Point3):\n return Point3(other.x - self.x, other.y - self.y, other.z - self.z)\n else:\n raise TypeError(f"unsupported operand type(s) for -: '{type(other)}' and 'Vector3'")
@overload
self * other: Vector3 => Vector3
@overload\ndef __mul__(self, other: 'Vector3') -> 'Vector3':\n ...
@overload
self * other: RealNumber => Vector3
@overload\ndef __mul__(self, other: RealNumber) -> 'Vector3':\n ...
self * other: int | float | Vector3 => Vector3
说明: 数组运算 非点乘。点乘使用@,叉乘使用cross。
参数:
返回: Vector3
: 数组运算结果
def __mul__(self, other: 'int | float | Vector3') -> 'Vector3':\n if isinstance(other, Vector3):\n return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)\n elif isinstance(other, (float, int)):\n return Vector3(self.x * other, self.y * other, self.z * other)\n else:\n raise TypeError(f"unsupported operand type(s) for *: 'Vector3' and '{type(other)}'")
self * other: RealNumber => Vector3
def __rmul__(self, other: 'RealNumber') -> 'Vector3':\n return self.__mul__(other)
self @ other: Vector3 => RealNumber
说明: 点乘。
参数:
- other (
Vector3
): 另一个向量
返回: float
: 点乘结果
def __matmul__(self, other: 'Vector3') -> 'RealNumber':\n return self.x * other.x + self.y * other.y + self.z * other.z
self / other: RealNumber => Vector3
def __truediv__(self, other: RealNumber) -> 'Vector3':\n return Vector3(self.x / other, self.y / other, self.z / other)
- self => Vector3
说明: 取负。
返回: Vector3
: 负向量
def __neg__(self) -> 'Vector3':\n return Vector3(-self.x, -self.y, -self.z)
zero_vector3
说明: 零向量
类型: Vector3
默认值: Vector3(0, 0, 0)
x_axis
说明: x轴单位向量
类型: Vector3
默认值: Vector3(1, 0, 0)
y_axis
说明: y轴单位向量
类型: Vector3
默认值: Vector3(0, 1, 0)
z_axis
说明: z轴单位向量
类型: Vector3
默认值: Vector3(0, 0, 1)