import{_ as l,c as a,j as s,a as e,a4 as t,o as i}from"./chunks/framework.DpC1ZpOZ.js";const o1=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"}'),n={name:"api/mp_math/vector.md"},h=t('

模块 mbcp.mp_math.vector

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

class Vector3


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

说明: 3维向量

参数:

源代码在GitHub上查看
python
def __init__(self, x: float, y: float, z: float):\n    self.x = x\n    self.y = y\n    self.z = z

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

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

参数:

返回: bool: 是否近似相等

源代码在GitHub上查看
python
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])

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

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

',19),r={class:"tip custom-block"},o=s("p",{class:"custom-block-title"},"TIP",-1),p=s("p",null,"向量夹角计算公式:",-1),T={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=t('',1),Q=[k],m=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=t('

参数:

返回: AnyAngle: 夹角

源代码在GitHub上查看
python
def cal_angle(self, other: 'Vector3') -> 'AnyAngle':\n    return AnyAngle(math.acos(self @ other / (self.length * other.length)), is_radian=True)

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

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

',7),c={class:"tip custom-block"},y=s("p",{class:"custom-block-title"},"TIP",-1),E=s("p",null,"叉乘运算法则为:",-1),u={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"},b=t('',1),F=[b],C=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),_=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"}},x={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"},H=t('',1),V=[H],w=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),D=t('

参数:

返回: Vector3: 叉乘结果

源代码在GitHub上查看
python
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)

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

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

参数:

返回: bool: 是否近似平行

源代码在GitHub上查看
python
def is_approx_parallel(self, other: 'Vector3', epsilon: float=APPROX) -> bool:\n    return self.cross(other).length < epsilon

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

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

参数:

返回: bool: 是否平行

源代码在GitHub上查看
python
def is_parallel(self, other: 'Vector3') -> bool:\n    return self.cross(other).approx(zero_vector3)

method normalize(self)

说明: 将向量归一化。

自体归一化,不返回值。

源代码在GitHub上查看
python
def normalize(self):\n    length = self.length\n    self.x /= length\n    self.y /= length\n    self.z /= length

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

说明: 计算自向量在另一个向量上的投影向量。

