import{_ as n,c as i,j as s,a4 as a,o as t}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":"en/api/mp_math/vector.md","filePath":"en/api/mp_math/vector.md"}'),e={name:"en/api/mp_math/vector.md"},l=a('

Module mbcp.mp_math.vector

本模块定义了3维向量的类Vector3,以及一些常用的向量。

class Vector3

method __init__(self, x: float, y: float, z: float)

Description: 3维向量

Arguments:

Source code or View on GitHub
python
def __init__(self, x: float, y: float, z: float):\n    """\n        3维向量\n        Args:\n            x ([`float`](https%3A//docs.python.org/3/library/functions.html#float)): x轴分量\n            y (`float`): y轴分量\n            z (`float`): z轴分量\n        """\n    self.x = x\n    self.y = y\n    self.z = z

method approx(self, other: Vector3, epsilon: float = APPROX) -> bool

Description: 判断两个向量是否近似相等。

Arguments:

Return: bool: 是否近似相等

Source code or View on GitHub
python
def approx(self, other: 'Vector3', epsilon: float=APPROX) -> bool:\n    """\n        判断两个向量是否近似相等。\n        Args:\n            other ([`Vector3`](#class-vector3)): 另一个向量\n            epsilon ([`float`](https%3A//docs.python.org/3/library/functions.html#float)): 误差\n\n        Returns:\n            [`bool`](https%3A//docs.python.org/3/library/functions.html#bool): 是否近似相等\n        """\n    return all([abs(self.x - other.x) < epsilon, abs(self.y - other.y) < epsilon, abs(self.z - other.z) < epsilon])

method cal_angle(self, other: Vector3) -> AnyAngle

Description: 计算两个向量之间的夹角。

',16),h={class:"tip custom-block"},p=s("p",{class:"custom-block-title"},"TIP",-1),r=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"}},k={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"},d=a('',1),T=[d],Q=s("mjx-assistive-mml",{unselectable:"on",display:"block",style:{top:"0px",left:"0px",clip:"rect(1px, 1px, 1px, 1px)","-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","user-select":"none",position:"absolute",padding:"1px 0px 0px 0px",border:"0px",display:"block",overflow:"hidden",width:"100%"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block"},[s("mi",null,"θ"),s("mo",null,"="),s("mi",null,"arccos"),s("mo",{"data-mjx-texclass":"NONE"},"⁡"),s("mo",{stretchy:"false"},"("),s("mfrac",null,[s("mrow",null,[s("mi",null,"v"),s("mn",null,"1"),s("mo",null,"⋅"),s("mi",null,"v"),s("mn",null,"2")]),s("mrow",null,[s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|"),s("mi",null,"v"),s("mn",null,"1"),s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|"),s("mo",null,"⋅"),s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|"),s("mi",null,"v"),s("mn",null,"2"),s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|")])]),s("mo",{stretchy:"false"},")")])],-1),g=a(`

Arguments:

Return: AnyAngle: 夹角

Source code or View on GitHub
python
def cal_angle(self, other: 'Vector3') -> 'AnyAngle':
    """
        计算两个向量之间的夹角。
        :::tip
        向量夹角计算公式:
        $$\\\\theta = \\\\arccos(\\\\frac{v1 \\\\cdot v2}{|v1| \\\\cdot |v2|})$$
        :::
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
        Returns:
            [\`AnyAngle\`](./angle#class-anyangle): 夹角
        """
    return AnyAngle(math.acos(self @ other / (self.length * other.length)), is_radian=True)

method cross(self, other: Vector3) -> Vector3

Description: 向量积 叉乘:v1 x v2 -> v3

