📚 Unit Vectors and Basis Vectors | 单位向量与基向量
In computer science, especially in fields like computer graphics, game development, data science, and robotics, vectors are fundamental tools for representing positions, directions, velocities, colours, and feature spaces. Among the many concepts surrounding vectors, unit vectors and basis vectors stand out as essential building blocks. Unit vectors give us a clean way to express direction without worrying about magnitude, while basis vectors define the very coordinate frame in which we describe our data and geometry. Understanding how to normalise a vector, what î, ĵ, k̂ really mean, and how change-of-basis matrices work unlocks a deeper grasp of transformation pipelines, lighting calculations, and even dimensionality reduction. This article explores these concepts from a computer scientist's perspective, bridging linear algebra theory with practical coding intuition.
在计算机科学中,尤其是在计算机图形学、游戏开发、数据科学和机器人学等领域,向量是用于表示位置、方向、速度、颜色和特征空间的基本工具。在围绕向量的众多概念中,单位向量和基向量是极其重要的构建模块。单位向量使我们能够干净地表达方向,而无需担心大小;基向量则定义了我们用来描述数据和几何体所处的坐标系。理解如何归一化向量、î、ĵ、k̂ 的真正含义以及基变换矩阵如何工作,能让你更深入地掌握变换流水线、光照计算甚至降维。本文从计算机科学家的视角探索这些概念,在线性代数理论与实践编码直觉之间架起桥梁。
1. Vectors in Computer Science | 计算机科学中的向量
Vectors appear everywhere in computing. In 2D and 3D graphics, a vector (x, y, z) might represent a vertex position, a surface normal, or a light direction. In physics simulations, velocity and acceleration are stored as vectors. In machine learning, each data sample is often a high-dimensional feature vector. Operations such as dot products, cross products, and normalisation form the backbone of shaders, collision detection routines, and similarity measures. Before diving into unit vectors, recall that any vector v has a magnitude |v| (or ||v||) computed via the Euclidean norm: √x² + y² + z². Its direction can be isolated by scaling it to length 1 — that is exactly a unit vector.
向量在计算中无处不在。在二维和三维图形学中,向量 (x, y, z) 可能表示顶点位置、表面法线或光照方向。在物理模拟中,速度和加速度以向量形式存储。在机器学习中,每个数据样本通常是高维特征向量。点积、叉积和归一化等运算构成了着色器、碰撞检测例程和相似度量的主干。在深入单位向量之前,请回忆任何向量 v 都有一个大小 |v|(或 ||v||),通过欧几里得范数计算:√x² + y² + z²。它的方向可以通过将其缩放至长度 1 来单独提取——这正是单位向量。
2. What is a Unit Vector? | 什么是单位向量?
A unit vector is a vector whose length (magnitude) equals exactly 1. It preserves the direction of the original vector but discards all information about scale. Mathematically, given a non-zero vector v, the corresponding unit vector v̂ is obtained by dividing each component by the magnitude:
单位向量是长度(大小)恰好等于 1 的向量。它保留了原向量的方向,但丢弃了所有关于尺度的信息。数学上,给定一个非零向量 v,对应的单位向量 v̂ 通过将每个分量除以大小得到:
v̂ = v / |v|
This operation is called normalisation. In code, you would compute the magnitude, check that it is not zero (to avoid division by zero), and then divide. Unit vectors are often depicted with a hat: û, î, n̂. The hat notation signals that the vector has unit length.
这个操作称为归一化。在代码中,你会计算大小,检查它不为零(以避免除零错误),然后做除法。单位向量通常用一个帽子符号表示:û、î、n̂。帽子记号表明该向量具有单位长度。
3. Normalisation in Practice | 归一化实践
a common requirement. Suppose you have a direction vector d = (3, 4, 0). Its magnitude is √3² + 4² + 0² = 5. The unit vector d̂ ime. In shaders, surface normals must be unit-length for correct lighting computations. In many APIs like OpenGL or DirectX, normal vectors must be re-normalised after interpolation.
归一化向量是一种常见需求。假设你有一个方向向量 d = (3, 4, 0)。它的大小是 √3² + 4² + 0² = 5。单位向量 d̂ = (3/5, 4/5, 0) = (0.6, 0.8, 0)。当游戏角色向前移动时,移动方向通常是一个单位向量,乘以速度和帧时间。在着色器中,表面法线必须是单位长度才能进行正确的光照计算。在许多 API 如 OpenGL 或 DirectX 中,法线向量在插值后必须重新归一化。
4. Properties and Uses of Unit Vectors | 单位向量的性质与用途
A unit vector' ection. Because its length is 1, the dot product of two unit vectors gives the cosine of the angle between them directly: û · v̂ sively useful in graphics for calculating how much a surface faces a light source (Lambert' nal relationships. In physics engines, collision normals are unit vectors. Directional lights are defined by a unit vector pointing toward the light. Even in text analysis, TF-IDF vectors are often normalised to unit length so that documents can be compared by angle (cosine similarity) rather than raw frequency.
单位向量的主要任务是表示纯方向。因为它的长度为 1,两个单位向量的点积直接给出它们之间夹角的余弦:û · v̂ = cos θ。这在图形学中极为有用,用于计算表面面向光源的程度(兰伯特余弦定律)或检查正交关系。在物理引擎中,碰撞法线是单位向量。方向光由指向光源的单位向量定义。甚至在文本分析中,TF-IDF 向量通常被归一化到单位长度,这样文档可以通过角度(余弦相似度)而不是原始频率来比较。
5. Introduction to Basis Vectors | 基向量简介
rs are mutually perpendicular and each has unit length, the basis is called orthonormal. The standard basis in R³ is formed by the vectors e₁ = (1,0,0), e₂ = (0,1,0), e₃ = (0,0,1), commonly written as î, ĵ, k̂. They point along the x, y, z axes respectively.
单位向量捕捉的是方向,而基向量定义的是坐标系。向量空间的一组基是一组线性无关且能张成整个空间的向量。在三维欧几里得空间中,任何向量都可以唯一地表示为基向量的线性组合。如果基向量相互垂直且每个都具有单位长度,则这组基被称为标准正交基。R³ 中的标准基由向量 e₁ = (1,0,0)、e₂ = (0,1,0)、e₃ = (0,0,1) 构成,通常写为 î、ĵ、k̂。它们分别指向 x、y、z 轴。
6. Standard Basis î, ĵ, k̂ in Computing | 计算中的标准基 î、ĵ、k̂
In code, î, ĵ, and k̂ are the default axes of any 3D world. When you define a vector v = (vx, vy, vz), you are implicitly writing v = vx î + vy ĵ + vz k̂. Transformation matrices rely on this basis: the identity matrix I has columns î, ĵ, k̂ (plus translation in homogeneous coordinates). Many graphics engines expose 'right', 'up', and 'forward' vectors that correspond to these basis vectors in object-local space. Understanding this relationship helps when constructing look-at matrices or applying rotations.
在代码中,î、ĵ 和 k̂ 是任何 3D 世界的默认坐标轴。当你定义一个向量 v = (vx, vy, vz) 时,你隐含地写了 v = vx î + vy ĵ + vz k̂。变换矩阵依赖这组基:单位矩阵 I 的列就是 î、ĵ、k̂(在齐次坐标中还包括平移)。许多图形引擎暴露 'right'、'up' 和 'forward' 向量,它们对应于物体局部空间中的这些基向量。理解这一关系有助于构建观察矩阵或应用旋转。
7. Orthonormal Basis and Coordinate Frames | 标准正交基与坐标系
ficient along u is a = p·u. In computer graphics, camera coordinate frames are orthonormal: you build the camera' ensures that rotations are pure and that dot-product lighting remains physically correct.
标准正交基由两两正交的单位向量组成。在三维中,如果你有三个标准正交向量 u、v、w,它们满足 u·u = v·v = w·w = 1 以及 u·v = v·w = w·u = 0。这样的基极其方便,因为任何向量的坐标可以简单地通过投影获得:沿 u 的系数 a = p·u。在计算机图形学中,相机坐标系是标准正交的:你构建相机的右向、上向和前向轴作为相互垂直的单位向量。这确保了旋转是纯粹的,并且点积光照保持物理正确。
8. Change of Basis and Transformation Matrices | 基变换与变换矩阵
Often you need to switch from one basis to another. If you have an object described in its local basis (e.g., model space) and you want to express it in world coordinates (standard basis), you build a transformation matrix whose columns are the local basis vectors expressed in world coordinates. Multiplying this matrix by a local-space vector yields the world-space vector: vworld = M * vlocal and basis change is essential for skeletal animation, where each bone has its own coordinate frame.
你经常需要从一组基转换到另一组基。如果你有一个用局部基(例如模型空间)描述的物体,并且想将其用世界坐标(标准基)表示,你可以构建一个变换矩阵,其各列是在世界坐标下表达的局部基向量。这个矩阵乘以局部空间向量得到世界空间向量:vworld = M * vlocal。反之,逆矩阵映射回去。在游戏引擎中,模型矩阵正是这个:局部右向、上向和前向向量构成了上 3×3 部分。单位向量和基变换的知识对于骨骼动画至关重要,其中每根骨骼都有自己的坐标框架。
9. Unit Vectors in Movement and Steering | 移动与操控中的单位向量
In autonomous agent steering (used in AI for games), behaviours such as seek, flee, and arrive rely heavily on unit vectors. The desired velocity direction is a unit vector pointing from the agent toward the target, scaled by maximum speed. Separation and alignment forces in flocking simulations also use unit vectors to keep calculations scale-invariant. By normalising directions, developers prevent fast-moving agents from having disproportionately large steering forces, leading to smoother motion.
在自主代理操控(用于游戏 AI)中,诸如寻找、逃离和到达等行为严重依赖单位向量。理想速度方向是一个从代理指向目标的单位向量,并乘以最大速度。集群模拟中的分离和对齐力也使用单位向量以保持计算的尺度不变性。通过归一化方向,开发者可以防止移动较快的代理产生不成比例的巨大操控力,从而实现更流畅的运动。
10. Basis Vectors in Feature Spaces and Dimensionality Reduction | 特征空间与降维中的基向量
In data science, the concept of basis vectors extends into high dimensions. Principal Component Analysis (PCA) finds a new orthonormal basis for the data, where the first few basis vectors (principal components) capture the most variance. Each data point can then be approximated as a linear combination of these top components. This is a change of basis from the original feature axes to a compressed, meaningful set. Similarly, in the discrete cosine transform (used in JPEG), image blocks are expressed in a basis of cosine functions. Understanding basis vectors helps you see these algorithms not as black boxes but as geometric transformations.
在数据科学中,基向量的概念延伸到高维空间。主成分分析(PCA)会找到数据的一组新的标准正交基,其中前几个基向量(主成分)捕获了最大的方差。然后每个数据点可以近似为这些主要成分的线性组合。这是从原始特征轴到一组压缩且有意义轴的基变换。同样,在用于 JPEG 的离散余弦变换中,图像块在余弦函数基中表达。理解基向量能让你把这些算法视为几何变换,而不是黑箱。
11. Summary and Key Takeaways | 总结与要点
Unit vectors and basis vectors are far more than mathematical formalism — they are the vocabulary of coordinate systems, transformations, and directional calculations in computing. A unit vector gives direction with a length of 1, simplifying angle calculations and normalising inputs. Basis vectors define the 'grid' on which all other vectors are built. By mastering normalisation, orthonormal bases, and change-of-basis matrix construction, you equip yourself to work confidently with graphics pipelines, physics simulations, game AI, and machine learning feature engineering. Always remember: a direction is best stored as a unit vector, and a coordinate frame is best described by its unit basis vectors.
单位向量和基向量远不止是数学形式——它们是计算中坐标系、变换和方向计算的词汇。单位向量以长度为 1 给出方向,简化了角度计算并使输入归一化。基向量定义了所有其他向量所依托的“网格”。通过掌握归一化、标准正交基和基变换矩阵的构建,你将有能力自信地处理图形管线、物理模拟、游戏 AI 和机器学习特征工程。永远记住:方向最好以单位向量存储,坐标系最好由其单位基向量描述。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)

或截图后用「扫一扫 → 相册」选择本图