A community-maintained Python framework for creating mathematical animations.
DashedVMobject reworked (#1823)
sparshgbc93a2e99afb
diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.pyindex 7cfd749e1..394cd1b0e 100644--- a/manim/mobject/geometry.py+++ b/manim/mobject/geometry.py@@ -1089,10 +1089,8 @@ class DashedLine(Line):Arguments to be passed to :class:`Line`dash_length : :class:`float`, optionalThe length of each individual dash of the line.- dash_spacing : Optional[:class:`float`]- The spacing between the dashes.- positive_space_ratio : :class:`float`, optional- The ratio of empty space to dash space. Range of 0-1.+ dashed_ratio : :class:`float`, optional+ The ratio of dash space to empty space. Range of 0-1.kwargs : AnyAdditional arguments to be passed to :class:`Line`@@ -1107,8 +1105,8 @@ class DashedLine(Line):dashed_1 = DashedLine(config.left_side, config.right_side, dash_length=2.0).shift(UP*2)# normaldashed_2 = DashedLine(config.left_side, config.right_side)- # positive_space_ratio decreased- dashed_3 = DashedLine(config.left_side, config.right_side, positive_space_ratio=0.1).shift(DOWN*2)+ # dashed_ratio decreased+ dashed_3 = DashedLine(config.left_side, config.right_side, dashed_ratio=0.1).shift(DOWN*2)self.add(dashed_1, dashed_2, dashed_3)See Also@@ -1120,18 +1118,16 @@ class DashedLine(Line):self,*args,dash_length=DEFAULT_DASH_LENGTH,- dash_spacing=None,- positive_space_ratio=0.5,+ dashed_ratio=0.5,**kwargs,):self.dash_length = dash_length- self.dash_spacing = (dash_spacing,)- self.positive_space_ratio = positive_space_ratio+ self.dashed_ratio = dashed_ratiosuper().__init__(*args, **kwargs)dashes = DashedVMobject(self,num_dashes=self.calculate_num_dashes(),- positive_space_ratio=positive_space_ratio,+ dashed_ratio=dashed_ratio,)self.clear_points()self.add(*dashes)@@ -1147,14 +1143,10 @@ class DashedLine(Line):20"""- try:- full_length = self.dash_length / self.positive_space_ratio- return int(np.ceil(self.get_length() / full_length))- except ZeroDivisionError:- return 1-- def calculate_positive_space_ratio(self):- return fdiv(self.dash_length, self.dash_length + self.dash_spacing)+ # Minimum number of dashes has to be 2+ return max(+ 2, int(np.ceil((self.get_length() / self.dash_length) * self.dashed_ratio))+ )def get_start(self) -> np.ndarray:"""Returns the start point of the line.@@ -1180,7 +1172,7 @@ class DashedLine(Line):::>>> DashedLine().get_end()- array([0.99871795, 0. , 0. ])+ array([1., 0., 0.])"""if len(self.submobjects) > 0:@@ -1209,7 +1201,7 @@ class DashedLine(Line):::>>> DashedLine().get_last_handle()- array([0.98205128, 0. , 0. ])+ array([0.98333333, 0. , 0. ])"""return self.submobjects[-1].get_points()[-2]diff --git a/manim/mobject/opengl_geometry.py b/manim/mobject/opengl_geometry.pyindex ab4044db5..5835859e9 100644--- a/manim/mobject/opengl_geometry.py+++ b/manim/mobject/opengl_geometry.py@@ -526,30 +526,22 @@ class OpenGLLine(OpenGLTipableVMobject):class OpenGLDashedLine(OpenGLLine):def __init__(- self, *args, dash_length=DEFAULT_DASH_LENGTH, positive_space_ratio=0.5, **kwargs+ self, *args, dash_length=DEFAULT_DASH_LENGTH, dashed_ratio=0.5, **kwargs):self.dash_length = dash_length- self.positive_space_ratio = positive_space_ratio+ self.dashed_ratio = dashed_ratiosuper().__init__(*args, **kwargs)- ps_ratio = self.positive_space_ratio- num_dashes = self.calculate_num_dashes(ps_ratio)+ dashed_ratio = self.dashed_ratio+ num_dashes = self.calculate_num_dashes(dashed_ratio)dashes = OpenGLDashedVMobject(- self, num_dashes=num_dashes, positive_space_ratio=ps_ratio+ self, num_dashes=num_dashes, dashed_ratio=dashed_ratio)self.clear_points()self.add(*dashes)- def calculate_num_dashes(self, positive_space_ratio):- try:- full_length = self.dash_length / positive_space_ratio- return int(np.ceil(self.get_length() / full_length))- except ZeroDivisionError:- return 1-- def calculate_positive_space_ratio(self):- return fdiv(- self.dash_length,- self.dash_length + self.dash_spacing,+ def calculate_num_dashes(self, dashed_ratio):+ return max(+ 2, int(np.ceil((self.get_length() / self.dash_length) * dashed_ratio)))def get_start(self):diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.pyindex d80a15afc..31ad2bef9 100644--- a/manim/mobject/types/opengl_vectorized_mobject.py+++ b/manim/mobject/types/opengl_vectorized_mobject.py@@ -1228,29 +1228,27 @@ class OpenGLCurvesAsSubmobjects(OpenGLVGroup):class OpenGLDashedVMobject(OpenGLVMobject):def __init__(- self, vmobject, num_dashes=15, positive_space_ratio=0.5, color=WHITE, **kwargs+ self, vmobject, num_dashes=15, dashed_ratio=0.5, color=WHITE, **kwargs):self.num_dashes = num_dashes- self.positive_space_ratio = positive_space_ratio+ self.dashed_ratio = dashed_ratiosuper().__init__(color=color, **kwargs)- num_dashes = self.num_dashes- ps_ratio = self.positive_space_ratio+ r = self.dashed_ratio+ n = self.num_dashesif num_dashes > 0:- # End points of the unit interval for division- alphas = np.linspace(0, 1, num_dashes + 1)-- # This determines the length of each "dash"- full_d_alpha = 1.0 / num_dashes- partial_d_alpha = full_d_alpha * ps_ratio-- # Rescale so that the last point of vmobject will- # be the end of the last dash- alphas /= 1 - full_d_alpha + partial_d_alpha+ # Assuming total length is 1+ dash_len = r / n+ if vmobject.is_closed():+ void_len = (1 - r) / n+ else:+ void_len = (1 - r) / (n - 1)self.add(*[- vmobject.get_subcurve(alpha, alpha + partial_d_alpha)- for alpha in alphas[:-1]+ vmobject.get_subcurve(+ i * (dash_len + void_len), i * (dash_len + void_len) + dash_len+ )+ for i in range(n)])# Family is already taken care of by get_subcurvediff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.pyindex cf01ca8d3..3e83b10cd 100644--- a/manim/mobject/types/vectorized_mobject.py+++ b/manim/mobject/types/vectorized_mobject.py@@ -2082,10 +2082,10 @@ class DashedVMobject(VMobject, metaclass=ConvertToOpenGL):circ = DashedVMobject(Circle(radius=r, color=WHITE), num_dashes=dashes)top_row.add(circ)- middle_row = VGroup() # Increasing positive_space_ratio+ middle_row = VGroup() # Increasing dashed_ratiofor ratio in np.arange(1 / 11, 1, 1 / 11):circ = DashedVMobject(- Circle(radius=r, color=WHITE), positive_space_ratio=ratio+ Circle(radius=r, color=WHITE), dashed_ratio=ratio)middle_row.add(circ)@@ -2102,35 +2102,27 @@ class DashedVMobject(VMobject, metaclass=ConvertToOpenGL):"""def __init__(- self, vmobject, num_dashes=15, positive_space_ratio=0.5, color=WHITE, **kwargs+ self, vmobject, num_dashes=15, dashed_ratio=0.5, color=WHITE, **kwargs):self.num_dashes = num_dashes- self.positive_space_ratio = positive_space_ratio+ self.dashed_ratio = dashed_ratiosuper().__init__(color=color, **kwargs)- ps_ratio = self.positive_space_ratio+ r = self.dashed_ratio+ n = self.num_dashesif num_dashes > 0:- # End points of the unit interval for division- alphas = np.linspace(0, 1, num_dashes + 1)-- # This determines the length of each "dash"- full_d_alpha = 1.0 / num_dashes- partial_d_alpha = full_d_alpha * ps_ratio-- # Shifts the alphas and removes the last dash- # to give closed shapes even spacing+ # Assuming total length is 1+ dash_len = r / nif vmobject.is_closed():- alphas += partial_d_alpha / 2- np.delete(alphas, -1)-- # Rescale so that the last point of vmobject will- # be the end of the last dash+ void_len = (1 - r) / nelse:- alphas /= 1 - full_d_alpha + partial_d_alpha+ void_len = (1 - r) / (n - 1)self.add(*[- vmobject.get_subcurve(alpha, alpha + partial_d_alpha)- for alpha in alphas[:-1]+ vmobject.get_subcurve(+ i * (dash_len + void_len), i * (dash_len + void_len) + dash_len+ )+ for i in range(n)])# Family is already taken care of by get_subcurvediff --git a/tests/control_data/graphical_units_data/geometry/DashedVMobjectTest.npz b/tests/control_data/graphical_units_data/geometry/DashedVMobjectTest.npznew file mode 100644index 000000000..dddbbc68eBinary files /dev/null and b/tests/control_data/graphical_units_data/geometry/DashedVMobjectTest.npz differdiff --git a/tests/test_graphical_units/test_geometry.py b/tests/test_graphical_units/test_geometry.pyindex 44a67bc8c..c044a0729 100644--- a/tests/test_graphical_units/test_geometry.py+++ b/tests/test_graphical_units/test_geometry.py@@ -55,6 +55,13 @@ class CirclePointsTest(Scene):self.add(circle)+class DashedVMobjectTest(Scene):+ def construct(self):+ circle = DashedVMobject(Circle(), 12, 0.9)+ line = DashedLine(dash_length=0.5)+ self.add(circle, line)++class DotTest(Scene):def construct(self):dot = Dot()