`,6),c={class:"tip custom-block"},m=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"}},F={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"},u=a('',1),b=[u],f=s("mjx-assistive-mml",{unselectable:"on",display:"block",style:{top:"0px",left:"0px",clip:"rect(1px, 1px, 1px, 1px)","-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","user-select":"none",position:"absolute",padding:"1px 0px 0px 0px",border:"0px",display:"block",overflow:"hidden",width:"100%"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block"},[s("mi",null,"v"),s("mn",null,"1"),s("mo",null,"×"),s("mi",null,"v"),s("mn",null,"2"),s("mo",null,"="),s("mo",{stretchy:"false"},"("),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"y")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"z")]),s("mo",null,"−"),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"z")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"y")]),s("mo",null,","),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"z")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"x")]),s("mo",null,"−"),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"x")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"z")]),s("mo",null,","),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"x")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"y")]),s("mo",null,"−"),s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"y")]),s("mo",null,"⋅"),s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"x")]),s("mo",{stretchy:"false"},")")])],-1),C=s("p",null,"转换为行列式形式:",-1),v={tabindex:"0",class:"MathJax",jax:"SVG",display:"true",style:{direction:"ltr",display:"block","text-align":"center",margin:"1em 0",position:"relative"}},B={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-3.835ex"},xmlns:"http://www.w3.org/2000/svg",width:"25.963ex",height:"8.801ex",role:"img",focusable:"false",viewBox:"0 -2195 11475.8 3889.9","aria-hidden":"true"},_=a('',1),V=[_],H=s("mjx-assistive-mml",{unselectable:"on",display:"block",style:{top:"0px",left:"0px",clip:"rect(1px, 1px, 1px, 1px)","-webkit-touch-callout":"none","-webkit-user-select":"none","-khtml-user-select":"none","-moz-user-select":"none","-ms-user-select":"none","user-select":"none",position:"absolute",padding:"1px 0px 0px 0px",border:"0px",display:"block",overflow:"hidden",width:"100%"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block"},[s("mi",null,"v"),s("mn",null,"1"),s("mo",null,"×"),s("mi",null,"v"),s("mn",null,"2"),s("mo",null,"="),s("mrow",{"data-mjx-texclass":"INNER"},[s("mo",{"data-mjx-texclass":"OPEN"},"|"),s("mtable",{columnspacing:"1em",rowspacing:"4pt"},[s("mtr",null,[s("mtd",null,[s("mi",null,"i")]),s("mtd",null,[s("mi",null,"j")]),s("mtd",null,[s("mi",null,"k")])]),s("mtr",null,[s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"x")])]),s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"y")])]),s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"1"),s("mi",null,"z")])])]),s("mtr",null,[s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"x")])]),s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"y")])]),s("mtd",null,[s("mi",null,"v"),s("msub",null,[s("mn",null,"2"),s("mi",null,"z")])])])]),s("mo",{"data-mjx-texclass":"CLOSE"},"|")])])],-1),x=a(`

Arguments:

Return: Vector3: 叉乘结果

Source code or View on GitHub
python
def cross(self, other: 'Vector3') -> 'Vector3':
    """
        向量积 叉乘:v1 x v2 -> v3

        :::tip
        叉乘运算法则为:
        $$ v1 \\\\times v2 = (v1_y \\\\cdot v2_z - v1_z \\\\cdot v2_y, v1_z \\\\cdot v2_x - v1_x \\\\cdot v2_z, v1_x \\\\cdot v2_y - v1_y \\\\cdot v2_x) $$
        转换为行列式形式:
        $$ v1 \\\\times v2 = \\\\begin{vmatrix} i & j & k \\\\\\\\ v1_x & v1_y & v1_z \\\\\\\\ v2_x & v2_y & v2_z \\\\end{vmatrix} $$
        :::
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
        Returns:
            [\`Vector3\`](#class-vector3): 叉乘结果
        """
    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)

method is_approx_parallel(self, other: Vector3, epsilon: float = APPROX) -> bool

Description: 判断两个向量是否近似平行。

Arguments:

Return: bool: 是否近似平行

Source code or View on GitHub
python
def is_approx_parallel(self, other: 'Vector3', epsilon: float=APPROX) -> bool:
    """
        判断两个向量是否近似平行。
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
            epsilon ([\`float\`](https%3A//docs.python.org/3/library/functions.html#float)): 误差
        Returns:
            [\`bool\`](https%3A//docs.python.org/3/library/functions.html#bool): 是否近似平行
        """
    return self.cross(other).length < epsilon

method is_parallel(self, other: Vector3) -> bool

Description: 判断两个向量是否平行。

Arguments:

Return: bool: 是否平行

