三角函数 — 斜着走怎么算?Trigonometry — How to Calculate Diagonal Movement?
Trigonometry For Beginners! — The Organic Chemistry Tutor · 21 分钟min · 英文English · 800 万播放的经典三角函数入门Classic trig intro with 8M views
Trig Visualized — 4 分钟min · 英文English · 一张图看懂所有三角函数关系Understand all trig relationships in one diagram
3.1 问题引入3.1 The Problem
机器人朝 30 度方向走了 10cm。问题来了:The robot moves 10cm in the 30-degree direction. Here's the question:
- 往右走了多少?(x 变了多少?)How far did it move right? (How much did x change?)
- 往前走了多少?(y 变了多少?)How far did it move forward? (How much did y change?)
画出来就是一个直角三角形:If you draw it out, it forms a right triangle:
斜边是 10cm,其中一个角是 30 度。数学家给这个拆分过程起了个名字:sin 和 cos。The hypotenuse is 10cm, and one angle is 30 degrees. Mathematicians gave this splitting process a name: sin and cos.
3.2 sin 和 cos 是什么3.2 What Are sin and cos?
很简单,它们就是"拆分比例":It's simple — they're just "splitting ratios":
cos(角度) = 往前走了多少 / 总共走了多少(纵向比例)cos(angle) = how far forward / total distance (vertical ratio)
所以:So:
- 往右走了 = 总距离 x sin(角度)Distance moved right = total distance x sin(angle)
- 往前走了 = 总距离 x cos(角度)Distance moved forward = total distance x cos(angle)
用上面的例子验证:机器人朝 30 度走了 10cmLet's verify with the example above: the robot moves 10cm at 30 degrees
- 往右 = 10 x sin(30 度) = 10 x 0.5 = 5cmRight = 10 x sin(30°) = 10 x 0.5 = 5cm
- 往前 = 10 x cos(30 度) = 10 x 0.866 = 8.66cmForward = 10 x cos(30°) = 10 x 0.866 = 8.66cm
sin 和 cos 就是把"斜着走"翻译成"往右走了多少 + 往前走了多少"的工具。sin and cos are tools that translate "moving diagonally" into "how far right + how far forward."
不用计算器,猜一猜:Without a calculator, take a guess:
朝 0 度(正前方)走 10 步,sin(0 度) 应该是多少?(提示:完全往前走,往右走了 0 步)Walk 10 steps at 0 degrees (straight ahead). What should sin(0°) be? (Hint: you're going entirely forward, 0 steps to the right)
3.3 常用值参考(不用背,用的时候查这里)3.3 Common Values Reference (No need to memorize — look them up here)
以下数值不需要记住。写代码时电脑会帮你算 sin/cos,这张表只是帮你建立直觉 -- 角度越大,sin 越大,cos 越小。You don't need to memorize these values. The computer calculates sin/cos for you when coding. This table just helps build intuition — as the angle increases, sin gets larger and cos gets smaller.
| 角度Angle | sin | cos | 含义Meaning |
|---|---|---|---|
| 0 度0° | 0 | 1 | 完全往前走Moving entirely forward |
| 30 度30° | 0.5 | 0.87 | 主要往前,稍微偏右Mostly forward, slightly right |
| 45 度45° | 0.71 | 0.71 | 往前和往右各一半Equal parts forward and right |
| 60 度60° | 0.87 | 0.5 | 主要往右,稍微偏前Mostly right, slightly forward |
| 90 度90° | 1 | 0 | 完全往右走Moving entirely right |
规律很明显:角度越大,sin 越大(越偏右),cos 越小(越不往前)。The pattern is clear: as the angle increases, sin gets larger (more rightward) and cos gets smaller (less forward).
3.4 自己试一试3.4 Try It Yourself
拖动下面的滑块,看看不同角度时 sin 和 cos 怎么变化:Drag the slider below to see how sin and cos change at different angles:
3.5 为什么定位轮需要 sin/cos?3.5 Why Do Tracking Wheels Need sin/cos?
定位轮测到的是两个信息:Tracking wheels measure two things:
- 机器人走了多远(通过轮子转了几圈计算出来的距离)How far the robot traveled (distance calculated from wheel rotations)
- 机器人朝哪个方向(通过陀螺仪或两个轮子的差值)Which direction the robot is facing (from the gyroscope or the difference between two wheels)
但我们的坐标系需要的是:But our coordinate system needs:
- x 变了多少(左右方向的位移)How much x changed (left-right displacement)
- y 变了多少(前后方向的位移)How much y changed (front-back displacement)
sin 和 cos 就是那个翻译器 -- 把"朝某个方向走了多远"翻译成"x 变了多少、y 变了多少"。sin and cos are the translator — they convert "moved this far in this direction" into "x changed by this much, y changed by this much."
在定位轮的代码里,你会看到这两行:In the tracking wheel code, you'll see these two lines:
deltaX = distance * sin(heading)
deltaY = distance * cos(heading)
现在你知道它们在干什么了。Now you know what they're doing.