manimlib/utils/ 这个文件中主要实现了和空间坐标计算有关的函数

manimlib.utils.space_ops.get_norm(vect: Iterable) → float

返回向量 vect 的模长

manimlib.utils.space_ops.quaternion_mult(*quats: Sequence[float]) → list[float]

返回两个 四元数 q1, q2 相乘的乘积

manimlib.utils.space_ops.quaternion_from_angle_axis(angle: float, axis: np.ndarray) → list[float]

根据 轴-角 确定用于旋转的 四元数 返回[cos(angle/2), sin(angle/2)*axis]

manimlib.utils.space_ops.angle_axis_from_quaternion(quat: Sequence[float]) → tuple[float, np.ndarray]


manimlib.utils.space_ops.quaternion_conjugate(quaternion: Iterable) → list

返回 quaternion 的共轭四元数

manimlib.utils.space_ops.rotate_vector(vector: Iterable, angle: float, axis: np.ndarray = array([0., 0., 1.])) → np.ndarray | list[float]


  • 若 vector 是二维 ndarray,则使用复数运算

  • 若 vector 是三维 ndarray,则使用四元数运算

manimlib.utils.space_ops.thick_diagonal(dim: int, thickness: int = 2) → numpy.ndarray

返回一个 dim*dim 大小,对角线宽度为 thickness 的方阵

manimlib.utils.space_ops.rotation_matrix_transpose_from_quaternion(quat: Iterable) → np.ndarray


manimlib.utils.space_ops.rotation_matrix_from_quaternion(quat: Iterable) → np.ndarray


manimlib.utils.space_ops.rotation_matrix_transpose(angle: float, axis: numpy.ndarray) → numpy.ndarray

返回通过角 angle 轴 axis 确定的旋转矩阵(但是转置的)

manimlib.utils.space_ops.rotation_matrix(angle: float, axis: numpy.ndarray) → numpy.ndarray

Rotation in R^3 about a specified axis of rotation.

返回通过角 angle 轴 axis 确定的旋转矩阵

manimlib.utils.space_ops.rotation_about_z(angle: float) → list[list[float]]

返回沿 z 轴旋转 angle 的旋转矩阵

manimlib.utils.space_ops.z_to_vector(vector: numpy.ndarray) → numpy.ndarray

返回可以使 z 轴方向旋转到 vector 方向的变换矩阵

manimlib.utils.space_ops.angle_of_vector(vector: Sequence[float]) → float

Returns polar coordinate theta when vector is project on xy plane

返回 vector 在 xy 平面投影的极坐标系下的 theta

manimlib.utils.space_ops.angle_between_vectors(v1: numpy.ndarray, v2: numpy.ndarray) → float

Returns the angle between two 3D vectors. This angle will always be btw 0 and pi

返回两向量 v1, v2 的夹角

manimlib.utils.space_ops.project_along_vector(point: numpy.ndarray, vector: numpy.ndarray) → numpy.ndarray


manimlib.utils.space_ops.normalize(vect: np.ndarray, fall_back: np.ndarray | None = None) → np.ndarray

返回 vect 的单位向量

  • 若 vect 为零向量,且 fall_back=None,返回零向量

  • 若 vect 为零向量,且 fall_back不为None,返回 fall_back

manimlib.utils.space_ops.normalize_along_axis(array: numpy.ndarray, axis: numpy.ndarray) → numpy.ndarray

将所有向量沿 axis 单位化

manimlib.utils.space_ops.cross(v1: np.ndarray, v2: np.ndarray) → list[np.ndarray]

返回两向量 v1, v2 的叉积

manimlib.utils.space_ops.get_unit_normal(v1: numpy.ndarray, v2: numpy.ndarray, tol: float = 1e-06) → numpy.ndarray

返回向量 v1, v2 确定的平面的法向量

manimlib.utils.space_ops.compass_directions(n: int = 4, start_vect: numpy.ndarray = array([1., 0., 0.])) → numpy.ndarray

将 TAU 分成 n 份,从 start_vect 开始返回沿每个方向的单位向量

manimlib.utils.space_ops.complex_to_R3(complex_num: complex) → numpy.ndarray

复数转化为坐标(z 轴为 0)

manimlib.utils.space_ops.R3_to_complex(point: Sequence[float]) → complex


manimlib.utils.space_ops.complex_func_to_R3_func(complex_func: Callable[[complex], complex]) → Callable[[np.ndarray], np.ndarray]


manimlib.utils.space_ops.center_of_mass(points: Iterable[npt.ArrayLike]) → np.ndarray

返回点集 points 的重心

manimlib.utils.space_ops.midpoint(point1: Sequence[float], point2: Sequence[float]) → np.ndarray

返回 point1,point2 的中点

manimlib.utils.space_ops.line_intersection(line1: Sequence[Sequence[float]], line2: Sequence[Sequence[float]]) → np.ndarray

return intersection point of two lines, each defined with a pair of vectors determining the end points


  • 注意: 需要使用get_start_and_end()

p = line_intersection(

manimlib.utils.space_ops.find_intersection(p0: npt.ArrayLike, v0: npt.ArrayLike, p1: npt.ArrayLike, v1: npt.ArrayLike, threshold: float = 1e-05) → np.ndarray

Return the intersection of a line passing through p0 in direction v0 with one passing through p1 in direction v1. (Or array of intersections from arrays of such points/directions). For 3d values, it returns the point on the ray p0 + v0 * t closest to the ray p1 + v1 * t

过 p0 点,v0 向量方向上的射线 l1,与过 p1 点,v1 的向量上的射线 l2 的交点


manimlib.utils.space_ops.get_closest_point_on_line(a: numpy.ndarray, b: numpy.ndarray, p: numpy.ndarray) → numpy.ndarray

It returns point x such that x is on line ab and xp is perpendicular to ab. If x lies beyond ab line, then it returns nearest edge(a or b).

找到点 p 到 线段 ab 距离最近的点 x

  • 如果 p 点投影在线段 ab 上,则返回垂足

  • 如果 p 点投影不在线段 ab 上,则返回与投影最接近的线段端点

manimlib.utils.space_ops.get_winding_number(points: Iterable[float]) → float


manimlib.utils.space_ops.cross2d(a: numpy.ndarray, b: numpy.ndarray) → numpy.ndarray

二阶矩阵相乘,如果 a 不是二阶矩阵,则返回 a[0] * b[1] - b[0] * a[1]

manimlib.utils.space_ops.tri_area(a: Sequence[float], b: Sequence[float], c: Sequence[float]) → float

manimlib.utils.space_ops.is_inside_triangle(p: numpy.ndarray, a: numpy.ndarray, b: numpy.ndarray, c: numpy.ndarray) → bool

Test if point p is inside triangle abc

判断点 p 是否在点 a, b, c 构成的三角形中

manimlib.utils.space_ops.norm_squared(v: Sequence[float]) → float


(似乎可以用其他方法计算任意维度向量模长平方,例如 (v ** 2).sum()

manimlib.utils.space_ops.earclip_triangulation(verts: np.ndarray, ring_ends: list[int]) → list

Returns a list of indices giving a triangulation of a polygon, potentially with holes

  • verts is a numpy array of points

  • ring_ends is a list of indices indicating where the ends of new paths are