Source code or View on GitHub
python
def is_parallel(self, other: 'Vector3') -> bool:
    """
        判断两个向量是否平行。
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
        Returns:
            [\`bool\`](https%3A//docs.python.org/3/library/functions.html#bool): 是否平行
        """
    return self.cross(other).approx(zero_vector3)

method normalize(self)

Description: 将向量归一化。

自体归一化,不返回值。

Source code or View on GitHub
python
def normalize(self):
    """
        将向量归一化。

        自体归一化,不返回值。
        """
    length = self.length
    self.x /= length
    self.y /= length
    self.z /= length

@property

method np_array(self) -> np.ndarray

Return: np.ndarray: numpy数组

Source code or View on GitHub
python
@property
def np_array(self) -> 'np.ndarray':
    """
        返回numpy数组
        Returns:
            [\`np.ndarray\`](https%3A//numpy.org/doc/stable/reference/generated/numpy.ndarray.html): numpy数组
        """
    return np.array([self.x, self.y, self.z])

@property

method length(self) -> float

Description: 向量的模。

Return: float: 模

Source code or View on GitHub
python
@property
def length(self) -> float:
    """
        向量的模。
        Returns:
            [\`float\`](https%3A//docs.python.org/3/library/functions.html#float): 模
        """
    return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)

@property

method unit(self) -> Vector3

Description: 获取该向量的单位向量。

Return: Vector3: 单位向量

Source code or View on GitHub
python
@property
def unit(self) -> 'Vector3':
    """
        获取该向量的单位向量。
        Returns:
            [\`Vector3\`](#class-vector3): 单位向量
        """
    return self / self.length

method __abs__(self)

Source code or View on GitHub
python
def __abs__(self):
    return self.length

@overload

method self + other: Vector3 => Vector3

Source code or View on GitHub
python
@overload
def __add__(self, other: 'Vector3') -> 'Vector3':
    ...

@overload

method self + other: Point3 => Point3

Source code or View on GitHub
python
@overload
def __add__(self, other: 'Point3') -> 'Point3':
    ...

method self + other

Description: V + P -> P

V + V -> V

Arguments:

Return: Vector3 | Point3: 新的向量或点

Source code or View on GitHub
python
def __add__(self, other):
    """
        V + P -> P

        V + V -> V
        Args:
            other ([\`Vector3\`](#class-vector3) | [\`Point3\`](./point#class-point3)): 另一个向量或点
        Returns:
            [\`Vector3\`](#class-vector3) | [\`Point3\`](./point#class-point3): 新的向量或点
        """
    if isinstance(other, Vector3):
        return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
    elif isinstance(other, Point3):
        return Point3(self.x + other.x, self.y + other.y, self.z + other.z)
    else:
        raise TypeError(f"unsupported operand type(s) for +: 'Vector3' and '{type(other)}'")

method __eq__(self, other)

Description: 判断两个向量是否相等。

Arguments:

Return: bool: 是否相等

Source code or View on GitHub
python
def __eq__(self, other):
    """
        判断两个向量是否相等。
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
        Returns:
            [\`bool\`](https%3A//docs.python.org/3/library/functions.html#bool): 是否相等
        """
    return approx(self.x, other.x) and approx(self.y, other.y) and approx(self.z, other.z)

method self + other: Point3 => Point3

Description: P + V -> P

别去点那边实现了。

Arguments:

Return: Point3: 新的点

Source code or View on GitHub
python
def __radd__(self, other: 'Point3') -> 'Point3':
    """
        P + V -> P

        别去点那边实现了。
        Args:
            other ([\`Point3\`](./point#class-point3)): 另一个点
        Returns:
            [\`Point3\`](./point#class-point3): 新的点
        """
    return Point3(self.x + other.x, self.y + other.y, self.z + other.z)

@overload

method self - other: Vector3 => Vector3

Source code or View on GitHub
python
@overload
def __sub__(self, other: 'Vector3') -> 'Vector3':
    ...

@overload

method self - other: Point3 => Point3

Source code or View on GitHub
python
@overload
def __sub__(self, other: 'Point3') -> 'Point3':
    ...

method self - other

Description: V - P -> P

V - V -> V

Arguments:

Return: Vector3 | Point3: 新的向量

