# 太极图形课S1-沙堆模型

## 整体结构（Optional）

``````import taichi as ti
ti.init(arch=ti.gpu)

res = 500
scale = 5

pixels = ti.Vector.field(3, ti.uint8, shape = [res, res])
sandpile = ti.field(ti.i32, shape = [int(res/scale), int(res/scale)])
iters = 5

@ti.kernel
def throw_sand():
sandpile[int(res/scale)/2+0.5, int(res/scale)/2+0.5] += 1

@ti.kernel
def render():
color = 0xE7E0AA
for i, j in pixels:
if sandpile[i/5, j/5] == 0:
pixels[i, j] = [171, 182, 155]
elif sandpile[i/5, j/5] == 1:
pixels[i, j] = [234, 0, 118]
elif sandpile[i/5, j/5] == 2:
pixels[i, j] = [230, 178, 0]
elif sandpile[i/5, j/5] == 3:
pixels[i, j] = [0, 81, 28]

@ti.kernel
def evolve():
for i, j in sandpile:
if sandpile[i, j] >= 4:
sandpile[i + 1, j] += sandpile[i, j] / 4
sandpile[i - 1, j] += sandpile[i, j] / 4
sandpile[i, j - 1] += sandpile[i, j] / 4
sandpile[i, j + 1] += sandpile[i, j] / 4
sandpile[i, j] = 0

def main():
gui = ti.GUI("SandPile Model", res)
while gui.running:
for j in range(iters):
throw_sand()
evolve()
render()
gui.set_image(pixels)
gui.show()

if __name__ == '__main__':
main()
``````

## 运行方式

`python3 main.py`

## 代码链接

Code

5 Likes

Hi,

``````@ti.kernel
def evolve():
for i, j in sandpile:
if sandpile[i, j] >= 4:
sandpile[i + 1, j] += sandpile[i, j] / 4
sandpile[i - 1, j] += sandpile[i, j] / 4
sandpile[i, j - 1] += sandpile[i, j] / 4
sandpile[i, j + 1] += sandpile[i, j] / 4
sandpile[i, j] = 0
``````

4 Likes

1 Like