from manim import *
import numpy as np
class PolarPlaneGraphs(Scene):
    """3.4 plot_polar_graph 绘制两条极坐标曲线。
    r=3sin(6θ) 是六瓣玫瑰线，r=2.5(1-sinθ) 是心形线；
    返回值是 ParametricFunction，必须 self.add/play 才会上屏。
    """
    def construct(self):
        title = Text("plot_polar_graph 极坐标函数图像", font="STLiti", font_size=32).to_edge(UP)
        plane = PolarPlane(size=5.4, radius_max=5.5, radius_step=1, azimuth_step=12,
                           background_line_style=dict(stroke_color=BLUE_D,
                                                      stroke_width=1.5,
                                                      stroke_opacity=0.5))
        rose = plane.plot_polar_graph(lambda t: 3 * np.sin(6 * t),
                                     [0, 2 * PI], color=YELLOW, stroke_width=4)
        heart = plane.plot_polar_graph(lambda t: 2.5 * (1 - np.sin(t)),
                                      [0, 2 * PI], color=RED, stroke_width=4)
        legend = VGroup(
            Text("r = 3 sin(6θ)  六瓣玫瑰线", font="STLiti", font_size=21, color=YELLOW),
            Text("r = 2.5 (1 - sinθ)  心形线", font="STLiti", font_size=21, color=RED),
        ).arrange(DOWN, aligned_edge=LEFT, buff=0.18).to_corner(DL)
        self.play(FadeIn(title), run_time=0.6)
        self.play(FadeIn(plane), run_time=1.0)
        self.play(LaggedStart(Create(rose), Create(heart),
                              lag_ratio=0.5, run_time=2.0))
        self.play(FadeIn(legend), run_time=0.8)
        self.wait(3.0)
