如何创建一个虚线矩形,在 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)))
这就是它目前的样子: