From 24f312a9a5830b5334637b12f415dcb2797e5053 Mon Sep 17 00:00:00 2001 From: XorUnison Date: Thu, 28 May 2020 11:23:08 +0200 Subject: [PATCH 1/4] ArcBetweenPoints fix/enhancement mobject.py has been slightly changed to fix an issue where points that weren't passed into ArcBetweenPoints as np.array caused manim to fail. geometry.py had ArcBetweenPoints adjusted to support radius instead of angle. Small changes to Arc were necessary to facilitate that .radius returns correct values at all times. --- manim/mobject/geometry.py | 36 ++++++++++++++++++++++++++++++++---- manim/mobject/mobject.py | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py index 5636219560..0b31cae2b6 100644 --- a/manim/mobject/geometry.py +++ b/manim/mobject/geometry.py @@ -1,5 +1,6 @@ import warnings import numpy as np +import math from ..constants import * from ..mobject.mobject import Mobject @@ -245,7 +246,7 @@ def set_pre_positioned_points(self): anchors[1:], ) - def get_arc_center(self): + def get_arc_center(self,warning=True): """ Looks at the normals to the first two anchors, and finds their intersection points @@ -258,13 +259,16 @@ def get_arc_center(self): # Normals n1 = rotate_vector(t1, TAU / 4) n2 = rotate_vector(t2, TAU / 4) + self.failed_to_get_center=False try: return line_intersection( line1=(a1, a1 + n1), line2=(a2, a2 + n2), ) except Exception: - warnings.warn("Can't find Arc center, using ORIGIN instead") + if warning: + warnings.warn("Can't find Arc center, using ORIGIN instead") + self.failed_to_get_center=True return np.array(ORIGIN) def move_arc_center_to(self, point): @@ -278,7 +282,24 @@ def stop_angle(self): class ArcBetweenPoints(Arc): - def __init__(self, start, end, angle=TAU / 4, **kwargs): + """ + Inherits from Arc and additionally takes 2 points between which the arc is spanned. + """ + def __init__(self, start, end, angle=TAU / 4, radius=None, **kwargs): + if radius!=None: + self.radius=radius + if radius < 0: + sign=-2 + radius*=(-1) + else: + sign=2 + halfdist=np.linalg.norm(np.array(start) - np.array(end)) / 2 + if radius < halfdist: + raise ValueError("""ArcBetweenPoints called with a radius that is + smaller than half the distance between the points.""") + arc_height=radius - math.sqrt(radius ** 2 - halfdist ** 2) + angle=math.acos((radius - arc_height) / radius)*sign + Arc.__init__( self, angle=angle, @@ -287,6 +308,13 @@ def __init__(self, start, end, angle=TAU / 4, **kwargs): if angle == 0: self.set_points_as_corners([LEFT, RIGHT]) self.put_start_and_end_on(start, end) + + if radius==None: + center=self.get_arc_center(warning=False) + if not self.failed_to_get_center: + self.radius=np.linalg.norm(np.array(start) - np.array(center)) + else: + self.radius=math.inf class CurvedArrow(ArcBetweenPoints): @@ -847,4 +875,4 @@ class RoundedRectangle(Rectangle): def __init__(self, **kwargs): Rectangle.__init__(self, **kwargs) - self.round_corners(self.corner_radius) \ No newline at end of file + self.round_corners(self.corner_radius) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index edd22af14d..0fb3c41aa3 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -550,7 +550,7 @@ def put_start_and_end_on(self, start, end): curr_vect = curr_end - curr_start if np.all(curr_vect == 0): raise Exception("Cannot position endpoints of closed loop") - target_vect = end - start + target_vect = np.array(end) - np.array(start) self.scale( get_norm(target_vect) / get_norm(curr_vect), about_point=curr_start, From 6207eaef537b88be2b870fb0018f5294f8bd4981 Mon Sep 17 00:00:00 2001 From: XorUnison Date: Thu, 28 May 2020 14:01:13 +0200 Subject: [PATCH 2/4] Adjusted style of conditions --- manim/mobject/geometry.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py index 0b31cae2b6..bff3031179 100644 --- a/manim/mobject/geometry.py +++ b/manim/mobject/geometry.py @@ -286,7 +286,7 @@ class ArcBetweenPoints(Arc): Inherits from Arc and additionally takes 2 points between which the arc is spanned. """ def __init__(self, start, end, angle=TAU / 4, radius=None, **kwargs): - if radius!=None: + if radius is not None: self.radius=radius if radius < 0: sign=-2 @@ -309,7 +309,7 @@ def __init__(self, start, end, angle=TAU / 4, radius=None, **kwargs): self.set_points_as_corners([LEFT, RIGHT]) self.put_start_and_end_on(start, end) - if radius==None: + if radius is None: center=self.get_arc_center(warning=False) if not self.failed_to_get_center: self.radius=np.linalg.norm(np.array(start) - np.array(center)) From e2fc3b9e71f35924b7c52a2c9a15b90a7f46f6d9 Mon Sep 17 00:00:00 2001 From: XorUnison Date: Sun, 31 May 2020 01:22:39 +0200 Subject: [PATCH 3/4] Moved attr --- manim/mobject/geometry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py index bff3031179..7bc1bd4a52 100644 --- a/manim/mobject/geometry.py +++ b/manim/mobject/geometry.py @@ -214,6 +214,7 @@ class Arc(TipableVMobject): def __init__(self, start_angle=0, angle=TAU / 4, **kwargs): self.start_angle = start_angle self.angle = angle + self.failed_to_get_center=False VMobject.__init__(self, **kwargs) def generate_points(self): @@ -259,7 +260,6 @@ def get_arc_center(self,warning=True): # Normals n1 = rotate_vector(t1, TAU / 4) n2 = rotate_vector(t2, TAU / 4) - self.failed_to_get_center=False try: return line_intersection( line1=(a1, a1 + n1), From eda9ba8ff47d93571d40d29f5cd46cdfa5003301 Mon Sep 17 00:00:00 2001 From: XorUnison Date: Sun, 31 May 2020 01:42:01 +0200 Subject: [PATCH 4/4] Set the new attribute to private --- manim/mobject/geometry.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manim/mobject/geometry.py b/manim/mobject/geometry.py index 7bc1bd4a52..d9de953ac4 100644 --- a/manim/mobject/geometry.py +++ b/manim/mobject/geometry.py @@ -214,7 +214,7 @@ class Arc(TipableVMobject): def __init__(self, start_angle=0, angle=TAU / 4, **kwargs): self.start_angle = start_angle self.angle = angle - self.failed_to_get_center=False + self._failed_to_get_center=False VMobject.__init__(self, **kwargs) def generate_points(self): @@ -268,7 +268,7 @@ def get_arc_center(self,warning=True): except Exception: if warning: warnings.warn("Can't find Arc center, using ORIGIN instead") - self.failed_to_get_center=True + self._failed_to_get_center=True return np.array(ORIGIN) def move_arc_center_to(self, point): @@ -311,7 +311,7 @@ def __init__(self, start, end, angle=TAU / 4, radius=None, **kwargs): if radius is None: center=self.get_arc_center(warning=False) - if not self.failed_to_get_center: + if not self._failed_to_get_center: self.radius=np.linalg.norm(np.array(start) - np.array(center)) else: self.radius=math.inf