インストール
pip install vpython
Python3.6環境で普通に動いた。
動かしてみる
import vpython as vs
import numpy as np
s1 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.green)
s2 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.yellow)
s3 = vs.sphere(pos=vs.vector(0,0,0),radius=0.1,color=vs.color.red)
s1
原点をぐるぐる周るだけ。
このコードだと近日点移動しまくります。
class Obj:
def __init__(self, x, y, vx, vy, o):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.o = o
def update(self):
self.x += self.vx
self.y += self.vy
self.o.pos = vs.vector(self.x, self.y, 0)
def f(self):
t = np.arctan2(self.y, self.x)
r2 = self.x * self.x + self.y * self.y
F = - 0.5 / r2
self.vx += F * np.cos(t)
self.vy += F * np.sin(t)
b1 = Obj(-4.5, 0.0, 0.05, 0.3, s1)
b2 = Obj(-2.5, 0.0, 0.23, 0.45, s2)
b3 = Obj(-10.5, 0.0, 0.03, 0.15, s3)
b1.update()
b2.update()
b3.update()
for i in range(0, 2000):
vs.rate(10)
b1.f()
b1.update()
b2.f()
b2.update()
b3.f()
b3.update()