LoginSignup
1
0

中学数学の交差する図形のアニメーション

Last updated at Posted at 2023-12-19

はじめに

中学数学でよく見る、図形が交差するときの重なった部分の面積を manim で書いてみました。面積や長さなどは書いていないのであくまでイメージを補完するためのものです。

gif

大半の人はコードに興味ないと思うので結果から。

RightAngleExample_ManimCE_v0.18.0.gif

ソースコード

自分の環境(jupyter lab or jupyter notebook)がある人は最初のセルに下のコードを実行してから

from manim import *
import numpy as np

これ↓をコピペ実行すればできます。
(コメントは見ないで......)

%%manim -qm RightAngleExample
class RightAngleExample(Scene):
    def construct(self):
        vertex_1 = np.array([-1, -1, 0])
        vertex_2 = np.array([-3, -1, 0])
        vertex_3 = np.array([-1, 1, 0])
        line1 = Line(vertex_1, vertex_2)
        line2 = Line(vertex_1, vertex_3)
        line3 = Line(vertex_2, vertex_3)
        # RightTriangle = VGroup(line1, line2, line3)
        # Polygon is a closed shape with vertices with lines connecting them
        RightTriangle = Polygon(vertex_1, vertex_2, vertex_3, color=RED, fill_opacity=0.5)


        # Create the hexagon
        vertex_hex_1 = np.array([0, -1, 0])
        vertex_hex_2 = np.array([2, -1, 0])
        vertex_hex_3 = np.array([2, 0, 0])
        vertex_hex_4 = np.array([1, 0, 0])
        vertex_hex_5 = np.array([1, 1, 0])
        vertex_hex_6 = np.array([0, 1, 0])
        line_hex_1 = Line(vertex_hex_1, vertex_hex_2)
        line_hex_2 = Line(vertex_hex_2, vertex_hex_3)
        line_hex_3 = Line(vertex_hex_3, vertex_hex_4)
        line_hex_4 = Line(vertex_hex_4, vertex_hex_5)
        line_hex_5 = Line(vertex_hex_5, vertex_hex_6)
        line_hex_6 = Line(vertex_hex_6, vertex_hex_1)
        #hexagon = VGroup(line_hex_1, line_hex_2, line_hex_3, line_hex_4, line_hex_5, line_hex_6)
        hexagon = Polygon(vertex_hex_1, vertex_hex_2, vertex_hex_3, vertex_hex_4, vertex_hex_5, vertex_hex_6, color=BLUE, fill_opacity=0.5)
        
        # Define the starting and ending positions
        start_pos = np.array([3, 0, 0])
        middle_pos_1 = np.array([2, 0, 0])
        middle_pos_2 = np.array([1, 0, 0])
        end_pos = np.array([0, 0, 0])
                
        # Set the position of the RightTriangle
        RightTriangle.move_to(start_pos).set_fill(RED, opacity=0.5)
        # Create the animation
        
        self.add(hexagon)
        self.play(RightTriangle.animate.move_to(middle_pos_1), rate_func=linear, run_time=1) 
        self.wait()
        self.play(RightTriangle.animate.move_to(middle_pos_2), rate_func=linear, run_time=1)
        self.wait()
        self.play(RightTriangle.animate.move_to(end_pos), rate_func=linear, run_time=1) # move_to(end_pos) is that center of RightTriangle moves to end_pos 
        self.wait()
1
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
1
0