相信很多同学就算没听过3Blue1Brown,也一定曾看过他们出品的视频,其从独特的视觉角度解说各种数学概念,内容包括线性代数、微积分、神经网络、傅里叶变换以及四元数等晦涩难懂的知识点。例如最火的《线性代数本质》系列视频。
那么这些视频是如何制作的呢?
这里需要引入的是Python的Manim视频支持引擎——专门用于支持数学可视化的媒体引擎,通过Manim并结合Python编程就可以实现3Blue1Brown的视频效果。
什么是Python
在当今备受推崇的大数据技术和人工智能技术领域,Python一直是一门备受瞩目的编程语言。它长期占据着TIOBE公布的编程语言排行榜的榜首位置。业界也一直流传着一句话:“人生苦短,我用Python”。Python作为一门计算机编程语言具有独特的优势,可以总结为以下三个特点:简洁性、易读性和可扩展性。
简洁性:
Python是一门非常简洁的脚本语言。很多时候,只需一行代码就能实现复杂的功能。比如,下面这行代码就能打印出一个九九乘法表。这种简洁性使得你能够将注意力集中在解决问题上,而不必花费过多时间去理解语言本身。
易读性:
Python的语法风格清晰简约,易于阅读。有时候,它就像人类说话一样自然简单,没有过多的修饰。
可扩展性:
Python最初的设计就注重可扩展性,它并没有将所有的特性和功能集成到核心语言中。Python提供了丰富的API和工具,以及众多第三方库,让你可以灵活扩展其功能。你可以根据需要选择合适的库来解决问题,这使得Python非常适合各种不同的应用场景。
什么是manim
manim视频
manim是一个Python第三方库,全称是mathematical animation engine(数学动画引擎)。它是由斯坦福大学数学系学生Grant Sanderson在其YouTube频道3Blue1Brown上创建的。manim用于解说线性代数、微积分、神经网络、黎曼猜想、傅里叶变换以及四元数等数学概念。
manim因其精美的动画制作和独特的解释角度而广受关注,并吸引了越来越多的读者参与其中,共同贡献内容。manim经历了两个版本的迭代。第一代是ManimCairo,这是较早的版本,Grant Sanderson早期的项目都是在此版本中编译的。它使用cairo库作为渲染引擎。第二代是ManimGL,由Grant Sanderson等人开发的最新版本。该版本的最大改进是采用更强大的OpenGL库进行渲染。
此外,还有ManimCE(manim社区版),也使用OpenGL进行渲染。
manim使你能够以编程的方式创建精确的数学图形、动画和场景。与传统的几何画板等绘图软件不同,manim提供了一种全新的思路:“所思即可得”。它能够实现非常精准的绘制。在manim的世界中,你可以体验到一切皆可设置的感觉。颜色、粗细、长度、角度、时长、播放方式等均可通过设置来完成,这使你能够制作出具有个性化的数学动画。
版本 | 渲染引擎 | 库 |
---|---|---|
ManimCairo | Cairo | manimlib |
ManimGL | OpenGL | manimgl |
ManimCE | OpenGL | manim |
如何来运行Manim
-
安装 Manim:
首先,您需要安装 Python 和一些依赖项。然后,您可以通过以下命令使用 pip 安装 Manim:pip install manim
-
创建动画:
创建一个 Python 脚本,并使用 Manim 中的类来定义您的动画场景和对象。 -
运行 Manim:
在命令行中,转到包含您的 Python 脚本的目录,并使用以下命令来运行 Manim:manim your_script.py YourSceneName -p -ql
其中,
your_script.py
是您的 Python 脚本的文件名,YourSceneName
是您定义的场景名称。
这样,Manim 就会开始渲染动画。请注意,Manim 的运行可能需要一定的时间,具体取决于动画内容和计算机性能。
用 Manim 绘制图形
首先需要引入 Manim 库,然后将需要绘制的内容封装到一个类(class)里面。
from manim import *
对于编辑好的程序文件(例如christmas-intro.py文件),需要在同一个文件夹下运行命令来运行程序,命令格式如下:
manim -pql christmas-intro.py DemoSquare
上面的命令行中:
manim
是运行程序的主要标志用语;p
表示程序运行后会进行预览(图片或视频);ql
表示低质量(quality low), 其他的选项有-ql
,-qm
,-qh
,-qk
, 分别表示 低质量、正常质量、高质量、4K质量;christmas-intro.py
是py代码文件;DemoSquare
是 py代码文件中的一个类;
命令行中,还有其他许多参数可以设置,可以通过社区版的支持文档来进一步了解。
几个常见方程的示例
from manim import *class QuadraticSurface(ThreeDScene):def construct(self):resolution_fa = 24 #曲面样本数axes = ThreeDAxes() #3维度笛卡尔直角坐标系self.set_camera_orientation(phi = 75*DEGREES, theta = 45*DEGREES) #phi是竖直方向岔开的角度,theta是水平面岔开的角度def paramsFunc(u, v): #参数方程x = uy = 1/2*u**2z = vreturn np.array([x, y, z])quadratic_surface = Surface(paramsFunc,resolution = (resolution_fa, resolution_fa), #分辨率u_range=[-2.5, +2.5], #参数u的范围v_range=[-1, +1], #参数v的范围fill_opacity = 0.5 #透明度) #二次曲面self.add(axes) #添加坐标轴self.wait() #停留一秒self.play(Write(quadratic_surface), run_time = 10) #绘制二次曲面self.begin_3dillusion_camera_rotation(rate = 2) #摇晃rotation illusionself.wait(PI) #摇晃时长self.stop_3dillusion_camera_rotation() #停止摇晃with tempconfig({'quality':'medium_quality', 'preview':True}):scene = QuadraticSurface()scene.render()
from manim import *class QuadraticSurface(ThreeDScene):def construct(self):resolution_fa = 24 #曲面样本数axes = ThreeDAxes() #3维度笛卡尔直角坐标系self.set_camera_orientation(phi = 75*DEGREES, theta = 45*DEGREES) #phi是竖直方向岔开的角度,theta是水平面岔开的角度def paramsFunc(u, v): #参数方程x = np.sqrt(2)* np.cos(TAU*u)*vy = np.sqrt(2)* np.sin(TAU*u)*vz = vreturn np.array([x, y, z])quadratic_surface = Surface(paramsFunc,resolution = (resolution_fa, resolution_fa), #分辨率u_range=[-2, +2], #参数u的范围v_range=[-2, +2], #参数v的范围fill_opacity = 0.5 #透明度) #二次曲面self.add(axes) #添加坐标轴self.wait() #停留一秒self.play(Write(quadratic_surface), run_time = 10) #绘制二次曲面self.begin_3dillusion_camera_rotation(rate = 2) #摇晃rotation illusionself.wait(PI) #摇晃时长self.stop_3dillusion_camera_rotation() #停止摇晃with tempconfig({'quality':'medium_quality', 'preview':True}):scene = QuadraticSurface()scene.render()
manin安装教程
https://docs.manim.community/en/stable/installation.html(社区版)
Manim教程合集
https://www.bilibili.com/video/BV1W4411Z7Zt/?vd_source=3ef6540f8473c7367625a53b7b77fd66
Manim官方文档
https://docs.manim.org.cn/cairo-backend/getting_started/index.html
from manim import *class QuadraticSurface(ThreeDScene):def construct(self):resolution_fa = 24 #曲面样本数axes = ThreeDAxes() #3维度笛卡尔直角坐标系self.set_camera_orientation(phi = 75*DEGREES, theta = 45*DEGREES) #phi是竖直方向岔开的角度,theta是水平面岔开的角度def paramsFunc(u, v): #参数方程x = np.sinh(u)*np.cos(v)y = np.sinh(u)*np.sin(v)z = np.cosh(u)return np.array([x, y, z])quadratic_surface = Surface(paramsFunc,resolution = (resolution_fa, resolution_fa), #分辨率u_range=[-1.5, +1.5], #参数u的范围v_range=[-PI, +PI], #参数v的范围fill_opacity = 0.5 #透明度) #二次曲面def paramsFunc1(u, v): #参数方程x = np.sinh(u)*np.cos(v)y = np.sinh(u)*np.sin(v)z = np.cosh(u)return np.array([x, y, -z])quadratic_surface1 = Surface(paramsFunc1,resolution = (resolution_fa, resolution_fa), #分辨率u_range=[-1.5, +1.5], #参数u的范围v_range=[-PI, +PI], #参数v的范围fill_opacity = 0.5 #透明度) #二次曲面self.add(axes) #添加坐标轴self.wait() #停留一秒self.play(Write(quadratic_surface), run_time = 10) #绘制二次曲面self.play(Write(quadratic_surface1), run_time = 10) #绘制二次曲面self.begin_3dillusion_camera_rotation(rate = 2) #摇晃rotation illusionself.wait(PI) #摇晃时长self.stop_3dillusion_camera_rotation() #停止摇晃with tempconfig({'quality':'medium_quality', 'preview':True}):scene = QuadraticSurface()scene.render()
版本说明
对于 Manim 的初学者,使用 Manim 时,对于其版本的选择还是有些需要注意的地方。
不同版本的代码、教程等,还是有稍许差异的。
目前 Manim 主要有三个版本:
3b1b 旧版:3blue1brown 自己维护的版本,使用 Cairo 作为后端;
3b1b 新版:3blue1brown 自己维护的版本,使用 OpenGL 和 moderngl 来进行 GPU 渲染,优点是速度快;
Manim 社区版:社区版是2020年下半年才出来的版本,目前主要是 Manim 旧版的衍生,更新更活跃,有很好的文档和社区支持。当然,随着社区版的迭代更新,目前 3b1b 新版 的某些特性也在逐步容纳进来。
关于这几个版本如何选择的建议:
如果你是新接触这个工具的,建议可以从社区版开始,主要是社区版的文档教程比较齐全,其迭代的频率更快。
如果你是 Python 或者其他编程领域的资深人士,想体自己动手深入理解并优化这个工具,可以使用 3b1b 新版
Manim 的安装
在 Manim 3b1b 版本 和 社区版均提供了如何安装的文档:
3b1b 新版的安装文档:https://github.com/3b1b/manim
社区版的安装文档:https://docs.manim.community/en/stable/installation.html
安装ffmpeg
FFmpeg是一个开源的跨平台音视频处理工具,可以用来录制、转换和流式传输音视频内容。它包括一组库和程序,可以处理多种不同格式的音频、视频和图片文件。FFmpeg支持几乎所有主流的音视频格式,因此被广泛应用于多媒体处理领域。
FFmpeg提供了丰富的功能,包括但不限于:
- 音视频编解码:可以对音频和视频进行编码(压缩)和解码(解压),支持众多编解码器。
- 格式转换:可以将音视频文件从一个格式转换为另一个格式,例如从MP4转换为AVI。
- 剪辑和编辑:可以对音视频文件进行剪辑、合并、分离等操作。
- 流媒体处理:支持流式传输音视频内容,适用于网络直播、视频会议等场景。
- 屏幕录制:可以用来录制屏幕内容并保存为视频文件。
由于其强大的功能和灵活性,FFmpeg被广泛用于视频处理、视频转码、音频处理等各种多媒体应用中。同时,FFmpeg也是许多视频播放器、编辑软件以及流媒体平台的重要组件之一。
安装FFmpeg可以通过命令行或者图形界面进行操作,以下是在Windows和MacOS上安装FFmpeg的基本步骤:
在Windows上安装FFmpeg
-
下载FFmpeg:
- 访问FFmpeg官方网站或Windows Builds by BtbN下载最新版本的静态链接的可执行文件。
-
解压文件:
- 将下载的压缩包解压到你想要安装的位置。
-
设置环境变量(可选):
- 将FFmpeg的bin目录添加到系统的PATH环境变量中,这样就可以在命令行中直接访问FFmpeg。
-
测试安装:
- 打开命令提示符(Win + R,输入cmd,回车),输入以下命令来检查FFmpeg是否成功安装:
ffmpeg -version
- 打开命令提示符(Win + R,输入cmd,回车),输入以下命令来检查FFmpeg是否成功安装:
在MacOS上安装FFmpeg
-
使用Homebrew安装(推荐):
- 打开终端,运行以下命令安装Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 然后运行以下命令来安装FFmpeg:
brew install ffmpeg
- 打开终端,运行以下命令安装Homebrew:
-
测试安装:
- 在终端中输入以下命令来检查FFmpeg是否成功安装:
ffmpeg -version
- 在终端中输入以下命令来检查FFmpeg是否成功安装:
在Linux上安装FFmpeg
-
使用包管理器安装:
- 对于大多数Linux发行版,可以使用其包管理器来安装FFmpeg。例如,在Ubuntu上可以使用以下命令:
sudo apt-get update sudo apt-get install ffmpeg
- 对于大多数Linux发行版,可以使用其包管理器来安装FFmpeg。例如,在Ubuntu上可以使用以下命令:
-
测试安装:
- 在终端中输入以下命令来检查FFmpeg是否成功安装:
ffmpeg -version
- 在终端中输入以下命令来检查FFmpeg是否成功安装:
验证安装
无论在哪个操作系统上安装,验证安装步骤都是一样的。只需要在命令行或终端中输入以下命令:
ffmpeg -version
如果安装成功,会显示FFmpeg的版本信息。这样就可以确认FFmpeg已经成功安装并且可以在系统中正常运行了。
安装Latex
LaTeX是一种专业的排版系统,常用于撰写学术论文、书籍、报告和演示文稿。与常见的文字处理软件(如Microsoft Word)不同,LaTeX使用的是一种“标记语言”,用户需要通过输入特定的命令和标记来控制文档的格式、结构和样式。
LaTeX最初是由著名计算机科学家Leslie Lamport开发的,它建立在TeX排版系统之上,并提供了一系列功能强大的工具和环境,使用户能够轻松地创建高质量的文档。
使用LaTeX编写文档的优势包括:
- 专业的排版效果:LaTeX能够生成高质量、专业水准的文档排版,特别适合于学术论文、书籍等内容丰富的文档。
- 具有强大的数学公式排版能力:LaTeX在排版数学公式和符号方面非常出色,被广泛应用于数学、物理等领域的文档撰写。
- 跨平台性:LaTeX在不同操作系统上都可以运行,并且生成的文档格式稳定、可靠。
虽然学习曲线较陡,但一旦熟悉了LaTeX的基本语法和命令,就能够享受到其带来的排版便利和效果。
安装LaTeX可以通过以下步骤进行操作:
在Windows上安装LaTeX
-
下载安装包:
- 访问TeX Users Group或MiKTeX网站,下载LaTeX发行版的安装程序。
-
运行安装程序:
- 双击下载的安装程序,按照提示进行安装。
-
配置编辑器(可选):
- 如果你希望使用图形化的编辑器来编写LaTeX文档,可以安装一些流行的LaTeX编辑器,如TeXstudio、TeXworks等,这些编辑器通常会自动检测已安装的LaTeX发行版。
-
测试安装:
- 打开命令提示符或者你喜欢的LaTeX编辑器,新建一个简单的.tex文件,输入以下内容并保存:
\documentclass{article} \begin{document} Hello, LaTeX! \end{document}
- 编译这个文件,如果能够生成对应的.pdf文件并且内容显示正常,那么LaTeX安装成功了。
- 打开命令提示符或者你喜欢的LaTeX编辑器,新建一个简单的.tex文件,输入以下内容并保存:
在MacOS上安装LaTeX
-
使用MacTeX安装:
- 下载并安装MacTeX,这是专为MacOS准备的LaTeX发行版。你可以在MacTeX官方网站下载最新版本的安装程序。
-
配置编辑器(可选):
- 安装一些流行的LaTeX编辑器,如TeXShop、TeXstudio等,这些编辑器通常会自动检测已安装的LaTeX发行版。
-
测试安装:
- 使用编辑器创建一个简单的.tex文件,输入上述示例中的内容,并进行编译。
在Linux上安装LaTeX
-
使用包管理器安装:
- 对于大多数Linux发行版,可以使用其包管理器来安装LaTeX。例如,在Ubuntu上可以使用以下命令:
sudo apt-get update sudo apt-get install texlive-full
- 对于大多数Linux发行版,可以使用其包管理器来安装LaTeX。例如,在Ubuntu上可以使用以下命令:
-
测试安装:
- 使用编辑器创建一个简单的.tex文件,输入上述示例中的内容,并进行编译。
安装dvisvgm
安装 dvisvgm 可以通过以下步骤进行:
Windows 用户
- 使用 Chocolatey 进行安装:
打开命令提示符(CMD)或 PowerShell,并执行以下命令:choco install dvisvgm
macOS 用户
- 使用 Homebrew 进行安装:
打开终端,并执行以下命令:brew install dvisvgm
Linux 用户
-
对于基于 Debian 的系统(如 Ubuntu):
打开终端,并执行以下命令:sudo apt-get install dvisvgm
-
对于基于 Red Hat 的系统(如 Fedora):
打开终端,并执行以下命令:sudo dnf install dvisvgm
一旦安装完成,您可以在命令行中运行 dvisvgm --version
来验证 dvisvgm 是否成功安装。
利用Manim绘制苹果logo
from manimlib.imports import *
from my_projects.my_utils.boolean_operation import VGroup_get_angle = lambda c: np.angle(-c) + PI if not c/abs(c) == 1 else 0
convert_angle = lambda a: a if a>=0 else a + TAUclass Apple_logo(VGroup):CONFIG = {'scale': 0.25,'logo_color': RED_D,'logo_stroke': 8,'coord_center': DOWN * 1.25,'assistent_lines_config': {'stroke_width': 2,'stroke_color': GREY,},'key_lines_config': {'stroke_width': 2.5,'stroke_color': GREY_E,},'step_size': 0.02,'create_shape': True,}def __init__(self, **kwargs):VGroup.__init__(self, **kwargs)self.set_key_lines()self.set_outline()if self.create_shape:self.set_logo_shape()def set_key_lines(self):s = self.scalecenter = self.coord_centerself.c1 = Circle(arc_center=center+ORIGIN, radius=1/2*s, **self.key_lines_config)self.c5_r = Circle(arc_center=center+3*s*RIGHT, radius=5/2*s, **self.key_lines_config)self.c5_l = Circle(arc_center=center+3*s*LEFT, radius=5/2*s, **self.key_lines_config)# self.c5_l = self.c5_r.copy().flip()self.c8_d = Circle(arc_center=center + s * 5.77 * DOWN, radius=8/2*s, **self.key_lines_config)self.c13 = Circle(arc_center=center + s * 4.73 * UP, radius=13/2*s, **self.key_lines_config)self.c8_r = Circle(arc_center=center + s * ( 3.48 * RIGHT + 7.69 * UP),radius=8/2*s, **self.key_lines_config)self.c8_l = Circle(arc_center=center + s * (-3.48 * RIGHT + 7.69 * UP),radius=8/2*s, **self.key_lines_config)self.c3_r = Circle(arc_center=center + s * ( 5.9 * RIGHT + 2.75 * UP),radius=3/2*s, **self.key_lines_config)self.c3_l = Circle(arc_center=center + s * (-5.9 * RIGHT + 2.75 * UP),radius=3/2*s, **self.key_lines_config)self.c24_r = Circle(arc_center=center + s * ( 3.89 * RIGHT + 6.54 * UP),radius=24/2*s, **self.key_lines_config)self.c24_l = Circle(arc_center=center + s * (-3.89 * RIGHT + 6.54 * UP),radius=24/2*s, **self.key_lines_config)self.c12_r = Circle(arc_center=center + s * ( 2.11 * RIGHT + 6.23 * UP),radius=12/2*s, **self.key_lines_config)self.c12_l = Circle(arc_center=center + s * (-2.11 * RIGHT + 6.23 * UP),radius=12/2*s, **self.key_lines_config)self.c8_u = Circle(arc_center=center + s * 14.89 * UP, radius=8/2*s, **self.key_lines_config)self.c8_01 = Circle(arc_center=center + s * (8.82 * RIGHT + 6.23 * UP),radius=8/2*s, **self.key_lines_config)self.c8_02 = Circle(arc_center=center + s * (3.61 * RIGHT + 12.23 * UP),radius=8/2*s, **self.key_lines_config)self.c8_03 = Circle(arc_center=center + s * (-0.46 * RIGHT + 15.69 * UP),radius=8/2*s, **self.key_lines_config)self.key_lines = VGroup(self.c1, self.c5_r, self.c5_l,self.c8_d, self.c13,self.c8_r, self.c8_l,self.c3_r, self.c3_l,self.c24_r, self.c24_l,self.c12_r, self.c12_l,self.c8_u,self.c8_01,self.c8_02, self.c8_03,)self.key_lines_center = VGroup(* [Dot(c.get_center(), stroke_color=self.key_lines_config['stroke_color'],radius=self.key_lines_config['stroke_width']/100) for c in self.key_lines])return selfdef set_outline(self):s = self.scalecenter = self.coord_center# kp_list = [[7.41, 2.49, 0], [4.81, -1.72, 0], [1.85, -2.22, 0], [-1.85, -2.22, 0], [-4.81, 1.72, 0], [-8.1, 5.91, 0],# [-6.22, 10.6, 0], [-1.74, 11.29, 0], [1.74, 11.29, 0], [6.22, 10.6, 0], [6.95, 9.77, 0], [-0.37, 11.87, 0], [3.42, 16.22, 0]]kp_list = [[6.95, 9.77, 0], [6.22, 10.6, 0], [1.74, 11.29, 0], [-1.74, 11.29, 0], [-6.22, 10.6, 0], [-8.1, 5.91, 0],[-4.81, -1.72, 0], [-1.85, -2.22, 0], [1.85, -2.22, 0], [4.81, -1.72, 0], [7.41, 2.49, 0], [-0.35, 11.69, 0], [3.5, 16.23, 0]]self.key_angles = np.array([10.62, 69, -51.5, 69, 49.77, 40.52, 73.94, -54.97, 73.94, 23.77, -131.51, 96.09, -96.09]) * DEGREESself.key_radius = np.array([12/2, 8/2, 8/2, 8/2, 12/2, 24/2, 5/2, 8/2, 5/2, 24/2, 8/2]) * sself.key_points = np.array(kp_list) * s + centern = len(kp_list)self.outline_by_arcs = VGroup_(step_size=self.step_size)for i in range(n-2):p1, p2 = self.key_points[i], self.key_points[(i+1) % (n-2)]arc_i = ArcBetweenPoints(p1, p2, angle=self.key_angles[i], radius=self.key_radius[i], stroke_color=self.logo_color, stroke_width=self.logo_stroke)self.outline_by_arcs.add(arc_i)arc_1 = ArcBetweenPoints(self.key_points[-2], self.key_points[-1], angle=self.key_angles[-2], radius=self.key_radius[-2], stroke_color=self.logo_color, stroke_width=self.logo_stroke)arc_2 = ArcBetweenPoints(self.key_points[-2], self.key_points[-1], angle=self.key_angles[-1], radius=self.key_radius[-1], stroke_color=self.logo_color, stroke_width=self.logo_stroke)self.outline_by_arcs.add(arc_1, arc_2)return selfdef set_logo_shape(self):s = self.scalecenter = self.coord_centerself.outline_by_arcs.get_all_outline_points()self.shape_1 = self.outline_by_arcs.get_shape(center=center + s * (-3.89 * RIGHT + 6.54 * UP), fill_color=self.logo_color, fill_opacity=1, stroke_color=self.logo_color, stroke_width=self.logo_stroke)self.shape_2 = self.outline_by_arcs.get_shape(center=center + s * (1.52 * RIGHT + 14.05 * UP), fill_color=self.logo_color, fill_opacity=1, stroke_color=self.logo_color, stroke_width=self.logo_stroke)# shape_1.set_fill(self.logo_color, 1).set_stroke(self.logo_color, self.logo_stroke, 1)# shape_2.set_fill(self.logo_color, 1).set_stroke(self.logo_color, self.logo_stroke, 1)self.add(self.shape_1, self.shape_2)return self# from my_projects.others.BooleanOperationsOnPolygons import PolygonIntersection, PolygonUnion, PolygonSubtraction
#
# class PolygonBooleanTest(Scene):
# def construct(self):
# pol1 = RegularPolygon(9).scale(2).shift(LEFT)
# pol2 = RegularPolygon(9).scale(2).shift(RIGHT)
# start = time.perf_counter()
# boolpol = PolygonSubtraction(
# PolygonUnion(pol1, pol2), PolygonIntersection(pol1, pol2)
# ).set_stroke(color=YELLOW, width=0).set_fill(color=YELLOW, opacity=0.5)
# end = time.perf_counter()
# print(end - start)
# self.add(pol1, pol2, boolpol)
#
# '''
# pol1 = RegularPolygon(16).scale(2)
# pol2 = Circle().stretch(1.2, 1).stretch(0.8, 0)
# pol3 = RegularPolygon(5).scale(2).shift(LEFT*0.9)
# pol4 = RegularPolygon(3).stretch(3, 1).shift(LEFT)
# pol5 = Ellipse().scale(0.6).shift(LEFT).stretch(2, 0)
# #start = time.perf_counter()
# boolpol = PolygonSubtraction(
# PolygonUnion(
# PolygonIntersection(
# PolygonSubtraction(
# pol1, pol2), pol3), pol4), pol5) \
# .set_stroke(color=YELLOW, width=0).set_fill(color=YELLOW, opacity=0.5)
# #end = time.perf_counter()
# #print(end - start)#0.49910769999999993
# self.add(pol1, pol2, pol3, pol4, pol5, boolpol)
# '''
#class Compass(VGroup):CONFIG = {'stroke_color': GREY_E,'fill_color': WHITE,'stroke_width': 2,'leg_length': 3,'leg_width': 0.12,'r': 0.2,'depth_test': True,}def __init__(self, span=2.5, **kwargs):VGroup.__init__(self, **kwargs)self.span = spanself.create_compass()# def create_compass_old(self):## s, l, r = self.span, self.leg_length, self.r# theta = np.arcsin(s/2/l)# arc = Arc(start_angle=-PI/2 + theta, angle=TAU-theta*2, radius=r, stroke_color=self.stroke_color, stroke_width=self.stroke_width*4)# dot = Dot(color=self.stroke_color).set_height(self.stroke_width*8/100)# leg_01 = Line(arc.get_center(), arc.get_center() + complex_to_R3(l * np.exp(1j * (-PI/2-theta))), stroke_color=self.stroke_color, stroke_width=self.stroke_width)# leg_02 = Line(arc.get_center(), arc.get_center() + complex_to_R3(l * np.exp(1j * (-PI/2+theta))), stroke_color=self.stroke_color, stroke_width=self.stroke_width * 1)# leg_11 = Line(arc.get_center(), arc.get_center() + complex_to_R3((l - r * 0.8) * np.exp(1j * (-PI/2-theta))), stroke_color=self.stroke_color, stroke_width=self.stroke_width * 4)# leg_12 = Line(arc.get_center(), arc.get_center() + complex_to_R3((l - r * 0.8) * np.exp(1j * (-PI/2+theta))), stroke_color=self.stroke_color, stroke_width=self.stroke_width * 4)# pen_point = Dot(color=self.stroke_color).set_height(self.stroke_width * 1/125).move_to(leg_02.get_end())# leg_1, leg_2 = VGroup(leg_01, leg_11), VGroup(leg_02, leg_12, pen_point)# head = Line(UP * r, UP * (r + r * 1), stroke_color=self.stroke_color, stroke_width=self.stroke_width * 4)## self.add(arc, dot, head, leg_1, leg_2)# self.move_to(ORIGIN)## return selfdef create_compass(self):s, l, r, w = self.span, self.leg_length, self.r, self.leg_widthself.theta = np.arcsin(s/2/l)self.c = Circle(radius=r, fill_color=self.fill_color, fill_opacity=1, stroke_color=self.stroke_color, stroke_width=self.stroke_width*5)c2 = Circle(radius=r+self.stroke_width*5/100/2, fill_opacity=0, stroke_color=self.fill_color, stroke_width=self.stroke_width)self.leg_1 = Polygon(ORIGIN, l * RIGHT, (l-w*np.sqrt(3)) * RIGHT + w * DOWN, w * DOWN,stroke_width=0, stroke_color=self.fill_color, fill_color=self.stroke_color,fill_opacity=1).rotate(-PI/2-self.theta, about_point=self.c.get_center())self.leg_2 = Polygon(ORIGIN, l * RIGHT, (l-w*np.sqrt(3)) * RIGHT + w * UP, w * UP,stroke_width=0, stroke_color=self.fill_color, fill_color=self.stroke_color,fill_opacity=1).rotate(-PI/2+self.theta, about_point=self.c.get_center())# self.leg_1, self.leg_2 = VGroup(leg_01, leg_11), VGroup(leg_02, leg_12, pen_point)h = Line(UP * r, UP * (r + r * 1.8), stroke_color=self.stroke_color, stroke_width=self.stroke_width*6)self.head = VGroup(h, self.c, c2)self.add(self.leg_1, self.leg_2, self.head)self.move_to(ORIGIN)return selfdef get_niddle_tip(self):return self.leg_1.get_vertices()[1]def get_pen_tip(self):return self.leg_2.get_vertices()[1]def move_niddle_tip_to(self, pos):self.shift(pos-self.get_niddle_tip())return selfdef rotate_about_niddle_tip(self, angle=PI/2):self.rotate(angle=angle, about_point=self.get_niddle_tip())def get_span(self):# return self.span 如果进行了缩放而self.span没变会有问题return get_norm(self.get_pen_tip() - self.get_niddle_tip())def set_span(self, s):self.span = sl, r, w = self.leg_length, self.r, self.leg_widththeta_new, theta_old = np.arcsin(s/2/l), self.thetasign = np.sign(get_angle(R3_to_complex(self.leg_2.get_vertices()[1] - self.leg_2.get_vertices()[0])) - get_angle(R3_to_complex(self.leg_1.get_vertices()[1] - self.leg_1.get_vertices()[0])))rotate_angle = 2 * (theta_new - theta_old) * signself.leg_2.rotate(rotate_angle, about_point=self.c.get_center())self.theta=theta_newself.head.rotate(rotate_angle/2, about_point=self.c.get_center())self.rotate_about_niddle_tip(-rotate_angle/2)return selfdef set_compass(self, center, pen_tip):self.move_niddle_tip_to(center)self.set_span(get_norm(pen_tip - center))self.rotate_about_niddle_tip(np.angle(R3_to_complex(pen_tip - center)) - np.angle(R3_to_complex(self.get_pen_tip() - center)))return selfdef set_compass_to_draw_arc(self, arc):return self.set_compass(arc.arc_center, arc.get_start())def reverse_tip(self):return self.flip(axis=self.head[0].get_end() - self.head[0].get_start(), about_point=self.c.get_center())class DrawingScene(Scene):CONFIG = {'compass_config':{'stroke_color': GREY_E,'fill_color': WHITE,'stroke_width': 2,'leg_length': 3,'leg_width': 0.12,'r': 0.2,'depth_test': True,},'ruler_config':{'width': 10,'height': 0.8,'stroke_width': 8,'stroke_color': GREY_E,'stroke_opacity': 0.4,'fill_color': WHITE,'fill_opacity': 0.5,},'dot_config':{'radius': 0.06,'color': GREY_E,},'line_config':{'stroke_color': GREY_E,'stroke_width': 2.5,},'brace_config':{'fill_color': GREY_E,'buff':0.025,},'text_config':{'size': 0.6 * 5, # 5 times than the actual size and the sacle down'font': 'Cambria Math','color': GREY_E,},'add_ruler': False,}def setup(self):self.cp = Compass(**self.compass_config)self.ruler = VGroup(Rectangle(**self.ruler_config).set_height(self.ruler_config['height'] - self.ruler_config['stroke_width']/2/100, stretch=True)\.round_corners(self.ruler_config['height']/8),Rectangle(**self.ruler_config).set_opacity(0))self.dot = Dot(**self.dot_config)self.cp.move_to(UP * 10)if self.add_ruler:self.ruler.move_to(DOWN * 10)self.add(self.ruler)self.add(self.cp)self.temp_points = []def construct(self):self.add(self.cp)self.play(self.cp.move_niddle_tip_to, ORIGIN, run_time=1)self.wait(0.3)self.set_span(3.6, run_time=1, rate_func=smooth)self.wait(0.5)self.set_compass(DL * 0.5, UR * 0.5, run_time=1, rate_func=there_and_back)arc = Arc(color=GREY_E)self.set_compass_to_draw_arc(arc)self.draw_arc_by_compass(arc)self.wait()def set_span(self, s, run_time=1, rate_func=smooth):s_old = self.cp.get_span()n = int(run_time * self.camera.frame_rate)dt = 1/self.camera.frame_ratet_series = np.linspace(1, n, n)/n# s_series = s_old + rate_func(t_series) * (s - s_old)s_series = [s_old + rate_func(t_series[i]) * (s - s_old) for i in range(n)]for i in range(n):self.cp.set_span(s_series[i])self.wait(dt)def set_compass_direction(self, start, end, run_time=1, rate_func=smooth):vect = end - starta = np.angle(R3_to_complex(vect))c_old, p_old = self.cp.get_niddle_tip(), self.cp.get_pen_tip()a_old = np.angle(R3_to_complex(p_old - c_old))n = int(run_time * self.camera.frame_rate)dt = 1/self.camera.frame_ratet_series = np.linspace(1, n, n)/nc_series = [c_old + rate_func(t_series[i]) * (start - c_old) for i in range(n)]delta_a = (a - a_old)/nfor i in range(n):self.bring_to_front(self.cp)self.cp.move_niddle_tip_to(c_series[i])self.cp.rotate_about_niddle_tip(delta_a)self.wait(dt)def set_compass(self, center, pen_tip, run_time=1, rate_func=smooth, emphasize_dot=False):if emphasize_dot:run_time -= 0.15c_old, p_old = self.cp.get_niddle_tip(), self.cp.get_pen_tip()n = int(run_time * self.camera.frame_rate)dt = 1/self.camera.frame_ratet_series = np.linspace(1, n, n)/n# s_series = s_old + rate_func(t_series) * (s - s_old)c_series = [c_old + rate_func(t_series[i]) * (center - c_old) for i in range(n)]p_series = [p_old + rate_func(t_series[i]) * (pen_tip - p_old) for i in range(n)]for i in range(n):self.bring_to_front(self.cp)self.cp.set_compass(c_series[i], p_series[i])self.wait(dt)if emphasize_dot:self.emphasize_dot([center, pen_tip], run_time=0.15)def set_compass_(self, center, pen_tip, adjust_angle=0, run_time=1, rate_func=smooth, emphasize_dot=False):vect = center - pen_tipa = np.angle(R3_to_complex(vect)) + adjust_angles = get_norm(vect)c_old, p_old, s_old = self.cp.get_niddle_tip(), self.cp.get_pen_tip(), self.cp.get_span()a_old = np.angle(R3_to_complex(p_old - c_old))if emphasize_dot:run_time -= 0.15n = int(run_time * self.camera.frame_rate)dt = 1/self.camera.frame_ratet_series = np.linspace(1, n, n)/nc_series = [c_old + rate_func(t_series[i]) * (center - c_old) for i in range(n)]delta_a = (a - a_old)/ns_series = [s_old + rate_func(t_series[i]) * (s - s_old) for i in range(n)]for i in range(n):self.bring_to_front(self.cp)self.cp.move_niddle_tip_to(c_series[i])self.cp.rotate_about_niddle_tip(delta_a)self.cp.set_span(s_series[i])self.wait(dt)if emphasize_dot:self.emphasize_dot([center, pen_tip], run_time=0.15)def set_compass_to_draw_arc(self, arc, **kwargs):self.set_compass(arc.arc_center, arc.get_start(), **kwargs)def set_compass_to_draw_arc_(self, arc, **kwargs):self.set_compass_(arc.arc_center, arc.get_start(), **kwargs)def draw_arc_by_compass(self, arc, is_prepared=True, run_time=1, rate_func=smooth, reverse=False, add_center=False, **kwargs):self.bring_to_front(self.cp)if not is_prepared: self.set_compass_to_draw_arc(arc, run_time=0.5)theta = arc.angle if not reverse else -1 * arc.angleself.play(Rotating(self.cp, angle=theta, about_point=self.cp.get_niddle_tip()), ShowCreation(arc), rate_func=rate_func, run_time=run_time)if add_center:d = Dot(self.cp.get_niddle_tip(), **self.dot_config).scale(0.5)self.temp_points.append(d)self.add(d)def emphasize_dot(self, pos, add_dot=False, size=1.2, run_time=0.2, **kwargs):if type(pos) == list:d = VGroup(*[Dot(pos[i], radius=size/2, color=GREY_C, fill_opacity=0.25).scale(0.25) for i in range(len(pos))])else:d = Dot(pos, radius=size/2, color=GREY_C, fill_opacity=0.25).scale(0.25)self.add(d)if type(pos) == list:self.play(d[0].scale, 4, d[1].scale, 4, rate_func=linear, run_time=run_time)else:self.play(d.scale, 4, rate_func=linear, run_time=run_time)self.remove(d)if add_dot:if type(pos) == list:dot = VGroup(*[Dot(pos[i],**kwargs) for i in range(len(pos))])else:dot = Dot(pos, **kwargs)self.add(dot)return dotdef set_ruler(self, pos1, pos2, run_time=1, rate_func=smooth):p1, p2 = self.ruler[-1].get_vertices()[1], self.ruler[-1].get_vertices()[0]c12 = (p1 + p2) / 2center = (pos1 + pos2)/2self.bring_to_front(self.ruler)self.play(self.ruler.shift, center - c12, run_time=run_time/2, rate_func=rate_func)self.play(Rotating(self.ruler, angle=np.angle(R3_to_complex(pos2 - pos1)) - np.angle(R3_to_complex(p2 - p1)), about_point=center), run_time=run_time/2, rate_func=rate_func)def draw_line(self, pos1, pos2, is_prepared=True, run_time=1.2, rate_func=smooth, pre_time=0.8):if not is_prepared: self.set_ruler(pos1, pos2, run_time=pre_time)self.dot.move_to(pos1)self.emphasize_dot(pos1, run_time=0.15)self.add(self.dot)l = Line(pos1, pos2, **self.line_config)self.play(ShowCreation(l), self.dot.move_to, pos2, run_time=run_time-0.3, rate_func=rate_func)self.emphasize_dot(pos2, run_time=0.15)self.remove(self.dot)return ldef draw_line_(self, l, is_prepared=True, run_time=1.2, rate_func=smooth):pos1, pos2 = l.get_start(), l.get_end()if not is_prepared: self.set_ruler(pos1, pos2, run_time=0.5)self.dot.move_to(pos1)self.emphasize_dot(pos1, run_time=0.15)self.add(self.dot)# l = Line(pos1, pos2, **self.line_config)self.play(ShowCreation(l), self.dot.move_to, pos2, run_time=run_time-0.3, rate_func=rate_func)self.emphasize_dot(pos2, run_time=0.15)self.remove(self.dot)return ldef put_aside_ruler(self, direction=DOWN, run_time=0.5):self.bring_to_front(self.ruler)self.play(self.ruler.move_to, direction * 15, run_time=run_time)def put_aside_compass(self, direction=DOWN, run_time=0.5):self.bring_to_front(self.cp)self.play(self.cp.move_to, direction * 15, run_time=run_time)def get_length_label(self, p1, p2, text='', reverse_label=False, add_bg=False, bg_color=WHITE):l = Line(p1, p2)b = Brace(l, direction=complex_to_R3(np.exp(1j * (l.get_angle()+PI/2 * (1 -2 * float(reverse_label))))), **self.brace_config)t = Text(text, **self.text_config).scale(0.2)if add_bg:bg = SurroundingRectangle(t, fill_color=bg_color, fill_opacity=0.6, stroke_opacity=0).set_height(t.get_height() + 0.05, stretch=True).set_width(t.get_width() + 0.05, stretch=True)b.put_at_tip(bg, buff=0.0)b.put_at_tip(t, buff=0.05)return b, bg, telse:b.put_at_tip(t, buff=0.05)return b, tdef set_compass_and_show_span(self, p1, p2, run_time=1, show_span_time=[0.4, 0.3, 0.9, 0.4], text='', reverse_label=False, add_bg=True, **kwargs):self.set_compass(p1, p2, run_time=run_time, **kwargs)bt = self.get_length_label(p1, p2, text=text, reverse_label=reverse_label, add_bg=add_bg)b, t = bt[0], bt[-1]st = show_span_timeself.play(ShowCreation(b), run_time=st[0])if add_bg:self.add(bt[1])self.play(FadeIn(t), run_time=st[1])else:self.play(FadeIn(t), run_time=st[1])self.wait(st[2])self.play(FadeOut(VGroup(*bt)), run_time=st[3])return btdef set_compass_and_show_span_(self, p1, p2, run_time=1, show_span_time=[0.4, 0.3, 0.9, 0.4], text='', reverse_label=False, add_bg=True, **kwargs):self.set_compass_(p1, p2, run_time=run_time, **kwargs)bt = self.get_length_label(p1, p2, text=text, reverse_label=reverse_label)b, t = bt[0], bt[-1]st = show_span_timeself.play(ShowCreation(b), run_time=st[0])if add_bg:self.add(bt[1])self.play(FadeIn(t), run_time=st[1])else:self.play(FadeIn(t), run_time=st[1])self.wait(st[2])self.play(FadeOut(VGroup(*bt)), run_time=st[3])return btdef highlight_on(self, *mobjects, to_front=True, stroke_config={'color': '#66CCFF', 'width': 4}, run_time=1, **kwargs):self.highlight = VGroup(*mobjects)self.play(self.highlight.set_stroke, stroke_config, run_time=run_time, **kwargs)if to_front:self.bring_to_front(self.highlight)self.bring_to_front(self.cp, self.ruler)def highlight_off(self, *mobjects):passdef show_arc_info(self, arc, time_list=[0.5, 0.2, 0.3]):c, r, s, a, ps, pe = arc.arc_center, arc.radius, arc.start_angle, arc.angle, arc.get_start(), arc.get_end()d_center = Dot(c, radius=0.08, color=PINK)r1, r2 = DashedLine(c, ps, stroke_width=3.5, stroke_color=PINK), DashedLine(c, pe , stroke_width=3.5, stroke_color=PINK)arc_new = Arc(arc_center=c, radius=r, start_angle=s, angle=a, stroke_width=8, stroke_color=RED)self.play(ShowCreation(arc_new), run_time=time_list[0])self.play(FadeIn(arc_new), run_time=time_list[1])self.play(ShowCreation(r1), ShowCreation(r2), run_time=time_list[2])class Test_01(Scene):def construct(self):logo = Apple_logo()kl = logo.key_linesol = logo.outline_by_arcsself.play(ShowCreation(kl), run_time=6)self.wait(0.25)# self.play(ShowCreation(ol), run_time=4)# self.wait(0.8)self.play(DrawBorderThenFill(logo.shape_1, stroke_width=logo.logo_stroke), run_time=3)self.play(DrawBorderThenFill(logo.shape_2, stroke_width=logo.logo_stroke), run_time=1.6)self.wait(0.2)self.play(Uncreate(kl), run_time=1.5)self.wait(2)class Test_compass(Scene):def construct(self):coord = NumberPlane(axis_config={"stroke_color": GREY_D}, background_line_style={"stroke_color": BLUE_C},)self.add(coord)cp = Compass(stroke_color=PINK)c = Circle(radius=cp.get_span())self.add(cp)self.wait()self.play(cp.move_niddle_tip_to, ORIGIN, run_time=1)self.wait()self.bring_to_front(cp)self.play(Rotating(cp, about_point=cp.get_niddle_tip()), ShowCreation(c), rate_func=smooth, run_time=1.5)self.bring_to_front(cp)c1 = Circle(stroke_color=GREY_E)# self.play(cp.set_span, 1, run_time=1)# cp.set_span(1)# self.wait()# self.play(Rotating(cp, about_point=cp.get_niddle_tip()), ShowCreation(c1), rate_func=smooth, run_time=1.5)self.bring_to_front(cp)# self.play(cp.set_compass, DL * 2, UR * 2, run_time=1.5)cp.set_compass(DL * 2, UR * 2)self.wait(2)class Test_1(DrawingScene):def construct(self):s = 0.5# axis = Axes(x_range=[-30, 30, 1], y_range=[-20, 20, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s)# logo = Apple_logo(create_shape=False, coord_center=ORIGIN, scale=s*2)# self.add(axis, logo.c1)c = Circle(color=BLACK)c1 = Circle(color=BLACK)c2 = Circle(color=BLACK)self.add(c, c1, c2)self.wait()self.play(ApplyMethod(c.shift, RIGHT * 4, run_time=1),ApplyMethod(c1.shift, RIGHT * 4, run_time=2),ApplyMethod(c2.shift, RIGHT * 4, run_time=4))class Step_0(DrawingScene):'''画出基本的直角坐标系作为辅助'''def construct(self):s = 0.5# coord = NumberPlane(x_range=[-15, 15, 1], y_range=[-15, 15, 1], axis_config={"stroke_color": GREY_D}, background_line_style={"stroke_color": BLUE_C},).scale(s)# logo = Apple_logo(create_shape=False, coord_center=ORIGIN, scale=s)self.cp.move_to(UP * 10)# self.add(coord)self.wait()self.play(FadeIn(self.ruler))l1 = self.draw_line(LEFT * 4.5, RIGHT * 4.5, is_prepared=False)self.put_aside_ruler()self.wait()arc_1 = Arc(arc_center=LEFT * 2, radius=3, start_angle=-PI/3, angle=2*PI/3, **self.line_config)arc_2 = Arc(arc_center=RIGHT * 2, radius=3, start_angle=2 * PI/3, angle=2*PI/3, **self.line_config)self.play(self.cp.move_to, ORIGIN)self.wait(0.2)self.set_compass_to_draw_arc(arc_1, emphasize_dot=True)self.draw_arc_by_compass(arc_1)self.wait(0.1)self.set_compass_to_draw_arc_(arc_2, adjust_angle=PI, emphasize_dot=True)self.draw_arc_by_compass(arc_2)self.wait(0.2)self.put_aside_compass()self.set_ruler(UP, DOWN)l2 = self.draw_line(UP * 3, DOWN * 3)self.put_aside_ruler(direction=LEFT)self.wait(0.6)self.play(FadeOut(arc_1), FadeOut(arc_2), l1.scale, 2, l2.scale, 2, run_time=1.2)self.wait(0.8)c1 = Circle(radius=1*s, **self.line_config)# c2_l = Circle(arc_center=6*s*LEFT, radius=5*s, **self.line_config)# c2_r = Arc(arc_center=6*s*RIGHT, radius=5*s, start_angle=PI, angle=-TAU, **self.line_config)self.set_compass_to_draw_arc(c1, run_time=1.2)self.wait(0.12)self.draw_arc_by_compass(c1)self.wait(0.2)arcs = VGroup(*[Arc(arc_center=i*s*RIGHT, radius=1*s, start_angle=-PI/6, angle=PI/3, **self.line_config) for i in range(1, 7)])for i in range(6):self.set_compass_to_draw_arc(arcs[i], run_time=0.32, emphasize_dot=False)self.draw_arc_by_compass(arcs[i], run_time=0.5)self.wait(0.6)axis = Axes(x_range=[-15, 15, 1], y_range=[-10, 10, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5}, ).scale(s)dot = Dot(color=GREY_E, radius=0.06)self.play(FadeIn(axis), FadeIn(dot), FadeOut(l1), FadeOut(l2), FadeOut(self.cp), FadeOut(arcs))self.wait(2)class Step_1(DrawingScene):'''画出...'''def construct(self):s = 0.5axis = Axes(x_range=[-30, 30, 1], y_range=[-20, 20, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s)logo = Apple_logo(create_shape=False, coord_center=ORIGIN, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.06}d1 = Dot(**dot_config)self.add(axis, logo.c1, d1)self.wait()d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)# self.set_compass_to_draw_arc(logo.c5_l, emphasize_dot=True)self.set_compass_and_show_span(logo.c5_l.get_center(), logo.c5_l.get_start(), reverse_label=True, text='r=5')self.wait(0.1)self.draw_arc_by_compass(logo.c5_l)self.cp.reverse_tip()logo.c5_r.flip()self.wait(0.15)self.add(d5_l)self.set_compass_to_draw_arc(logo.c5_r, emphasize_dot=True)# self.set_compass_to_draw_arc_(logo.c5_r, emphasize_dot=True)self.draw_arc_by_compass(logo.c5_r, reverse=True)self.wait(0.1)self.add(d5_r)self.put_aside_compass(direction=UP)self.wait(0.4)self.play(VGroup(*self.mobjects).scale, 0.6, {'about_point': ORIGIN}, run_time=1.2)self.play(VGroup(*self.mobjects).shift, UP * 2, run_time=1.2)# s_vg = VGroup(axis, logo.c5_l, logo.c5_r, logo.c1)# s_vg2 = logo.key_lines.remove(logo.c5_l, logo.c5_r, logo.c1)# self.play(s_vg.scale, 0.5, {'about_point': ORIGIN}, run_time=1.2)# s_vg2.scale(0.5, about_point=ORIGIN)# self.wait(0.4)# self.set_compass_(logo.c8_d.get_center(), logo.c8_d.get_start(), emphasize_dot=True)# # self.set_compass_to_draw_arc(logo.c8_d, emphasize_dot=True)# self.draw_arc_by_compass(logo.c8_d)self.wait(2)class Step_11(DrawingScene):CONFIG = {'compass_config':{'stroke_color': GREY_E,'fill_color': WHITE,'stroke_width': 2,'leg_length': 4,'leg_width': 0.12,'r': 0.2,'depth_test': True,},'add_ruler': True,}def construct(self):s = 0.5 * 0.6axis = Axes(x_range=[-30, 30, 1], y_range=[-25, 25, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s).shift(UP * 2)logo = Apple_logo(create_shape=False, coord_center=UP * 2, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.03}d1 = Dot(logo.c1.get_center(), **dot_config)d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)self.add(axis, logo.c1, logo.c5_l, logo.c5_r, d1, d5_l, d5_r)self.wait()self.cp.move_to(DOWN * 10)self.cp.rotate_about_niddle_tip(PI)arc_13_l = Arc(arc_center=logo.c5_l.get_center(), radius=13 * s, start_angle=3*PI/2, angle=PI/4, **self.line_config)arc_13_r = Arc(arc_center=logo.c5_r.get_center(), radius=13 * s, start_angle=3*PI/2-PI/4, angle=PI/4 + PI/2, **self.line_config)self.highlight_on(logo.c5_l, logo.c5_r)self.wait(0.15)self.set_compass_and_show_span(logo.c5_l.get_center(), logo.c5_l.get_center() + (5 + 8) * s * LEFT, reverse_label=True, text='r=5+8=13', run_time=1.4)self.wait(0.4)self.set_compass_to_draw_arc_(arc_13_l, adjust_angle=PI, rate_func=linear, run_time=0.6)self.draw_arc_by_compass(arc_13_l, rate_func=linear, run_time=0.3)self.wait(0.12)self.set_compass_(logo.c5_r.get_center(), logo.c5_r.get_center() + (5 + 8) * s * LEFT, adjust_angle=PI, emphasize_dot=True, run_time=1.4)self.wait(0.2)self.set_compass_to_draw_arc_(arc_13_r, adjust_angle=-PI, rate_func=linear, run_time=0.6)self.draw_arc_by_compass(arc_13_r, rate_func=linear, run_time=0.9)self.wait(0.6)self.set_compass_and_show_span(axis.c2p(11, 0), axis.c2p(19, 0), reverse_label=True, text='r=8', run_time=1.2)self.wait(0.15)self.set_compass_to_draw_arc_(logo.c8_d.scale(0.995), adjust_angle=-PI, run_time=1.)self.draw_arc_by_compass(logo.c8_d, run_time=1.2)d8_d = Dot(logo.c8_d.get_center(), **dot_config)self.add(d8_d)self.wait(0.2)self.put_aside_compass(UP * 0.5, run_time=1.)self.play(FadeOut(arc_13_l), FadeOut(arc_13_r))self.remove(self.cp)self.wait(0.4)self.play(logo.c5_r.set_stroke, {'color': GREY_E, 'width': 2.5}, logo.c5_l.set_stroke, {'color': GREY_E, 'width': 2.5}, run_time=0.6)self.wait(0.2)self.play(VGroup(*self.mobjects).shift, DOWN * 4, run_time=1.25)self.wait(2)class Step_12(DrawingScene):def construct(self):s = 0.5 * 0.6axis = Axes(x_range=[-30, 30, 1], y_range=[-30, 30, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s).shift(DOWN * 2)logo = Apple_logo(create_shape=False, coord_center=DOWN * 2, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.03}d1 = Dot(logo.c1.get_center(), **dot_config)d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)self.add(axis, logo.c1, logo.c5_l, logo.c5_r, logo.c8_d.scale(0.995), d1, d5_l, d5_r)self.set_compass_and_show_span(axis.c2p(0, 13), axis.c2p(0,0), emphasize_dot=True, reverse_label=True, text='r=13', run_time=2)self.wait(0.6)logo.c13.rotate(-PI/2)self.set_compass_to_draw_arc(logo.c13, emphasize_dot=True)self.wait(0.2)d13 = Dot(logo.c13.get_center(), **dot_config)a_13 = Arc(arc_center=logo.c8_d.get_center() + UP * 8 * s, start_angle=PI/2 + PI/20, radius=13 * s, angle=-PI/10, **self.line_config)self.cp.reverse_tip()self.set_compass_to_draw_arc(a_13, run_time=0.25)self.draw_arc_by_compass(a_13, run_time=0.5)self.add(d13)self.wait(0.25)self.put_aside_compass(RIGHT * 0.8, run_time=1.2)self.wait(0.6)self.play(VGroup(*self.mobjects).scale, 0.8, {'about_point': axis.c2p(0,0)}, run_time=1.2)self.wait(2)class Step_2(DrawingScene):"""画出辅助正方形"""CONFIG = {'compass_config':{'stroke_color': GREY_E,'fill_color': WHITE,'stroke_width': 2,'leg_length': 4,'leg_width': 0.12,'r': 0.2,'depth_test': True,},}def construct(self):s = 0.5 * 0.6 * 0.8axis = Axes(x_range=[-40, 40, 1], y_range=[-36, 36, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s).shift(DOWN * 2)logo = Apple_logo(create_shape=False, coord_center=DOWN * 2, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.03}d1 = Dot(logo.c1.get_center(), **dot_config)d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)a_13 = Arc(arc_center=logo.c8_d.get_center() + UP * 8 * s, start_angle=PI/2 + PI/30, radius=13 * s, angle=-PI/15, **self.line_config)d13 = Dot(logo.c13.get_center(), **dot_config)self.add(axis, logo.c1, logo.c5_l, logo.c5_r, logo.c8_d.scale(0.995), a_13, d1, d5_l, d5_r, d13)self.wait()self.play(FadeIn(self.ruler))self.wait(0.2)dl, dr = logo.c5_l.get_center() * 8/13 + logo.c8_d.get_center() * 5/13, logo.c5_r.get_center() * 8/13 + logo.c8_d.get_center() * 5/13self.set_ruler(dl, dr)self.emphasize_dot([dl, dr], run_time=0.2)# l = Line(dl + LEFT * 4.5, dr + RIGHT *4.5, **self.line_config)self.wait(0.5)l = self.draw_line(dl + LEFT * 3.6, dr + RIGHT * 3.6, run_time=1.25)self.put_aside_ruler(DOWN * 0.4)self.wait(0.1)dm = (dl+dr)/2self.set_compass(d13.get_center(), dm, emphasize_dot=True, run_time=1.5)self.wait(0.2)a_13_r = Arc(arc_center=dm, radius=self.cp.get_span(), start_angle=10 * DEGREES, angle=-20 * DEGREES, **self.line_config)a_13_l = Arc(arc_center=dm, radius=self.cp.get_span(), start_angle=190 * DEGREES, angle=-20 * DEGREES, **self.line_config)# a_13_u = Arc(arc_center=d13.get_center(), radius=self.cp.get_span(), start_angle=100 * DEGREES, angle=20 * DEGREES, **self.line_config)l_square = get_norm(d13.get_center()-dm)circle = Circle(arc_center=d13.get_center(), radius=l_square * np.sqrt(2), **self.line_config).rotate(-PI/4)self.cp.reverse_tip()self.set_compass_to_draw_arc_(a_13_l, adjust_angle=PI, run_time=0.64)self.draw_arc_by_compass(a_13_l, run_time=0.12, rate_func=linear)self.set_compass_to_draw_arc_(a_13_r, adjust_angle=PI, run_time=0.96, rate_func=linear)self.draw_arc_by_compass(a_13_r, run_time=0.12, rate_func=linear)self.wait(0.25)self.cp.reverse_tip()self.set_compass_to_draw_arc(circle, run_time=1.2, emphasize_dot=True)self.wait(0.15)self.draw_arc_by_compass(circle, run_time=1.6)self.wait(0.5)self.set_compass(dm + l_square * LEFT, dm + l_square * RIGHT, emphasize_dot=True)self.wait(0.2)self.cp.reverse_tip()arc_r = Arc(arc_center=dm + l_square * RIGHT, radius=l_square * 2, start_angle=100 * DEGREES, angle=-20 * DEGREES, **self.line_config)arc_l = Arc(arc_center=dm + l_square * LEFT, radius=l_square * 2, start_angle=80 * DEGREES, angle=20 * DEGREES, **self.line_config)# self.set_compass_to_draw_arc_(arc_r, adjust_angle=PI, run_time=0.6, rate_func=linear)self.play(Rotating(self.cp, angle=-80 * DEGREES, about_point=self.cp.get_niddle_tip(), rate_func=linear), run_time=0.6)self.draw_arc_by_compass(arc_r, run_time=0.15, rate_func=linear)self.wait(0.2)# self.set_compass_(dm + l_square * RIGHT, dm + l_square * LEFT, emphasize_dot=True)self.play(Rotating(self.cp, angle=100 * DEGREES, about_point=self.cp.get_niddle_tip()), run_time=0.8)self.emphasize_dot([self.cp.get_niddle_tip(), self.cp.get_pen_tip()], run_time=0.15)self.wait(0.2)self.cp.reverse_tip()self.set_compass_to_draw_arc_(arc_l, adjust_angle=PI, run_time=0.6, rate_func=linear)self.draw_arc_by_compass(arc_l, run_time=0.15, rate_func=linear)self.wait(0.2)self.put_aside_compass(LEFT)self.wait(0.6)dots = [dm + l_square * RIGHT, dm + l_square * (RIGHT + 2 * UP), dm + l_square * (LEFT + 2 * UP), dm + l_square * LEFT]# self.set_ruler(dots[0], dots[1])l_r = self.draw_line(dots[0], dots[1], is_prepared=False, pre_time=0.75)self.wait(0.2)l_u = self.draw_line(dots[1], dots[2], is_prepared=False, pre_time=0.75)self.wait(0.2)l_l = self.draw_line(dots[3], dots[2], is_prepared=False, pre_time=0.75)l_d = Line(dots[3], dots[0],**self.line_config)self.wait(0.18)self.put_aside_ruler(LEFT * 0.7)self.wait(0.9)self.play(Transform(l, l_d, run_time=0.9),FadeOut(a_13_r, run_time=0.6),# FadeOut(a_13, run_time=0.8),FadeOut(a_13_l, run_time=0.8),FadeOut(arc_r, run_time=1.),FadeOut(arc_l, run_time=1.2),FadeOut(circle, run_time=1.4))self.wait(0.75)self.play(VGroup(*self.mobjects).scale, 0.8, {'about_point': axis.c2p(0,0)})self.wait(2)class Step_3(DrawingScene):CONFIG = {'add_ruler': True,}def construct(self):s = 0.5 * 0.6 * 0.8 * 0.8axis = Axes(x_range=[-40, 40, 1], y_range=[-36, 36, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s).shift(DOWN * 2)logo = Apple_logo(create_shape=False, coord_center=DOWN * 2, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.03}d1 = Dot(logo.c1.get_center(), **dot_config)d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)a_13 = Arc(arc_center=logo.c8_d.get_center() + UP * 8 * s, start_angle=PI/2 + PI/30, radius=13 * s, angle=-PI/15, **self.line_config)d13 = Dot(logo.c13.get_center(), **dot_config)dl, dr = logo.c5_l.get_center() * 8/13 + logo.c8_d.get_center() * 5/13, logo.c5_r.get_center() * 8/13 + logo.c8_d.get_center() * 5/13dm = (dl+dr)/2l_square = get_norm(d13.get_center()-dm)dots = [dm + l_square * RIGHT, dm + l_square * (RIGHT + 2 * UP), dm + l_square * (LEFT + 2 * UP), dm + l_square * LEFT]square = Polygon(*dots, **self.line_config)before = VGroup(axis, logo.c1, logo.c5_l, logo.c5_r, logo.c8_d.scale(0.995), a_13, d1, d5_l, d5_r, d13, square)self.add(before)self.wait()dots = square.get_vertices()r1 = l_square * 0.7### left part ###a_1 = Arc(arc_center=dots[2], radius=r1, start_angle=PI/3, angle=-2 * PI/3, **self.line_config)a_2 = Arc(arc_center=(dots[1] +dots[2])/2, radius=r1, start_angle=2 * PI/3, angle=2 * PI/3, **self.line_config)self.cp.flip(axis=RIGHT)self.set_compass_to_draw_arc(a_1)self.draw_arc_by_compass(a_1)self.wait(0.15)self.cp.reverse_tip()self.set_compass_to_draw_arc_(a_2, adjust_angle=PI)self.draw_arc_by_compass(a_2)self.put_aside_compass(RIGHT * 0.8, run_time=0.64)pm = (dots[2] + (dots[1] +dots[2])/2)/2pu, pd = pm + 10 * s * UP, pm + 10 * s * DOWNl = self.draw_line(pu, pd, is_prepared=False, pre_time=0.72)self.wait(0.18)self.put_aside_ruler(LEFT * 0.65)logo.c8_l.rotate(PI/2)self.set_compass_and_show_span_(axis.c2p(0,0), axis.c2p(0,8), run_time=1.4, emphasize_dot=True, adjust_angle=PI, text='r=8', reverse_label=True, add_bg=True)self.wait(0.5)self.set_compass_to_draw_arc_(logo.c8_l, adjust_angle=PI, emphasize_dot=True)self.wait(0.2)self.draw_arc_by_compass(logo.c8_l, run_time=1.25, add_center=True)self.wait(0.2)self.put_aside_compass(LEFT * 0.64)self.wait(0.6)### right part ### 同理直接copy吧a_3 = Arc(arc_center=(dots[1] + dots[2])/2, radius=r1, start_angle=PI/3, angle=-2 * PI/3, **self.line_config)a_4 = Arc(arc_center=dots[1], radius=r1, start_angle=2 * PI/3, angle=2 * PI/3, **self.line_config)d8_r = Dot(logo.c8_r.get_center(), **dot_config)pm_r = (dots[1] + (dots[1] +dots[2])/2)/2pu_r, pd_r = pm_r + 10 * s * UP, pm_r + 10 * s * DOWNlogo.c8_r.rotate(PI/2)l02 = Line(pu_r, pd_r, **self.line_config)right_part = VGroup(a_3, a_4, l02, logo.c8_r)right_part_ = right_part.copy().set_stroke(opacity=0.32).shift(l_square * LEFT)self.add(right_part_)self.play(right_part_.shift, l_square * RIGHT)self.wait(0.2)self.play(ShowCreation(a_3), run_time=0.8)self.play(ShowCreation(a_4), run_time=0.8)self.play(ShowCreation(l02), run_time=0.8)self.play(ShowCreation(logo.c8_r), FadeIn(d8_r))self.remove(right_part_)self.wait()self.play(FadeOut(a_4, run_time=0.6), FadeOut(l02, run_time=0.7), FadeOut(a_3, run_time=0.8),FadeOut(a_2, run_time=0.9), FadeOut(l, run_time=1.), FadeOut(a_1, run_time=1.1),)self.wait(0.8)self.play(VGroup(*self.mobjects).scale, 1.25, {'about_point': axis.c2p(0,0)})self.wait(2)class Step_4(DrawingScene):CONFIG = {'add_ruler': True,}def construct(self):s = 0.5 * 0.6 * 0.8axis = Axes(x_range=[-45, 45, 1], y_range=[-36, 36, 1], axis_config={"stroke_color": GREY_D, "stroke_width": 2.5, "stroke_opacity": 1},).scale(s).shift(DOWN * 2)logo = Apple_logo(create_shape=False, coord_center=DOWN * 2, scale=s*2)dot_config = {'color': GREY_E, 'radius': 0.03}d1 = Dot(logo.c1.get_center(), **dot_config)d5_l, d5_r = Dot(logo.c5_l.get_center(), **dot_config), Dot(logo.c5_r.get_center(), **dot_config)
参考文献
1.https://www.tiobe.com/tiobe-index
2.https://baike.baidu.com/item/Python/407313?fr=aladdin
3.https://docs.manim.community/en/stable/installation.html
4.https://docs.manim.org.cn/