# How to use the taichi.vec function in taichi

## To help you get started, we’ve selected a few taichi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

yuanming-hu / taichi / examples / difftaichi / billiards.py View on Github
if (t + 1) % interval == 0 and visualize:
canvas.clear(0x3C733F)

for i in range(n_balls):
if i == 0:
color = 0xCCCCCC
elif i == n_balls - 1:
color = 0x3344cc
else:
color = 0xF20530

canvas.circle(tc.vec(

gui.update()
if output:
gui.screenshot('billiards/{}/{:04d}.png'.format(output, t))

compute_loss(steps - 1)
yuanming-hu / taichi / misc / test_gui.py View on Github
import taichi as tc

vec = tc.vec

gui = tc.core.GUI("Test GUI", tc.veci(512, 512))
canvas = gui.get_canvas()
canvas.clear(0xFFFFFF)
canvas.rect(vec(0.3, 0.3), vec(0.6,

while True:
gui.update()
yuanming-hu / taichi / examples / mpm_lagrangian_forces.py View on Github
# Note that we are now differentiating the total energy w.r.t. the particle position.
# Recall that F = - \partial (total_energy) / \partial x
with ti.Tape(total_energy):
# Do the forward computation of total energy and backward propagation for x.grad, which is later used in p2g
compute_total_energy()
# It's OK not to use the computed total_energy at all, since we only need x.grad
p2g()
grid_op()
g2p()

# TODO: why is visualization so slow?
for i in range(n_elements):
for j in range(3):
a, b = vertices[i, j], vertices[i, (j + 1) % 3]
canvas.path(ti.vec(x[a][0], x[a][1]), ti.vec(
for i in range(n_particles):
gui.update()
gui.screenshot("tmp/{:04d}.png".format(f))
ti.profiler_print()
yuanming-hu / taichi / examples / difftaichi / mass_spring_velocity.py View on Github
def get_pt(x):
return tc.vec(x[0], x[1])
yuanming-hu / taichi / examples / mpm_fluid.py View on Github
v[i] = [0, -1]
J[i] = 1

for f in range(200):
canvas.clear(0x112F41)
for s in range(150):
clear_grid()
p2g()
grid_op()
print('here')
g2p()

# TODO: why is visualization so slow?
for i in range(n_particles):
gui.update()
ti.profiler_print()
yuanming-hu / taichi / examples / difftaichi / mass_spring.py View on Github
def get_pt(x):
return tc.vec(x[0], x[1])
yuanming-hu / taichi / examples / difftaichi / mass_spring.py View on Github
total_steps = steps if not output else steps * 2

for t in range(1, total_steps):
compute_center(t - 1)
nn1(t - 1)
nn2(t - 1)
apply_spring_force(t - 1)
if use_toi:
else:

if (t + 1) % interval == 0 and visualize:
canvas.clear(0xFFFFFF)
canvas.path(tc.vec(0, ground_height),

def circle(x, y, color):

for i in range(n_springs):

def get_pt(x):
return tc.vec(x[0], x[1])

a = act[t - 1, i] * 0.5
r = 2
if spring_actuation[i] == 0:
a = 0
c = 0x222222
else:
yuanming-hu / taichi / examples / difftaichi / rigid_body_toi_visualize.py View on Github
canvas.clear(0xFFFFFF)
for t in range(1, total_steps):
if use_toi:
else:

if (t + 1) % interval == 0 and visualize:
color = 0x010101 * min(255, max(0, int(
(1 - t * dt / total_t) * 0.7 * 255)))
canvas.circle(tc.vec(x[t, 0][0],
offset = 0.077
canvas.path(
tc.vec(0.05, ground_height - offset),
tc.vec(0.95,

if output:
gui.screenshot('rigid_body_toi/{}/{:04d}.png'.format(output, i))
gui.update()
yuanming-hu / taichi / examples / difftaichi / mass_spring.py View on Github
total_steps = steps if not output else steps * 2

for t in range(1, total_steps):
compute_center(t - 1)
nn1(t - 1)
nn2(t - 1)
apply_spring_force(t - 1)
if use_toi:
else:

if (t + 1) % interval == 0 and visualize:
canvas.clear(0xFFFFFF)
canvas.path(tc.vec(0, ground_height),

def circle(x, y, color):

for i in range(n_springs):

def get_pt(x):
return tc.vec(x[0], x[1])

a = act[t - 1, i] * 0.5
r = 2
if spring_actuation[i] == 0:
a = 0
c = 0x222222
else:
r = 4
yuanming-hu / taichi / examples / difftaichi / mass_spring_velocity.py View on Github
def circle(x, y, color):
canvas.circle(tc.vec(x, y)).color(rgb_to_hex(color)).radius(7).finish()

## taichi

The Taichi Programming Language

Apache-2.0