Last active
October 10, 2021 13:05
-
-
Save PerfectPan/0f4a936100c286ac561aeed7a9c4f9a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import taichi as ti | |
ti.init(arch=ti.gpu) | |
n = 320 | |
pixels = ti.field(dtype=float, shape=(n * 2, n)) | |
# Julia Set: https://en.wikipedia.org/wiki/Julia_set | |
# https://zhuanlan.zhihu.com/p/163166274 | |
@ti.func | |
def complex_sqr(z): | |
return ti.Vector([z[0]**2 - z[1]**2, z[1] * z[0] * 2]) | |
@ti.kernel | |
def paint(t: float): | |
for i, j in pixels: # Parallized over all pixels | |
c = ti.Vector([ti.cos(t), ti.sin(t)]) * 0.7885 | |
z = ti.Vector([i / n - 1, j / n - 0.5]) * 2 # 范围限制在 x - [-2, 2] y - [-1, 1] 之间 | |
iterations = 0 | |
while z.norm() < 20 and iterations < 50: | |
z = complex_sqr(z) + c | |
iterations += 1 | |
pixels[i, j] = 1 - iterations * 0.02 | |
gui = ti.GUI("Julia Set", res=(n * 2, n)) | |
for i in range(1000000): | |
paint(i * 0.01) | |
gui.set_image(pixels) | |
gui.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment