Skip to content

Module mbcp.mp_math.vector

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

class Vector3

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

Description: 3维向量

Arguments:

  • x (float): x轴分量
  • y (float): y轴分量
  • z (float): z轴分量
Source code or View on GitHub
python
def __init__(self, x: float, y: float, z: float):
    self.x = x
    self.y = y
    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:
    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: 计算两个向量之间的夹角。

TIP

向量夹角计算公式:

θ=arccos(v1v2|v1||v2|)

Arguments:

Return: AnyAngle: 夹角

Source code or View on GitHub
python
def cal_angle(self, other: 'Vector3') -> '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

TIP

叉乘运算法则为:

v1×v2=(v1yv2zv1zv2y,v1zv2xv1xv2z,v1xv2yv1yv2x)

转换为行列式形式:

v1×v2=|ijkv1xv1yv1zv2xv2yv2z|

Arguments:

Return: Vector3: 叉乘结果

Source code or View on GitHub
python
def cross(self, other: 'Vector3') -> '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:
    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:
    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

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

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

TIP

投影向量计算公式,projv(u)表示向量u在向量v上的投影向量:

projv(u)=uv|v|2v

Arguments:

Return: Vector3: 投影向量

Source code or View on GitHub
python
def project(self, other: 'Vector3') -> 'Vector3':
    return self @ other / other.length * other.unit

@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':
    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:
    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':
    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):
    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

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

Arguments:

Return: bool: 是否相等

Source code or View on GitHub
python
def __eq__(self, other):
    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':
    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):
    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'):
    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':
    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':
    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':
    return Vector3(-self.x, -self.y, -self.z)

var zero_vector3

  • Description: 零向量

  • Type: Vector3

  • Default: Vector3(0, 0, 0)

var x_axis

  • Description: x轴单位向量

  • Type: Vector3

  • Default: Vector3(1, 0, 0)

var y_axis

  • Description: y轴单位向量

  • Type: Vector3

  • Default: Vector3(0, 1, 0)

var z_axis

  • Description: z轴单位向量

  • Type: Vector3

  • Default: Vector3(0, 0, 1)

Documentation built with VitePress | API references generated by litedoc