Source code or View on GitHub
python
def __sub__(self, other):
    """
        V - P -> P

        V - V -> V
        Args:
            other ([\`Vector3\`](#class-vector3) | [\`Point3\`](./point#class-point3)): 另一个向量或点
        Returns:
            [\`Vector3\`](#class-vector3) | [\`Point3\`](./point#class-point3): 新的向量
        """
    if isinstance(other, Vector3):
        return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
    elif isinstance(other, Point3):
        return Point3(self.x - other.x, self.y - other.y, self.z - other.z)
    else:
        raise TypeError(f'unsupported operand type(s) for -: "Vector3" and "{type(other)}"')

method self - other: Point3

Description: P - V -> P

Arguments:

Return: Point3: 新的点

Source code or View on GitHub
python
def __rsub__(self, other: 'Point3'):
    """
        P - V -> P
        Args:
            other ([\`Point3\`](./point#class-point3)): 另一个点
        Returns:
            [\`Point3\`](./point#class-point3): 新的点
        """
    if isinstance(other, Point3):
        return Point3(other.x - self.x, other.y - self.y, other.z - self.z)
    else:
        raise TypeError(f"unsupported operand type(s) for -: '{type(other)}' and 'Vector3'")

@overload

method self * other: Vector3 => Vector3

Source code or View on GitHub
python
@overload
def __mul__(self, other: 'Vector3') -> 'Vector3':
    ...

@overload

method self * other: RealNumber => Vector3

Source code or View on GitHub
python
@overload
def __mul__(self, other: RealNumber) -> 'Vector3':
    ...

method self * other: int | float | Vector3 => Vector3

Description: 数组运算 非点乘。点乘使用@,叉乘使用cross。

Arguments:

Return: Vector3: 数组运算结果

Source code or View on GitHub
python
def __mul__(self, other: 'int | float | Vector3') -> 'Vector3':
    """
        数组运算 非点乘。点乘使用@,叉乘使用cross。
        Args:
            other ([\`Vector3\`](#class-vector3) | [\`float\`](https%3A//docs.python.org/3/library/functions.html#float)): 另一个向量或数
        Returns:
            [\`Vector3\`](#class-vector): 数组运算结果
        """
    if isinstance(other, Vector3):
        return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
    elif isinstance(other, (float, int)):
        return Vector3(self.x * other, self.y * other, self.z * other)
    else:
        raise TypeError(f"unsupported operand type(s) for *: 'Vector3' and '{type(other)}'")

method self * other: RealNumber => Vector3

Source code or View on GitHub
python
def __rmul__(self, other: 'RealNumber') -> 'Vector3':
    return self.__mul__(other)

method self @ other: Vector3 => RealNumber

Description: 点乘。

Arguments:

Return: float: 点乘结果

Source code or View on GitHub
python
def __matmul__(self, other: 'Vector3') -> 'RealNumber':
    """
        点乘。
        Args:
            other ([\`Vector3\`](#class-vector3)): 另一个向量
        Returns:
            [\`float\`](https%3A//docs.python.org/3/library/functions.html#float): 点乘结果
        """
    return self.x * other.x + self.y * other.y + self.z * other.z

method self / other: RealNumber => Vector3

Source code or View on GitHub
python
def __truediv__(self, other: RealNumber) -> 'Vector3':
    return Vector3(self.x / other, self.y / other, self.z / other)

method - self => Vector3

Description: 取负。

Return: Vector3: 负向量

Source code or View on GitHub
python
def __neg__(self) -> 'Vector3':
    """
        取负。
        Returns:
            [\`Vector3\`](#class-vector3): 负向量
        """
    return Vector3(-self.x, -self.y, -self.z)

var zero_vector3

var x_axis

var y_axis

var z_axis

`,115);function D(w,q,A,L,M,P){return t(),i("div",null,[l,s("div",h,[p,r,s("mjx-container",o,[(t(),i("svg",k,T)),Q])]),g,s("div",c,[m,y,s("mjx-container",E,[(t(),i("svg",F,b)),f]),C,s("mjx-container",v,[(t(),i("svg",B,V)),H])]),x])}const R=n(e,[["render",D]]);export{z as __pageData,R as default};