',26),L={class:"tip custom-block"},B=s("p",{class:"custom-block-title"},"TIP",-1),A={class:"MathJax",jax:"SVG",style:{direction:"ltr",position:"relative"}},M={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-0.566ex"},xmlns:"http://www.w3.org/2000/svg",width:"8.206ex",height:"2.262ex",role:"img",focusable:"false",viewBox:"0 -750 3626.9 1000","aria-hidden":"true"},Z=t('',1),P=[Z],q=s("mjx-assistive-mml",{unselectable:"on",display:"inline",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",width:"auto",overflow:"hidden"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("mi",null,"p"),s("mi",null,"r"),s("mi",null,"o"),s("msub",null,[s("mi",null,"j"),s("mi",null,"v")]),s("mo",{stretchy:"false"},"("),s("mi",null,"u"),s("mo",{stretchy:"false"},")")])],-1),z={class:"MathJax",jax:"SVG",style:{direction:"ltr",position:"relative"}},j={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-0.025ex"},xmlns:"http://www.w3.org/2000/svg",width:"1.294ex",height:"1.025ex",role:"img",focusable:"false",viewBox:"0 -442 572 453","aria-hidden":"true"},S=s("g",{stroke:"currentColor",fill:"currentColor","stroke-width":"0",transform:"scale(1,-1)"},[s("g",{"data-mml-node":"math"},[s("g",{"data-mml-node":"mi"},[s("path",{"data-c":"1D462",d:"M21 287Q21 295 30 318T55 370T99 420T158 442Q204 442 227 417T250 358Q250 340 216 246T182 105Q182 62 196 45T238 27T291 44T328 78L339 95Q341 99 377 247Q407 367 413 387T427 416Q444 431 463 431Q480 431 488 421T496 402L420 84Q419 79 419 68Q419 43 426 35T447 26Q469 29 482 57T512 145Q514 153 532 153Q551 153 551 144Q550 139 549 130T540 98T523 55T498 17T462 -8Q454 -10 438 -10Q372 -10 347 46Q345 45 336 36T318 21T296 6T267 -6T233 -11Q189 -11 155 7Q103 38 103 113Q103 170 138 262T173 379Q173 380 173 381Q173 390 173 393T169 400T158 404H154Q131 404 112 385T82 344T65 302T57 280Q55 278 41 278H27Q21 284 21 287Z",style:{"stroke-width":"3"}})])])],-1),G=[S],R=s("mjx-assistive-mml",{unselectable:"on",display:"inline",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",width:"auto",overflow:"hidden"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("mi",null,"u")])],-1),N={class:"MathJax",jax:"SVG",style:{direction:"ltr",position:"relative"}},I={style:{overflow:"visible","min-height":"1px","min-width":"1px","vertical-align":"-0.025ex"},xmlns:"http://www.w3.org/2000/svg",width:"1.097ex",height:"1.027ex",role:"img",focusable:"false",viewBox:"0 -443 485 454","aria-hidden":"true"},O=s("g",{stroke:"currentColor",fill:"currentColor","stroke-width":"0",transform:"scale(1,-1)"},[s("g",{"data-mml-node":"math"},[s("g",{"data-mml-node":"mi"},[s("path",{"data-c":"1D463",d:"M173 380Q173 405 154 405Q130 405 104 376T61 287Q60 286 59 284T58 281T56 279T53 278T49 278T41 278H27Q21 284 21 287Q21 294 29 316T53 368T97 419T160 441Q202 441 225 417T249 361Q249 344 246 335Q246 329 231 291T200 202T182 113Q182 86 187 69Q200 26 250 26Q287 26 319 60T369 139T398 222T409 277Q409 300 401 317T383 343T365 361T357 383Q357 405 376 424T417 443Q436 443 451 425T467 367Q467 340 455 284T418 159T347 40T241 -11Q177 -11 139 22Q102 54 102 117Q102 148 110 181T151 298Q173 362 173 380Z",style:{"stroke-width":"3"}})])])],-1),J=[O],X=s("mjx-assistive-mml",{unselectable:"on",display:"inline",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",width:"auto",overflow:"hidden"}},[s("math",{xmlns:"http://www.w3.org/1998/Math/MathML"},[s("mi",null,"v")])],-1),$={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":"-2.193ex"},xmlns:"http://www.w3.org/2000/svg",width:"18.757ex",height:"4.725ex",role:"img",focusable:"false",viewBox:"0 -1119 8290.4 2088.4","aria-hidden":"true"},K=t('',1),W=[K],Y=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("msub",null,[s("mtext",null,"proj"),s("mi",null,"v")]),s("mo",{stretchy:"false"},"("),s("mi",null,"u"),s("mo",{stretchy:"false"},")"),s("mo",null,"="),s("mfrac",null,[s("mrow",null,[s("mi",null,"u"),s("mo",null,"⋅"),s("mi",null,"v")]),s("mrow",null,[s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|"),s("mi",null,"v"),s("msup",null,[s("mo",{"data-mjx-texclass":"ORD",stretchy:"false"},"|"),s("mn",null,"2")])])]),s("mo",null,"⋅"),s("mi",null,"v")])],-1),s1=t('

参数:

返回: Vector3: 投影向量

源代码在GitHub上查看
python
def project(self, other: 'Vector3') -> 'Vector3':\n    return self @ other / other.length * other.unit

@property

method np_array(self) -> np.ndarray

返回: np.ndarray: numpy数组

源代码在GitHub上查看
python
@property\ndef np_array(self) -> 'np.ndarray':\n    return np.array([self.x, self.y, self.z])

@property

method length(self) -> float

说明: 向量的模。

返回: float: 模

源代码在GitHub上查看
python
@property\ndef length(self) -> float:\n    return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)

@property

method unit(self) -> Vector3

说明: 获取该向量的单位向量。

返回: Vector3: 单位向量

源代码在GitHub上查看
python
@property\ndef unit(self) -> 'Vector3':\n    return self / self.length

method __abs__(self)

