How to create a dashed rectangle with equally spaced dashes on both edges in manim? I've tried inheriting from Rectangle
and adding dashes the same way as it is implemented for DashedLine
but it produces very different densities on the longer and shorter edges. Here is my attempt (code derived from MIT licensed manim repository):
%%manim HighLevelIntuition -l -q
from manimlib_imports import *
class DashedRectangle(Rectangle):
CONFIG = {
"dash_length": DEFAULT_DASH_LENGTH,
"dash_spacing": None,
"positive_space_ratio": 0.5,
}
def __init__(self, *args, **kwargs):
Rectangle.__init__(self, *args, **kwargs)
ps_ratio = self.positive_space_ratio
num_dashes = self.calculate_num_dashes(ps_ratio)
dashes = DashedVMobject(
self,
num_dashes=num_dashes,
positive_space_ratio=ps_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_arc_length() / full_length
))
except ZeroDivisionError:
return 1
class HighLevelIntuition(Scene):
def construct(self):
self.play(ShowCreation(DashedRectangle(width=10, height=2)))
And that's how it looks like currently: