Advertisement
NikaGreg

Untitled

Jun 11th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.82 KB | None | 0 0
  1. class Cavalerie(ThreeDScene):
  2.     def construct(self):
  3.         mob_1 = SVGMobject(
  4.             'mob_1.svg', height=4
  5.         ).set_stroke(WHITE).set_fill(TEAL).shift(2 * RIGHT).set_z_index(2)
  6.         mob_2 = SVGMobject(
  7.             'mob_2.svg', height=4
  8.         ).set_stroke(WHITE).set_fill(TEAL).shift(2 * LEFT + 2 * IN).set_z_index(2)
  9.         self.add_fixed_orientation_mobjects(mob_1, mob_2)
  10.  
  11.         area = Polygon(
  12.             [5, 5, -3], [5, -5, -3], [-5, -5, -3], [-5, 5, -3]
  13.         ).set_fill(YELLOW, 0.2).set_stroke(opacity=0).set_z_index(-2)
  14.         # area = Polygon(
  15.         #     [5, 0, 0], [3, -3, 0], [-6, -3, 0], [-4, 0, 0]
  16.         # ).set_fill(YELLOW, 0.2).set_stroke(opacity=0).set_z_index(-2)
  17.  
  18.         area_2 = area.copy()
  19.         un = always_redraw(lambda:
  20.             Intersection(area_2, mob_1, color=GREEN, fill_opacity=1)
  21.         )
  22.         pM = always_redraw(lambda:
  23.             Dot(un.get_center()).set_fill(YELLOW)
  24.         )
  25.  
  26.         self.set_camera_orientation(
  27.             phi = 75 * DEGREES,
  28.             theta = 30 * DEGREES,
  29.             focal_distance=500
  30.         )
  31.         self.add(mob_1, mob_2, area, area_2, un, pM)
  32.         self.play(area_2.animate.shift(4 * OUT), run_time=7)
  33.  
  34. class Hyperboloid(ThreeDScene):
  35.     def construct(self):
  36.         axes = ThreeDAxes(x_range=[-3, 3], y_range=[-3, 3], z_range=[-3, 3])
  37.         # функции
  38.         vertex_coords = [
  39.             [2, 2, 0],
  40.             [2, -2, 0],
  41.             [-2, -2, 0],
  42.             [-2, 2, 0],
  43.             [0, 0, 4]
  44.         ]
  45.         faces_list = [
  46.             [0, 1, 4],
  47.             [1, 2, 4],
  48.             [2, 3, 4],
  49.             [3, 0, 4],
  50.             [0, 1, 2, 3]
  51.         ]
  52.         pyramid = Polyhedron(
  53.             vertex_coords, faces_list
  54.         ).move_to(2 * UL + 1 * IN).set_fill(opacity = 0.5)
  55.         # конус
  56.         h = 4
  57.         r = 2
  58.         cone_surface = Surface(
  59.             lambda u, v: np.array([v * r * np.cos(u), v * r * np.sin(u), (1 - v) * h]),
  60.             u_range=(0, 2 * PI),
  61.             v_range=(0, 1),
  62.             resolution=(16, 32),
  63.             color=BLUE,
  64.         ).shift(2 * DR + 2 * IN).set_fill(opacity = 0.5)
  65.         # плоскость
  66.         area = Rectangle(
  67.             height = 15, width = 10
  68.         ).shift([0, 0, -2]).set_stroke(YELLOW, 1).set_fill(YELLOW, 0.1)
  69.        
  70.         # анимации
  71.         self.set_camera_orientation(
  72.             phi=75 * DEGREES,
  73.             theta=45 * DEGREES
  74.         )
  75.         self.add(cone_surface, pyramid, area)
  76.         self.wait()
  77.         self.play(area.animate.shift([0, 0, 3]), run_time = 4)
  78.         self.wait()
  79.  
  80.  
  81.  
  82. class IntersectionExample(ThreeDScene):
  83.  
  84.     def construct(self):
  85.  
  86.         def param_surface(u, v):
  87.             x = u
  88.             y = v
  89.             z = 0
  90.             return np.array([x, y, z])
  91.  
  92.         # Parameters for the cone
  93.         radius = 1
  94.         height = 2
  95.  
  96.         # Create the cone
  97.         cone = Surface(
  98.             lambda u, v: np.array([
  99.                 radius * v * np.cos(u),
  100.                 radius * v * np.sin(u),
  101.                 height * (1 - v)
  102.             ]),
  103.             u_range=[0, 2 * PI],
  104.             v_range=[0, 1],
  105.             resolution=(20, 20),
  106.         ).set_color(BLUE).set_opacity(0.3)
  107.  
  108.         # Create the plane
  109.         plane = Rectangle().shift(OUT).set_color(TEAL, 0.6).set_fill(TEAL, 0.6)
  110.         # plane = Surface(
  111.         #     param_surface,
  112.         #     u_range=[0, 2 * PI],
  113.         #     v_range=[0, 1],
  114.         #     resolution=(20, 20),
  115.         # ).shift(OUT).set_color(TEAL, 0.6).set_fill(TEAL, 0.6)
  116.  
  117.         inter = Intersection(
  118.             plane,
  119.             cone,
  120.             fill_color = GREEN,
  121.             fill_opacity = 1,
  122.             stroke_color = GREEN,
  123.             stroke_opacity = 1
  124.         )
  125.  
  126.         self.set_camera_orientation(phi=75 * DEGREES, theta=45 * DEGREES)
  127.         self.add(cone, plane, inter)
  128.         self.wait()
  129.         self.play(plane.animate.shift([0, 0, 3]), run_time = 4)
  130.         # self.move_camera(
  131.         #     phi=75 * DEGREES,
  132.         #     theta=375 * DEGREES,
  133.         #     run_time = 10
  134.         # )
  135.  
  136. class IntersectionScene(ThreeDScene):
  137.     def construct(self):
  138.         # Задаем поверхность второго порядка
  139.         radius = 1
  140.         height = 2
  141.  
  142.         surface = Surface(
  143.             lambda u, v: np.array([
  144.                 radius * v * np.cos(u),
  145.                 radius * v * np.sin(u),
  146.                 height * (1 - v)
  147.             ]),
  148.             u_range=[0, 2 * PI],
  149.             v_range=[0, 1],
  150.             resolution=(20, 20),
  151.         ).set_color(TEAL, 0.6).set_fill(TEAL, 0.6)
  152.        
  153.         z = ValueTracker()
  154.         # Задаем плоскость
  155.         plane = always_redraw(lambda:
  156.             Surface(
  157.                 lambda u, v: np.array([u, v, z.get_value()]),
  158.                 u_range=[-2, 2],
  159.                 v_range=[-2, 2],
  160.                 resolution=(20, 20),
  161.             ).set_color(YELLOW, 0.6).set_fill(YELLOW, 0.6)
  162.         )
  163.        
  164.         plane = Surface(
  165.             lambda u, v: np.array([u, v, z.get_value()]),
  166.             u_range=[-2, 2],
  167.             v_range=[-2, 2],
  168.             resolution=(20, 20),
  169.         ).set_color(YELLOW, 0.6).set_fill(YELLOW, 0.6)
  170.        
  171.        
  172.         # Находим пересечение
  173.         intersection = always_redraw(lambda:
  174.             Intersection(
  175.                 surface, plane,
  176.                 fill_color = GREEN,
  177.                 fill_opacity = 1,
  178.                 stroke_color = GREEN,
  179.                 stroke_opacity = 1
  180.             )
  181.         )
  182.        
  183.         self.set_camera_orientation(phi=75 * DEGREES, theta=45 * DEGREES)
  184.         self.add(surface, plane, intersection)
  185.         self.wait()
  186.         self.play(z.animate.set_value(2), run_time = 4)
  187.  
  188.  
  189.  
  190.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement