プログラム一覧に戻る
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import sys
Lx = Ly = 38
WIDTH = HEIGHT = 500
x_g = WIDTH // 50
y_g = HEIGHT // 50
SCALE = (WIDTH - 2 * x_g) // Lx
Lx = WIDTH - 2 * x_g
Ly = HEIGHT - 2 * y_g
BACKGROUND_COLOR = (255, 255, 255)
# フォントの設定
if sys.platform == "win32":
font = ImageFont.truetype("arial.ttf", 10)
elif sys.platform == "darwin":
font = ImageFont.truetype("Arial.ttf", 10)
else:
font = None
data = np.loadtxt("kadai1-1.txt")
N = (data[0, :].size - 1) // 4 # 粒子数
t = data[:, 0]
x = SCALE * data[:, 1 : N + 1] + x_g
y = SCALE * data[:, N + 1 : 2 * N + 1] + y_g
R = SCALE
images = []
for k in range(t.size):
img = Image.new("RGB", (WIDTH, HEIGHT), BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
draw.rectangle((x_g, y_g, x_g + Lx, y_g + Ly), fill=(0, 0, 0))
draw.text((9 * WIDTH // 10, 49 * HEIGHT // 50), f"{t[k]:5.1f}", fill=(0, 0, 0), font=font)
for i in range(N):
draw.ellipse((x[k, i] - R, y[k, i] - R, x[k, i] + R, y[k, i] + R), fill=(0, 205, 0))
images.append(img)
images[0].save("kadai1-1.gif", save_all=True, append_images=images, optimize=True, duration=150, loop=1)