源代码在GitHub上查看
python
def __abs__(self):\n    return self.length

@overload

method self + other: Vector3 => Vector3

源代码在GitHub上查看
python
@overload\ndef __add__(self, other: 'Vector3') -> 'Vector3':\n    ...

@overload

method self + other: Point3 => Point3

源代码在GitHub上查看
python
@overload\ndef __add__(self, other: 'Point3') -> 'Point3':\n    ...

method self + other

说明: V + P -> P

V + V -> V

参数:

返回: Vector3 | Point3: 新的向量或点

源代码在GitHub上查看
python
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)}'")

method self == other

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

参数:

返回: bool: 是否相等

源代码在GitHub上查看
python
def __eq__(self, other):\n    return approx(self.x, other.x) and approx(self.y, other.y) and approx(self.z, other.z)

method self + other: Point3 => Point3

说明: P + V -> P

别去点那边实现了。

参数:

返回: Point3: 新的点

源代码在GitHub上查看
python
def __radd__(self, other: 'Point3') -> 'Point3':\n    return Point3(self.x + other.x, self.y + other.y, self.z + other.z)

@overload

method self - other: Vector3 => Vector3

源代码在GitHub上查看
python
@overload\ndef __sub__(self, other: 'Vector3') -> 'Vector3':\n    ...

@overload

method self - other: Point3 => Point3

源代码在GitHub上查看
python
@overload\ndef __sub__(self, other: 'Point3') -> 'Point3':\n    ...

method self - other

说明: V - P -> P

V - V -> V

参数:

返回: Vector3 | Point3: 新的向量

源代码在GitHub上查看
python
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)}"')

method self - other: Point3

说明: P - V -> P

参数:

返回: Point3: 新的点

源代码在GitHub上查看
python
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

method self * other: Vector3 => Vector3

源代码在GitHub上查看
python
@overload\ndef __mul__(self, other: 'Vector3') -> 'Vector3':\n    ...

@overload

method self * other: RealNumber => Vector3

源代码在GitHub上查看
python
@overload\ndef __mul__(self, other: RealNumber) -> 'Vector3':\n    ...

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

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

参数:

返回: Vector3: 数组运算结果

源代码在GitHub上查看
python
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)}'")

method self * other: RealNumber => Vector3

源代码在GitHub上查看
python
def __rmul__(self, other: 'RealNumber') -> 'Vector3':\n    return self.__mul__(other)

method self @ other: Vector3 => RealNumber

说明: 点乘。

参数:

返回: float: 点乘结果

源代码在GitHub上查看
python
def __matmul__(self, other: 'Vector3') -> 'RealNumber':\n    return self.x * other.x + self.y * other.y + self.z * other.z

method self / other: RealNumber => Vector3

源代码在GitHub上查看
python
def __truediv__(self, other: RealNumber) -> 'Vector3':\n    return Vector3(self.x / other, self.y / other, self.z / other)

method - self => Vector3

说明: 取负。

返回: Vector3: 负向量

源代码在GitHub上查看
python
def __neg__(self) -> 'Vector3':\n    return Vector3(-self.x, -self.y, -self.z)

var zero_vector3

var x_axis

var y_axis

var z_axis

',119);function t1(a1,i1,e1,l1,n1,h1){return i(),a("div",null,[h,s("div",r,[o,p,s("mjx-container",T,[(i(),a("svg",d,Q)),m])]),g,s("div",c,[y,E,s("mjx-container",u,[(i(),a("svg",f,F)),C]),_,s("mjx-container",v,[(i(),a("svg",x,V)),w])]),D,s("div",L,[B,s("p",null,[e("投影向量计算公式,"),s("mjx-container",A,[(i(),a("svg",M,P)),q]),e("表示向量"),s("mjx-container",z,[(i(),a("svg",j,G)),R]),e("在向量"),s("mjx-container",N,[(i(),a("svg",I,J)),X]),e("上的投影向量:")]),s("mjx-container",$,[(i(),a("svg",U,W)),Y])]),s1])}const p1=l(n,[["render",t1]]);export{o1 as __pageData,p1 as default};