Jesus!
我的上帝!
Nope, not Jesus. It’s just me.
不 不是上帝 是我回来了
What the hell is going on?
你都干了些啥?
Ah, yeah… yeah. I was recounting this program.
啊 对…… 对 我在重新编写这个程序
And yeah… yeah…. yeah. I think I figured it out.
还有对…… 对…… 对 我觉得我知道怎么解决它了
I think I’ve got the formula.
我已经想到了对应的公式
So basically, if you merge the projection equation,
基本上 只要把投影方程加进来的话
Wait, wait, wait…
等等 等等 等等……
What ARE you doing?
你到底在干什么?
Oh, right. Emm… Okay, let me just show you this.
噢 好了 嗯…… 现在让我来给你看看这个
Let me just finish up real quick.
再给我一点时间把这个写完
Ready?
准备好了吗?
[Click]
[敲击]
Wait…, I, I, I don’t follow this.
等一下…… 我 我 我不是很懂这是个啥
– It’s that a… – It’s a donut!
– 这是个…… – 这是个甜甜圈!
It’s incredible, isn’ it?
这让人难以置信 对吗?
So… so all this, just to render a donut
所以…所以你整的这些东西 就是为了在你的终端上
on your terminal?
画一个甜甜圈?
What the fu…
去他娘……
Thanks for watching.
感谢观看
The idea and code were orginally from the amazing Andy Sloane,
这个想法和代码出自于令人惊叹的Andy Sloane
where he was able to code this whole program in C,
他用C语言编写了整个程序
and make the code itself look like a donut.
并让代码本身看起来像个甜甜圈
He had an article explaining how the code works,
他写了一篇文章解释了这个代码的原理
which I’ll link it in description.
我会把文章链接放在视频描述中
So why is maths important for programming?
所以为什么数学对于编程来说很重要呢?
Well, It’s because even if
这是因为虽然
99% of the time you won’t need it.
99%的时候你编程都不需要用到数学
There’s a 1% chance that you might.
但还是有1%的时候你可能会用到它
And it’s in those moments that seperate out
就是这1%的部分
a great programmer from average ones.
把一个优秀的程序员和普通程序员区分开来
And if you do know your maths,
而且如果你数学很好
you’ll know how to modify this donut to increase the size,
你就会知道怎么修改代码来增加甜甜圈的大小
change the rotation or even make a cube instead.
改变它旋转的方向或者甚至把它变成一个立方体
Also, If you want to do computer graphics,
另外 如果你想掌握计算机图形学
machine learning or cryptography,
机器学习或者密码系统
you all gonna need maths.
这都需要用到数学
So might as well get good at it, as you did for programming.
所以可能你需要让你的数学水平和编程水平一样好
Anyway, back to the donut,
不管怎样 我们说回到那个甜甜圈
how do you write this program?
你要怎么写出这个程序呢?
Well, first you have to create a donut,
首先你必须创造一个甜甜圈
which is essentially a solid of revolution,
它得是一个旋转体
where we have a circle
我们要先画一个圆
that rotates around the Y axis,
它以Y轴为轴心转动
which will make a donut.
这样就可以做出一个甜甜圈
AKA a torus.
也可以说是一个圆环面
This is the formula to create the circle
这是创建圆形的公式
centred at R2 with the radians R1.
以R2为圆心 R1为圆弧
And this is a rotation matrix
这是一个旋转矩阵
to rotate the circle around the Y axis.
使圆绕着Y轴转动
So when we combine them,
所以当我们合并它们
we get the coordinates of every point on your donut.
我们会得到甜甜圈上每一个点的坐标
Now that we have a donut.
现在这里有一个甜甜圈
Shut up, I know this is a bagel.
住嘴 我知道这是一个贝果
Now that we have a donut.
现在我们假设它就是一个甜甜圈
We want to rotate it around the X and Z axis.
我们想要让它同时绕着X轴和Z轴旋转
So it looks cool, all flying and staff.
这样看起来很酷 整个甜甜圈在空中飞旋
We can do that by multiplying our donut coordinates
我们可以通过这两个旋转矩阵
with these two rotation matrixes.
来增加甜甜圈的坐标从而达成目的
Now we have to map this 3D flying donut,
现在我们必须把这个飞行的3D甜甜圈
onto a 2D screen,
绘制到一个2D的屏幕
which will become our terminal.
也就是我们的终端上面去
So each character on the terminal,
所以终端上的每一个元素
will be a pixel on the the screen.
都会是屏幕上的一个像素点
To figure out how to shade our donut,
为了弄清楚如何塑造出我们的甜甜圈
We calculate the Dot Product of the surface normal
我们要计算它表面法线的点积
and the direction of the light.
和光线的方向
So as you can see, when the normal points this way,
你可以看到 当法线指向这个方向
its way brighter.
这里就会更亮
And when I point at that way, its way darker.
如果它指向那个方向 那里就会更暗
So the Dot Product will tell us how light or dark it is.
所以点积结果可以告诉我们甜甜圈每个部分的明暗程度
We take the result of the Dot Product,
我们取出点积的结果
and map it into these characters
把它们带入到这些符号中
from darkness to brightness.
数字从小到大表示光线从暗到亮
And lastly, we draw that on our terminal.
最后 我们把这些在终端上画出来
And voila.
然后 完成啦
If you like the way I teach,
如果你喜欢我教学的方式
you can check out my new course on Data Structures and Algorithm,
可以在数据结构和算法上找到我的新课程
where I use easy to understand animations
在那里我用了一些容易理解的动画
to explain these concepts.
去解释这些概念
First 100 people to sign up for Joma Class will get 15% off.
前100个注册Joma课堂的人可以得到15%的折扣
JOMA CLASS
JOMA课堂
