A community-maintained Python framework for creating mathematical animations.
geometry.py updates
Grant Sanderson28193b54b382
diff --git a/topics/geometry.py b/topics/geometry.pyindex 81ca1316e..e7fc63d1f 100644--- a/topics/geometry.py+++ b/topics/geometry.py@@ -25,8 +25,8 @@ class Arc(VMobject):anchors = np.array([np.cos(a)*RIGHT+np.sin(a)*UPfor a in np.linspace(- self.start_angle,- self.start_angle + self.angle,+ self.start_angle,+ self.start_angle + self.angle,self.num_anchors)])@@ -56,7 +56,7 @@ class Arc(VMobject):p1, p2 = self.points[-3:-1]# self.points[-2:] did overshootstart_arrow = Arrow(- p1, 2*p2 - p1,+ p1, 2*p2 - p1,tip_length = tip_length,max_tip_length_to_length_ratio = 2.0)@@ -66,12 +66,16 @@ class Arc(VMobject):p4, p3 = self.points[1:3]# self.points[:2] did overshootend_arrow = Arrow(- p3, 2*p4 - p3,+ p3, 2*p4 - p3,tip_length = tip_length,max_tip_length_to_length_ratio = 2.0)self.add(end_arrow.split()[-1])++++self.set_color(self.get_color())return self@@ -96,11 +100,9 @@ class Arc(VMobject):def set_bound_angles(self,start=0,stop=np.pi):self.start_angle = startself.angle = stop - start-+return self--class ArcBetweenPoints(Arc):def __init__(self, start_point, end_point, angle = TAU/4, **kwargs):@@ -128,7 +130,7 @@ class ArcBetweenPoints(Arc):radius = radius,start_angle = start_angle,**kwargs)-+self.move_arc_center_to(arc_center)class CurvedArrow(ArcBetweenPoints):@@ -142,14 +144,12 @@ class CurvedArrow(ArcBetweenPoints):ArcBetweenPoints.__init__(self, end_point, start_point, angle = -angle, **kwargs)self.add_tip(at_start = False, at_end = True)-class CurvedDoubleArrow(ArcBetweenPoints):def __init__(self, start_point, end_point, angle = TAU/4, **kwargs):ArcBetweenPoints.__init__(self, start_point, end_point, angle = angle, **kwargs)self.add_tip(at_start = True, at_end = True)-class Circle(Arc):CONFIG = {"color" : RED,@@ -490,18 +490,20 @@ class Arrow(Line):stroke_color = self.color,stroke_width = 0,)+ tip.add_at_end = add_at_endself.set_tip_points(tip, add_at_end, preserve_normal = False)self.add(tip)if not hasattr(self, 'tip'):- self.tip = []- self.tip.append(tuple((tip, add_at_end)))+ self.tip = VGroup()+ self.tip.match_style(tip)+ self.tip.add(tip)return tipdef add_rectangular_stem(self):self.rect = Rectangle(stroke_width = 0,- fill_color = self.tip[0][0].get_fill_color(),- fill_opacity = self.tip[0][0].get_fill_opacity()+ fill_color = self.tip.get_fill_color(),+ fill_opacity = self.tip.get_fill_opacity())self.add_to_back(self.rect)self.set_stroke(width = 0)@@ -510,7 +512,7 @@ class Arrow(Line):def set_rectangular_stem_points(self):start, end = self.get_start_and_end()vect = end - start- tip_base_points = self.tip[0][0].get_anchors()[1:]+ tip_base_points = self.tip[0].get_anchors()[1:]tip_base = center_of_mass(tip_base_points)tbp1, tbp2 = tip_base_pointsperp_vect = tbp2 - tbp1@@ -571,7 +573,7 @@ class Arrow(Line):return selfdef get_normal_vector(self):- p0, p1, p2 = self.tip[0][0].get_anchors()+ p0, p1, p2 = self.tip[0].get_anchors()result = np.cross(p2 - p1, p1 - p0)norm = np.linalg.norm(result)if norm == 0:@@ -585,7 +587,7 @@ class Arrow(Line):def get_end(self):if hasattr(self, "tip"):- return self.tip[0][0].get_anchors()[0]+ return self.tip[0].get_anchors()[0]else:return Line.get_end(self)@@ -594,7 +596,7 @@ class Arrow(Line):def put_start_and_end_on(self, *args, **kwargs):Line.put_start_and_end_on(self, *args, **kwargs)- self.set_tip_points(self.tip[0][0], preserve_normal = False)+ self.set_tip_points(self.tip[0], preserve_normal = False)self.set_rectangular_stem_points()return self@@ -602,8 +604,7 @@ class Arrow(Line):Line.scale(self, scale_factor, **kwargs)if self.preserve_tip_size_when_scaling:for t in self.tip:- print(t)- self.set_tip_points(t[0], add_at_end=t[1])+ self.set_tip_points(t, add_at_end=t.add_at_end)if self.use_rectangular_stem:self.set_rectangular_stem_points()return self@@ -623,7 +624,12 @@ class Vector(Arrow):class DoubleArrow(Arrow):def init_tip(self):- self.tip = [(self.add_tip(), True), (self.add_tip(add_at_end = False), False)]+ self.tip = VGroup()+ for b in True, False:+ t = self.add_tip(add_at_end = b)+ t.add_at_end = b+ self.tip.add(t)+ self.tip.match_style(self.tip[0])class CubicBezier(VMobject):def __init__(self, points, **kwargs):@@ -681,94 +687,12 @@ class Square(Rectangle):def __init__(self, **kwargs):digest_config(self, kwargs)Rectangle.__init__(- self,+ self,height = self.side_length,width = self.side_length,**kwargs)-class SurroundingRectangle(Rectangle):- CONFIG = {- "color" : YELLOW,- "buff" : SMALL_BUFF,- }- def __init__(self, mobject, **kwargs):- digest_config(self, kwargs)- kwargs["width"] = mobject.get_width() + 2*self.buff- kwargs["height"] = mobject.get_height() + 2*self.buff- Rectangle.__init__(self, **kwargs)- self.move_to(mobject)--class BackgroundRectangle(SurroundingRectangle):- CONFIG = {- "color" : BLACK,- "stroke_width" : 0,- "fill_opacity" : 0.75,- "buff" : 0- }- def __init__(self, mobject, **kwargs):- SurroundingRectangle.__init__(self, mobject, **kwargs)- self.original_fill_opacity = self.fill_opacity-- def pointwise_become_partial(self, mobject, a, b):- self.set_fill(opacity = b*self.original_fill_opacity)- return self-- def get_fill_color(self):- return Color(self.color)--class ScreenRectangle(Rectangle):- CONFIG = {- "width_to_height_ratio" : 16.0/9.0,- "height" : 4,- }- def generate_points(self):- self.width = self.width_to_height_ratio * self.height- Rectangle.generate_points(self)--class FullScreenRectangle(ScreenRectangle):- CONFIG = {- "height" : FRAME_HEIGHT,- }--class FullScreenFadeRectangle(FullScreenRectangle):- CONFIG = {- "stroke_width" : 0,- "fill_color" : BLACK,- "fill_opacity" : 0.7,- }--class PictureInPictureFrame(Rectangle):- CONFIG = {- "height" : 3,- "aspect_ratio" : (16, 9)- }- def __init__(self, **kwargs):- digest_config(self, kwargs)- height = self.height- if "height" in kwargs:- kwargs.pop("height")- Rectangle.__init__(- self,- width = self.aspect_ratio[0],- height = self.aspect_ratio[1],- **kwargs- )- self.scale_to_fit_height(height)--class Cross(VGroup):- CONFIG = {- "stroke_color" : RED,- "stroke_width" : 6,- }- def __init__(self, mobject, **kwargs):- VGroup.__init__(self,- Line(UP+LEFT, DOWN+RIGHT),- Line(UP+RIGHT, DOWN+LEFT),- )- self.replace(mobject, stretch = True)- self.set_stroke(self.stroke_color, self.stroke_width)-class Grid(VMobject):CONFIG = {"height" : 6.0,@@ -794,4 +718,3 @@ class Grid(VMobject):))-