0
0

wslでmanim その24

Last updated at Posted at 2024-09-15

概要

wslでmanimやってみた。
練習問題やってみた。

練習問題

コッホ曲線を描け。

参考にしたページ

成果物

LSystem_ManimCE_v0.18.1.gif

サンプルコード


from manim import *

class LSystem(Scene):
	def setup(self):
		self.rules = {'F': 'F+F--F+F'}
		self.angle = PI / 3
		self.start_rot = 0
		self.initial = 'F'
		self.actions = {}
		self.locations = []
		self.rotations = []
		self.graphs = []
		self.expression = ''
		self.animation = None
		self.weight = 1
		self.iteration = 3
		self.start_loc = (6.0 - 0.3) * LEFT + DOWN
		self.step_time = 0.1
		self.length = 0.5
		self.actions['F'] = self.draw_forward
		self.actions['+'] = self.rotate_forward
		self.actions['-'] = self.rotate_backward
		self.actions['['] = self.push
		self.actions[']'] = self.pop
		self.cur_loc = self.start_loc
		self.cur_rot = self.start_rot
		self.expression = self.initial
		self.animation = lambda x: self.add(x)
	def draw_forward(self):
		o = self.cur_loc
		l = self.length
		a = self.cur_rot
		e = o + l * np.cos(a) * RIGHT + l * np.sin(a) * UP
		self.cur_loc = e
		g = Line(o, e)
		g.stroke_width = self.weight
		self.animation(g)
	def rotate_forward(self):
		self.cur_rot += self.angle
	def rotate_backward(self):
		self.cur_rot -= self.angle
	def push(self):
		self.locations.append(self.cur_loc)
		self.rotations.append(self.cur_rot)
	def pop(self):
		self.cur_loc = self.locations.pop()
		self.cur_rot = self.rotations.pop()
	def generate(self):
		for i in range(self.iteration):
			print(f'generating iteration {i+1}')
			new_exp = ''
			for e in self.expression:
				new_exp += self.rules.get(e, e)
			self.expression = new_exp
			print(f'iteration {i+1} is finished')
	def draw(self):
		count = self.expression.count("F")
		print(f'Total {count} Fs')
		for e in self.expression:
			act = self.actions.get(e, None)
			if act is not None:
				act()
	def construct(self):
		self.setup()
		self.generate()
		self.draw()
		self.wait()





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0