Как создать пунктирный прямоугольник с одинаково расположенными штрихами по обоим краям в manim?Я попытался унаследовать от Rectangle
и добавить тире таким же образом, как это реализовано для DashedLine
, но это дает очень разные плотности на длинных и коротких краях.Вот моя попытка (код, полученный из MIT-лицензированного репозитория manim):
%%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)))
И вот как это выглядит сейчас:
![enter image description here](https://i.stack.imgur.com/vgV7z.png)