📚 Unit Circle | 单位圆
The unit circle is one of the most powerful mathematical tools in a programmer’s arsenal. In computer science, it underpins everything from 2D graphics and game physics to audio synthesis and signal processing. Understanding the unit circle allows you to compute accurate rotations, generate smooth animations, normalise vectors, and work confidently with trigonometric functions in any programming language.
单位圆是程序员工具箱中最强大的数学工具之一。在计算机科学中,它支撑着从二维图形、游戏物理到音频合成和信号处理的方方面面。理解单位圆可以帮助你精确计算旋转、生成流畅动画、归一化向量,并在任何编程语言中自信地使用三角函数。
1. Introduction to the Unit Circle | 单位圆简介
The unit circle is a circle of radius 1 centred at the origin (0,0) of a Cartesian coordinate plane. For any angle θ measured counter‑clockwise from the positive x‑axis, the point where the ray at angle θ intersects the circle has coordinates (cos θ, sin θ). This simple model connects angles directly to coordinate pairs and forms the foundation of circular geometry in computing.
单位圆是以原点 (0,0) 为圆心、半径为 1 的圆。对于从正 x 轴逆时针测量的任意角度 θ,该角度上射线与圆的交点坐标为 (cos θ, sin θ)。这个简单模型将角度与坐标对直接关联起来,构成了计算机中圆几何学的基础。
In practice, the unit circle lets us treat an angle as a direction vector (cos θ, sin θ) whose length is always 1. That means any point on a larger circle of radius r is simply (r cos θ, r sin θ), and any vector can be decomposed into magnitude and direction using this concept.
实践中,单位圆让我们能把一个角度当作方向向量 (cos θ, sin θ),其长度始终为 1。这意味着半径为 r 的大圆上的任意点就是 (r cos θ, r sin θ),而任何向量都可以借助这个概念分解为大小和方向。
2. Radians and Degrees in Programming | 编程中的弧度与角度
Almost every programming language works with angles in radians. On the unit circle, one full revolution is 2π radians, corresponding to 360°. A quarter turn (90°) is π/2 rad, and half a turn (180°) is π rad. The conversion formulas are simple but critical:
几乎所有编程语言都使用弧度制来表示角度。在单位圆上,一整圈是 2π 弧度,对应 360°。四分之一圈 (90°) 是 π/2 弧度,半圈 (180°) 是 π 弧度。转换公式简单却至关重要:
rad = deg × π / 180
弧度 = 度 × π / 180
deg = rad × 180 / π
度 = 弧度 × 180 / π
Many languages provide constant Math.PI for π. Forgetting to convert degrees to radians is one of the most common bugs when using sin() and cos(). Always double‑check which unit your library expects.
许多语言提供了常量 Math.PI 表示 π。调用 sin() 和 cos() 时忘记将度转换为弧度,是最常见的错误之一。务必反复确认你的库期望什么单位。
3. Trigonometric Functions from the Unit Circle | 从单位圆看三角函数
From the definition (cos θ, sin θ) on the unit circle, all six trigonometric functions can be derived. In computing, the three most important are:
从单位圆上 (cos θ, sin θ) 的定义出发,可以推导出全部六个三角函数。在计算领域,最重要的是下面三个:
- sin θ = y-coordinate of the point on the unit circle
- sin θ = 单位圆上点的 y 坐标
- cos θ = x-coordinate of the point
- cos θ = 点的 x 坐标
- tan θ = sin θ / cos θ, undefined when cos θ = 0 (θ = π/2, 3π/2, …)
- tan θ = sin θ / cos θ,当 cos θ = 0 时无定义 (θ = π/2, 3π/2, …)
Inverse functions asin(), acos(), and especially atan2(y, x) allow you to recover an angle from coordinates. atan2(y, x) returns the angle from the positive x‑axis to the point (x,y) in the correct quadrant, using the signs of both arguments. This is far safer than plain atan(y/x) which loses quadrant information.
反函数 asin()、acos(),尤其是 atan2(y, x),能从坐标还原角度。atan2(y, x) 返回从正 x 轴到点 (x,y) 的角度,并根据两个参数的符号得出正确象限。这比简单的 atan(y/x) 安全得多,因为后者会丢失象限信息。
4. Using sin() and cos() in Code | 在代码中使用 sin() 和 cos()
Plotting a circle using the unit circle is straightforward. To draw a circle of radius r centred at (cx, cy) with n segments, you iterate over angles from 0 to 2π:
利用单位圆绘制一个圆非常简单。要画出以 (cx, cy) 为圆心、半径为 r 并由 n 段组成的圆,可以遍历从 0 到 2π 的角度:
Pseudocode:
伪代码:
for i = 0 to n:
θ = 2 × π × i / n
x = cx + r × cos(θ)
y = cy + r × sin(θ)
draw point or line to (x, y)
This approach works in any graphics or game engine, from HTML5 Canvas to Pygame or Unity. Many libraries also optimise the drawing of ellipses and arcs using the same parametric equations.
这种方法适用于任何图形或游戏引擎,从 HTML5 Canvas 到 Pygame 或 Unity。许多库也使用相同的参数方程来优化椭圆和弧线的绘制。
5. Rotating Points Around the Origin | 绕原点旋转点
A point (x, y) rotated counter‑clockwise by angle θ around the origin becomes a new point (x’, y’) given by the standard 2D rotation matrix derived directly from the unit circle:
将点 (x, y) 绕原点逆时针旋转角度 θ,会得到一个新点 (x’, y’),它由标准的二维旋转矩阵直接给出,该矩阵即源自单位圆:
x’ = x cos θ − y sin θ
x’ = x cos θ − y sin θ
y’ = x sin θ + y cos θ
y’ = x sin θ + y cos θ
This can be implemented in a single function and is essential in sprite rotation, camera movement, and any transform system. For example, rotating a character’s forward vector by 30° lets you compute a new aiming direction.
这可以在单个函数中实现,是精灵旋转、摄像机移动和任何变换系统中的基础。例如,将角色的前向向量旋转 30° 即可计算出新的瞄准方向。
6. Vector Normalization and Direction | 向量归一化与方向
A vector (vx, vy) can be normalised by dividing it by its magnitude r = √(vx² + vy²). The resulting unit vector (ux, uy) = (vx/r, vy/r) always lies on the unit circle. Because its length is 1, we can interpret it as (cos θ, sin θ) for some θ, turning a raw displacement into a clean direction.
通过将向量 (vx, vy) 除以其模长 r = √(vx² + vy²),即可对其归一化。得到的单位向量 (ux, uy) = (vx/r, vy/r) 始终位于单位圆上。由于其长度为 1,我们可以将它解释为某个 θ 的 (cos θ, sin θ),从而将原始位移转化为纯粹的方向。
Normalised vectors are used everywhere: moving a character at constant speed, bouncing a ball off a wall, or computing lighting normals in 3D graphics. The unit circle guarantees that directional calculations remain independent of magnitude.
归一化向量无处不在:让角色以恒定速度移动、球从墙上反弹,或在三维图形中计算光照法线。单位圆确保了方向计算不受长度影响。
7. Circular Motion and Animation | 圆周运动与动画
An object moving in a circle of radius r with angular speed ω (radians per second) updates its position each frame by incrementing the angle: θ += ω × Δt. The resulting coordinates at time t are:
一个物体以角速度 ω (弧度/秒) 在半径为 r 的圆上运动时,每帧通过增加角度来更新位置:θ += ω × Δt。在时刻 t 的坐标即为:
x(t) = r cos(ωt + φ)
x(t) = r cos(ωt + φ)
y(t) = r sin(ωt + φ)
y(t) = r sin(ωt + φ)
Here φ is an initial phase offset. This simple formula powers rotating menus, orbiting particles, and pendulum simulations. By varying r or ω over time you can create spirals and more complex motion paths.
这里 φ 是初始相位偏移。这一简单公式支撑着旋转菜单、粒子环绕和钟摆模拟。通过随时间改变 r 或 ω,你还可以创建螺旋线等更复杂的运动路径。
8. The Unit Circle in Game Development | 游戏开发中的单位圆
Games constantly use the unit circle. When a player aims with a gamepad thumbstick, the (x,y) values are essentially coordinates on (or inside) a mini unit circle. Developers normalise these values to get a pure direction, then multiply by speed to move a character or aim a projectile.
游戏开发中频繁用到单位圆。当玩家用手柄摇杆瞄准时,摇杆输出的 (x,y) 值本质上就是一个小型单位圆上 (或内部) 的坐标。开发者将这些值归一化以获得纯粹的方向,然后乘以速度来移动角色或发射弹丸。
Enemy AI often uses circular patrol paths defined by a centre, radius, and angular increment. When an object needs to smoothly turn to face a target, the dot product and cross product of normalised vectors (which are implicitly on the unit circle) determine the direction and amount of rotation.
敌人 AI 经常使用由圆心、半径和角增量定义的圆形巡逻路径。当物体需要平滑旋转以面向目标时,归一化向量 (它们隐含地位于单位圆上) 的点积和叉积就能决定旋转方向和量。
9. Implementing Angular Velocity | 实现角速度
Angular velocity ω is the rate of change of angle per unit time, measured in rad/s. In a game loop, you update an object’s angle with angle += ω × delta_time. Keeping ω constant yields uniform circular motion; applying angular acceleration α (rad/s²) modifies ω over time for realistic spinning effects.
角速度 ω 是单位时间内角度的变化率,单位为弧度/秒。在游戏循环中,你用 angle += ω × delta_time 来更新物体的角度。保持 ω 不变会产生匀速圆周运动;施加角加速度 α (弧度/秒²) 则可以随时间改变 ω,实现逼真的旋转效果。
For a rigid body, the relation between linear velocity and angular velocity for a point on a circle is v = ω × r, where v is the tangential speed. Understanding this helps when simulating wheels, gears, and rolling objects.
对于刚体,圆周上一点的线速度与角速度的关系为 v = ω × r,其中 v 是切向速率。理解这一点对于模拟车轮、齿轮和滚动体非常有帮助。
10. The Unit Circle in Audio and Signal Processing | 音频与信号处理中的单位圆
The unit circle appears in a different form in signal processing: the complex plane. Euler’s formula states eⁱᶿ = cos θ + i sin θ, which is exactly the point (cos θ, sin θ) on the unit circle represented as a complex number. This identity is the backbone of the Fourier transform, allowing any signal to be decomposed into sinusoids summed around the unit circle.
单位圆在信号处理中以不同的形式出现:复平面。欧拉公式 eⁱᶿ = cos θ + i sin θ,恰好就是单位圆上的点 (cos θ, sin θ) 以复数表示的形式。这一恒等式是傅里叶变换的基石,它使得任何信号都能被分解为环绕单位圆求和的正弦波。
Digital audio synthesis generates tones using sin(2π × frequency × t), which is a direct application of the unit circle oscillating over time. Phase vocoders, filters, and many other DSP algorithms rely on the unit circle’s stability and periodicity.
数字音频合成使用 sin(2π × 频率 × t) 来生成音调,这正是单位圆随时间振荡的直接应用。相位声码器、滤波器以及许多其他 DSP 算法都依赖于单位圆的稳定性和周期性。
11. Common Pitfalls: Precision and Degrees vs Radians | 常见陷阱:精度与角度与弧度
The most frequent mistake is feeding degrees into sin(), cos(), or atan2(). A value of 90 will be interpreted as 90 radians (about 5156°), giving completely wrong results. Always use explicit conversion when your data is in degrees.
最频繁的错误是将度数直接传给 sin()、cos() 或 atan2()。值 90 会被解释为 90 弧度 (约 5156°),导致完全错误的结果。当你的数据是度数时,务必进行显式转换。
Floating‑point precision can cause small errors. For instance, sin(π) might return a tiny non‑zero value due to π being irrational. When checking angles, use approximate comparisons (|a − b| < ε) rather than strict equality. With atan2(0,0) some libraries return 0, others raise a domain error – always check your documentation.
浮点精度可能导致微小误差。例如,由于 π 是无理数,sin(π) 可能返回一个极小的非零值。在比较角度时,应使用近似比较 (|a − b| < ε) 而非严格相等。对于 atan2(0,0),有些库返回 0,有些则引发定义域错误——务必查阅相关文档。
12. Practice Exercises for Programmers | 程序员练习题
To solidify your understanding, try implementing these tasks in your favourite language:
为了巩固理解,请尝试用你最喜爱的语言实现以下任务:
- Write a function that draws a regular n‑sided polygon inscribed in a unit circle using
sin()andcos(). - 编写一个函数,使用
sin()和cos()绘制内接于单位圆的正 n 边形。 - Create an animation of a point moving in a circle at a constant angular speed, allowing the user to change the radius and speed in real time.
- 创建一个点以恒定角速度做圆周运动的动画,并允许用户实时改变半径和速度。
- Implement the 2D rotation of a triangle around its centre by an angle given in degrees. Handle the conversion carefully.
- 实现将一个三角形绕其中心旋转给定度数的二维旋转,并小心处理单位转换。
- Write a vector normalisation routine and use it to show that the result always has length 1 (within tolerance).
- 编写一个向量归一化例程,并用它展示结果长度始终为 1 (在容差范围内)。
These exercises bridge the gap between the unit circle’s abstract geometry and real‑world code, deepening both your math and programming intuition.
这些练习在单位圆的抽象几何与现实代码之间架起桥梁,能同时加深你的数学直觉和编程直觉。
Published by TutorHao | Computer Science Revision Series | aleveler.com
Find Maths Textbooks on eBay UK
New, used and second-hand copies of textbooks and revision guides are often much cheaper than retail — check current listings and prices before you buy.
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导