2018-03-30 18:42:32 -07:00
|
|
|
import numpy as np
|
2021-08-21 10:35:29 -07:00
|
|
|
import math
|
|
|
|
import numbers
|
2018-03-30 18:42:32 -07:00
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.mobject import Mobject
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
2019-02-07 15:41:54 -08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import DashedVMobject
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
2019-02-07 21:57:40 -08:00
|
|
|
from manimlib.utils.iterables import adjacent_n_tuples
|
|
|
|
from manimlib.utils.iterables import adjacent_pairs
|
2019-02-07 15:41:54 -08:00
|
|
|
from manimlib.utils.simple_functions import fdiv
|
2020-02-18 22:32:57 -08:00
|
|
|
from manimlib.utils.simple_functions import clip
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.space_ops import angle_of_vector
|
2019-02-07 21:57:40 -08:00
|
|
|
from manimlib.utils.space_ops import angle_between_vectors
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.space_ops import compass_directions
|
2020-02-05 14:46:25 -08:00
|
|
|
from manimlib.utils.space_ops import find_intersection
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.space_ops import get_norm
|
2019-02-07 10:59:04 -08:00
|
|
|
from manimlib.utils.space_ops import normalize
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.space_ops import rotate_vector
|
2020-06-28 10:05:49 -07:00
|
|
|
from manimlib.utils.space_ops import rotation_matrix_transpose
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 10:12:42 -08:00
|
|
|
DEFAULT_DOT_RADIUS = 0.08
|
2019-03-03 07:42:00 -06:00
|
|
|
DEFAULT_SMALL_DOT_RADIUS = 0.04
|
2019-02-07 15:41:54 -08:00
|
|
|
DEFAULT_DASH_LENGTH = 0.05
|
2019-03-03 07:42:00 -06:00
|
|
|
DEFAULT_ARROW_TIP_LENGTH = 0.35
|
2020-06-06 10:56:23 -07:00
|
|
|
DEFAULT_ARROW_TIP_WIDTH = 0.35
|
2019-02-07 10:12:42 -08:00
|
|
|
|
|
|
|
|
2021-08-26 11:43:19 -07:00
|
|
|
# Deprecate?
|
2019-02-07 17:53:11 -08:00
|
|
|
class TipableVMobject(VMobject):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""
|
|
|
|
Meant for shared functionality between Arc and Line.
|
|
|
|
Functionality can be classified broadly into these groups:
|
|
|
|
|
|
|
|
* Adding, Creating, Modifying tips
|
|
|
|
- add_tip calls create_tip, before pushing the new tip
|
|
|
|
into the TipableVMobject's list of submobjects
|
|
|
|
- stylistic and positional configuration
|
|
|
|
|
|
|
|
* Checking for tips
|
|
|
|
- Boolean checks for whether the TipableVMobject has a tip
|
|
|
|
and a starting tip
|
|
|
|
|
|
|
|
* Getters
|
|
|
|
- Straightforward accessors, returning information pertaining
|
|
|
|
to the TipableVMobject instance's tip(s), its length etc
|
|
|
|
"""
|
2019-02-07 17:53:11 -08:00
|
|
|
CONFIG = {
|
2020-06-06 10:56:23 -07:00
|
|
|
"tip_config": {
|
2019-03-03 07:42:00 -06:00
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
2021-06-14 16:13:53 +08:00
|
|
|
"tip_style": 0, # triangle=0, inner_smooth=1, dot=2
|
2020-06-06 10:56:23 -07:00
|
|
|
},
|
|
|
|
"normal_vector": OUT,
|
2019-02-07 17:53:11 -08:00
|
|
|
}
|
2020-02-05 14:46:25 -08:00
|
|
|
|
2019-06-10 03:36:16 +08:00
|
|
|
# Adding, Creating, Modifying tips
|
2020-06-06 10:56:23 -07:00
|
|
|
def add_tip(self, at_start=False, **kwargs):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""
|
|
|
|
Adds a tip to the TipableVMobject instance, recognising
|
|
|
|
that the endpoints might need to be switched if it's
|
|
|
|
a 'starting tip' or not.
|
|
|
|
"""
|
2020-06-06 10:56:23 -07:00
|
|
|
tip = self.create_tip(at_start, **kwargs)
|
2019-03-03 07:42:00 -06:00
|
|
|
self.reset_endpoints_based_on_tip(tip, at_start)
|
|
|
|
self.asign_tip_attr(tip, at_start)
|
2021-06-13 21:07:25 +08:00
|
|
|
tip.set_color(self.get_stroke_color())
|
2019-03-03 07:42:00 -06:00
|
|
|
self.add(tip)
|
|
|
|
return self
|
|
|
|
|
2020-06-06 10:56:23 -07:00
|
|
|
def create_tip(self, at_start=False, **kwargs):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""
|
|
|
|
Stylises the tip, positions it spacially, and returns
|
|
|
|
the newly instantiated tip to the caller.
|
|
|
|
"""
|
2020-06-06 10:56:23 -07:00
|
|
|
tip = self.get_unpositioned_tip(**kwargs)
|
2019-03-03 07:42:00 -06:00
|
|
|
self.position_tip(tip, at_start)
|
|
|
|
return tip
|
|
|
|
|
2020-06-06 10:56:23 -07:00
|
|
|
def get_unpositioned_tip(self, **kwargs):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""
|
|
|
|
Returns a tip that has been stylistically configured,
|
|
|
|
but has not yet been given a position in space.
|
|
|
|
"""
|
2020-06-29 22:53:44 -07:00
|
|
|
config = dict()
|
2020-06-06 10:56:23 -07:00
|
|
|
config.update(self.tip_config)
|
|
|
|
config.update(kwargs)
|
|
|
|
return ArrowTip(**config)
|
2019-03-03 07:42:00 -06:00
|
|
|
|
|
|
|
def position_tip(self, tip, at_start=False):
|
2019-02-07 17:53:11 -08:00
|
|
|
# Last two control points, defining both
|
|
|
|
# the end, and the tangency direction
|
|
|
|
if at_start:
|
|
|
|
anchor = self.get_start()
|
|
|
|
handle = self.get_first_handle()
|
|
|
|
else:
|
|
|
|
handle = self.get_last_handle()
|
|
|
|
anchor = self.get_end()
|
2020-06-06 10:56:23 -07:00
|
|
|
tip.rotate(angle_of_vector(handle - anchor) - PI - tip.get_angle())
|
2019-03-03 07:42:00 -06:00
|
|
|
tip.shift(anchor - tip.get_tip_point())
|
2019-02-07 17:53:11 -08:00
|
|
|
return tip
|
|
|
|
|
|
|
|
def reset_endpoints_based_on_tip(self, tip, at_start):
|
2019-03-03 07:42:00 -06:00
|
|
|
if self.get_length() == 0:
|
|
|
|
# Zero length, put_start_and_end_on wouldn't
|
|
|
|
# work
|
|
|
|
return self
|
2019-06-10 03:36:16 +08:00
|
|
|
|
2019-02-07 17:53:11 -08:00
|
|
|
if at_start:
|
2020-02-05 14:46:25 -08:00
|
|
|
start = tip.get_base()
|
|
|
|
end = self.get_end()
|
2019-02-07 17:53:11 -08:00
|
|
|
else:
|
2020-02-05 14:46:25 -08:00
|
|
|
start = self.get_start()
|
|
|
|
end = tip.get_base()
|
|
|
|
self.put_start_and_end_on(start, end)
|
2019-02-07 17:53:11 -08:00
|
|
|
return self
|
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
def asign_tip_attr(self, tip, at_start):
|
|
|
|
if at_start:
|
|
|
|
self.start_tip = tip
|
|
|
|
else:
|
|
|
|
self.tip = tip
|
|
|
|
return self
|
|
|
|
|
2019-06-10 03:36:16 +08:00
|
|
|
# Checking for tips
|
|
|
|
def has_tip(self):
|
|
|
|
return hasattr(self, "tip") and self.tip in self
|
|
|
|
|
|
|
|
def has_start_tip(self):
|
|
|
|
return hasattr(self, "start_tip") and self.start_tip in self
|
|
|
|
|
|
|
|
# Getters
|
|
|
|
def pop_tips(self):
|
|
|
|
start, end = self.get_start_and_end()
|
|
|
|
result = VGroup()
|
|
|
|
if self.has_tip():
|
|
|
|
result.add(self.tip)
|
|
|
|
self.remove(self.tip)
|
|
|
|
if self.has_start_tip():
|
|
|
|
result.add(self.start_tip)
|
|
|
|
self.remove(self.start_tip)
|
|
|
|
self.put_start_and_end_on(start, end)
|
|
|
|
return result
|
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
def get_tips(self):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""
|
|
|
|
Returns a VGroup (collection of VMobjects) containing
|
|
|
|
the TipableVMObject instance's tips.
|
|
|
|
"""
|
2019-03-03 07:42:00 -06:00
|
|
|
result = VGroup()
|
|
|
|
if hasattr(self, "tip"):
|
|
|
|
result.add(self.tip)
|
|
|
|
if hasattr(self, "start_tip"):
|
|
|
|
result.add(self.start_tip)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_tip(self):
|
2019-06-10 03:36:16 +08:00
|
|
|
"""Returns the TipableVMobject instance's (first) tip,
|
|
|
|
otherwise throws an exception."""
|
2019-03-03 07:42:00 -06:00
|
|
|
tips = self.get_tips()
|
|
|
|
if len(tips) == 0:
|
|
|
|
raise Exception("tip not found")
|
|
|
|
else:
|
|
|
|
return tips[0]
|
|
|
|
|
|
|
|
def get_default_tip_length(self):
|
|
|
|
return self.tip_length
|
|
|
|
|
2019-02-07 17:53:11 -08:00
|
|
|
def get_first_handle(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.get_points()[1]
|
2019-02-07 17:53:11 -08:00
|
|
|
|
|
|
|
def get_last_handle(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.get_points()[-2]
|
2019-02-07 17:53:11 -08:00
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
def get_end(self):
|
|
|
|
if self.has_tip():
|
|
|
|
return self.tip.get_start()
|
|
|
|
else:
|
|
|
|
return VMobject.get_end(self)
|
|
|
|
|
|
|
|
def get_start(self):
|
|
|
|
if self.has_start_tip():
|
|
|
|
return self.start_tip.get_start()
|
|
|
|
else:
|
|
|
|
return VMobject.get_start(self)
|
|
|
|
|
2019-03-03 07:53:15 -06:00
|
|
|
def get_length(self):
|
2019-04-04 14:29:10 -07:00
|
|
|
start, end = self.get_start_and_end()
|
|
|
|
return get_norm(start - end)
|
2019-03-03 07:53:15 -06:00
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
|
2019-02-07 17:53:11 -08:00
|
|
|
class Arc(TipableVMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"radius": 1.0,
|
2020-02-05 14:46:25 -08:00
|
|
|
"n_components": 8,
|
2018-04-06 13:58:59 -07:00
|
|
|
"anchors_span_full_range": True,
|
2019-01-17 14:10:52 -08:00
|
|
|
"arc_center": ORIGIN,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 09:26:18 -08:00
|
|
|
def __init__(self, start_angle=0, angle=TAU / 4, **kwargs):
|
|
|
|
self.start_angle = start_angle
|
2018-01-19 17:30:39 -08:00
|
|
|
self.angle = angle
|
2016-04-17 00:31:38 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2020-06-28 10:05:49 -07:00
|
|
|
self.set_points(Arc.create_quadratic_bezier_points(
|
|
|
|
angle=self.angle,
|
|
|
|
start_angle=self.start_angle,
|
|
|
|
n_components=self.n_components
|
|
|
|
))
|
2019-02-07 09:26:18 -08:00
|
|
|
self.scale(self.radius, about_point=ORIGIN)
|
|
|
|
self.shift(self.arc_center)
|
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
@staticmethod
|
|
|
|
def create_quadratic_bezier_points(angle, start_angle=0, n_components=8):
|
2020-02-05 14:46:25 -08:00
|
|
|
samples = np.array([
|
2020-06-28 10:05:49 -07:00
|
|
|
[np.cos(a), np.sin(a), 0]
|
2016-07-12 15:16:20 -07:00
|
|
|
for a in np.linspace(
|
2020-06-28 10:05:49 -07:00
|
|
|
start_angle,
|
|
|
|
start_angle + angle,
|
|
|
|
2 * n_components + 1,
|
2016-04-17 00:31:38 -07:00
|
|
|
)
|
2018-01-17 15:12:39 -08:00
|
|
|
])
|
2020-06-28 10:05:49 -07:00
|
|
|
theta = angle / n_components
|
2020-02-05 14:46:25 -08:00
|
|
|
samples[1::2] /= np.cos(theta / 2)
|
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
points = np.zeros((3 * n_components, 3))
|
|
|
|
points[0::3] = samples[0:-1:2]
|
|
|
|
points[1::3] = samples[1::2]
|
|
|
|
points[2::3] = samples[2::2]
|
|
|
|
return points
|
2018-01-22 16:30:07 -08:00
|
|
|
|
2018-01-22 16:01:33 -08:00
|
|
|
def get_arc_center(self):
|
2019-02-07 09:26:18 -08:00
|
|
|
"""
|
|
|
|
Looks at the normals to the first two
|
|
|
|
anchors, and finds their intersection points
|
|
|
|
"""
|
|
|
|
# First two anchors and handles
|
2021-01-10 18:51:47 -08:00
|
|
|
a1, h, a2 = self.get_points()[:3]
|
2019-02-07 09:26:18 -08:00
|
|
|
# Tangent vectors
|
2020-02-05 14:46:25 -08:00
|
|
|
t1 = h - a1
|
|
|
|
t2 = h - a2
|
2019-02-07 09:26:18 -08:00
|
|
|
# Normals
|
|
|
|
n1 = rotate_vector(t1, TAU / 4)
|
|
|
|
n2 = rotate_vector(t2, TAU / 4)
|
2020-02-05 14:46:25 -08:00
|
|
|
return find_intersection(a1, n1, a2, n2)
|
|
|
|
|
|
|
|
def get_start_angle(self):
|
|
|
|
angle = angle_of_vector(self.get_start() - self.get_arc_center())
|
|
|
|
return angle % TAU
|
|
|
|
|
|
|
|
def get_stop_angle(self):
|
|
|
|
angle = angle_of_vector(self.get_end() - self.get_arc_center())
|
|
|
|
return angle % TAU
|
2018-01-22 16:01:33 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
def move_arc_center_to(self, point):
|
2019-02-07 09:26:18 -08:00
|
|
|
self.shift(point - self.get_arc_center())
|
2018-01-22 16:01:33 -08:00
|
|
|
return self
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-02-23 18:36:54 +01:00
|
|
|
class ArcBetweenPoints(Arc):
|
2019-02-07 21:57:40 -08:00
|
|
|
def __init__(self, start, end, angle=TAU / 4, **kwargs):
|
2020-06-28 10:05:49 -07:00
|
|
|
super().__init__(angle=angle, **kwargs)
|
2019-02-07 10:59:04 -08:00
|
|
|
if angle == 0:
|
|
|
|
self.set_points_as_corners([LEFT, RIGHT])
|
2019-02-07 21:57:40 -08:00
|
|
|
self.put_start_and_end_on(start, end)
|
2018-02-23 18:36:54 +01:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-02-23 18:36:54 +01:00
|
|
|
class CurvedArrow(ArcBetweenPoints):
|
2019-02-07 09:26:18 -08:00
|
|
|
def __init__(self, start_point, end_point, **kwargs):
|
|
|
|
ArcBetweenPoints.__init__(self, start_point, end_point, **kwargs)
|
|
|
|
self.add_tip()
|
2018-02-23 18:36:54 +01:00
|
|
|
|
|
|
|
|
2019-02-07 09:26:18 -08:00
|
|
|
class CurvedDoubleArrow(CurvedArrow):
|
|
|
|
def __init__(self, start_point, end_point, **kwargs):
|
2020-02-05 14:46:25 -08:00
|
|
|
CurvedArrow.__init__(self, start_point, end_point, **kwargs)
|
2019-02-07 09:26:18 -08:00
|
|
|
self.add_tip(at_start=True)
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-02-23 18:36:54 +01:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class Circle(Arc):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"color": RED,
|
|
|
|
"close_new_points": True,
|
|
|
|
"anchors_span_full_range": False
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
def __init__(self, **kwargs):
|
2019-02-07 09:26:18 -08:00
|
|
|
Arc.__init__(self, 0, TAU, **kwargs)
|
2015-09-28 16:25:18 -07:00
|
|
|
|
2020-02-05 14:46:25 -08:00
|
|
|
def surround(self, mobject, dim_to_match=0, stretch=False, buff=MED_SMALL_BUFF):
|
2018-01-10 17:57:22 -08:00
|
|
|
# Ignores dim_to_match and stretch; result will always be a circle
|
|
|
|
# TODO: Perhaps create an ellipse class to handle singele-dimension stretching
|
2018-03-22 11:54:08 -07:00
|
|
|
|
2018-01-10 17:57:22 -08:00
|
|
|
self.replace(mobject, dim_to_match, stretch)
|
2020-02-05 14:46:25 -08:00
|
|
|
self.stretch((self.get_width() + 2 * buff) / self.get_width(), 0)
|
|
|
|
self.stretch((self.get_height() + 2 * buff) / self.get_height(), 1)
|
2018-01-10 17:57:22 -08:00
|
|
|
|
2019-02-07 09:26:18 -08:00
|
|
|
def point_at_angle(self, angle):
|
2020-02-05 14:46:25 -08:00
|
|
|
start_angle = self.get_start_angle()
|
2019-01-17 14:10:52 -08:00
|
|
|
return self.point_from_proportion(
|
|
|
|
(angle - start_angle) / TAU
|
|
|
|
)
|
|
|
|
|
2021-12-07 10:07:25 -08:00
|
|
|
def get_radius(self):
|
|
|
|
return get_norm(self.get_start() - self.get_center())
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2017-07-20 13:37:12 -07:00
|
|
|
class Dot(Circle):
|
2016-04-17 00:31:38 -07:00
|
|
|
CONFIG = {
|
2019-02-07 10:12:42 -08:00
|
|
|
"radius": DEFAULT_DOT_RADIUS,
|
2018-04-06 13:58:59 -07:00
|
|
|
"stroke_width": 0,
|
|
|
|
"fill_opacity": 1.0,
|
|
|
|
"color": WHITE
|
2016-04-17 00:31:38 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
|
|
|
def __init__(self, point=ORIGIN, **kwargs):
|
2021-01-11 10:57:23 -10:00
|
|
|
super().__init__(arc_center=point, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
class SmallDot(Dot):
|
|
|
|
CONFIG = {
|
|
|
|
"radius": DEFAULT_SMALL_DOT_RADIUS,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-07 10:12:42 -08:00
|
|
|
class Ellipse(Circle):
|
2018-01-27 19:09:59 +01:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"width": 2,
|
|
|
|
"height": 1
|
2018-01-27 19:09:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 10:12:42 -08:00
|
|
|
def __init__(self, **kwargs):
|
2021-01-11 10:57:23 -10:00
|
|
|
super().__init__(**kwargs)
|
Fix Ellipse constructor: 'width' is not defined
Constructing an ellipse before gave the error "name 'width' is not defined"
I got this error when running the tutorial at https://github.com/zimmermant/manim_tutorial/blob/master/manim_tutorial_P37.py.
python3 -m manim manim_tutorial_P37.py MoreShapes -pl
Traceback (most recent call last):
File "/Users/jeanwhitmore/Code/python3/manim/manimlib/extract_scene.py", line 153, in main
scene = SceneClass(**scene_kwargs)
File "/Users/jeanwhitmore/Code/python3/manim/manimlib/scene/scene.py", line 52, in __init__
self.construct()
File "manim_tutorial_P37.py", line 33, in construct
ellipse=Ellipse(width=3, height=1, color=RED)
File "/Users/jeanwhitmore/Code/python3/manim/manimlib/mobject/geometry.py", line 319, in __init__
self.set_width(width, stretch=True)
NameError: name 'width' is not defined
After this fix, the Ellipse can be constructed and the animation is generated.
(Another shape is still broken, and I will try to fix that, too.)
2019-02-17 21:34:40 -06:00
|
|
|
self.set_width(self.width, stretch=True)
|
2019-03-07 10:44:43 -05:00
|
|
|
self.set_height(self.height, stretch=True)
|
2018-01-27 19:09:59 +01:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 10:12:42 -08:00
|
|
|
class AnnularSector(Arc):
|
2018-01-17 15:12:39 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"inner_radius": 1,
|
|
|
|
"outer_radius": 2,
|
|
|
|
"angle": TAU / 4,
|
|
|
|
"start_angle": 0,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
|
|
|
"color": WHITE,
|
2018-01-17 15:12:39 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2019-02-07 10:12:42 -08:00
|
|
|
inner_arc, outer_arc = [
|
|
|
|
Arc(
|
|
|
|
start_angle=self.start_angle,
|
|
|
|
angle=self.angle,
|
|
|
|
radius=radius,
|
|
|
|
arc_center=self.arc_center,
|
|
|
|
)
|
|
|
|
for radius in (self.inner_radius, self.outer_radius)
|
|
|
|
]
|
|
|
|
outer_arc.reverse_points()
|
2021-01-10 18:51:47 -08:00
|
|
|
self.append_points(inner_arc.get_points())
|
|
|
|
self.add_line_to(outer_arc.get_points()[0])
|
|
|
|
self.append_points(outer_arc.get_points())
|
|
|
|
self.add_line_to(inner_arc.get_points()[0])
|
2018-01-19 17:30:39 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-01-18 15:06:38 -08:00
|
|
|
class Sector(AnnularSector):
|
2018-01-19 17:30:39 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"outer_radius": 1,
|
|
|
|
"inner_radius": 0
|
2018-01-19 17:30:39 -08:00
|
|
|
}
|
2018-01-18 15:06:38 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-01-18 15:06:38 -08:00
|
|
|
class Annulus(Circle):
|
|
|
|
CONFIG = {
|
|
|
|
"inner_radius": 1,
|
|
|
|
"outer_radius": 2,
|
2018-04-06 13:58:59 -07:00
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
|
|
|
"color": WHITE,
|
|
|
|
"mark_paths_closed": False,
|
2018-01-18 15:06:38 -08:00
|
|
|
}
|
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2018-01-18 15:06:38 -08:00
|
|
|
self.radius = self.outer_radius
|
2018-04-06 13:58:59 -07:00
|
|
|
outer_circle = Circle(radius=self.outer_radius)
|
2018-01-18 15:06:38 -08:00
|
|
|
inner_circle = Circle(radius=self.inner_radius)
|
2019-02-07 10:12:42 -08:00
|
|
|
inner_circle.reverse_points()
|
2021-01-10 18:51:47 -08:00
|
|
|
self.append_points(outer_circle.get_points())
|
|
|
|
self.append_points(inner_circle.get_points())
|
2019-02-07 10:12:42 -08:00
|
|
|
self.shift(self.arc_center)
|
2018-01-18 15:06:38 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 17:53:11 -08:00
|
|
|
class Line(TipableVMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"buff": 0,
|
2020-02-05 14:46:25 -08:00
|
|
|
# Angle of arc specified here
|
2020-06-29 11:04:23 -07:00
|
|
|
"path_arc": 0,
|
2015-12-19 13:06:09 -08:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-04-06 11:52:23 -07:00
|
|
|
def __init__(self, start=LEFT, end=RIGHT, **kwargs):
|
2015-12-19 13:06:09 -08:00
|
|
|
digest_config(self, kwargs)
|
2019-03-03 07:42:00 -06:00
|
|
|
self.set_start_and_end_attrs(start, end)
|
2020-06-29 11:04:23 -07:00
|
|
|
super().__init__(**kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2020-06-28 10:05:49 -07:00
|
|
|
self.set_points_by_ends(self.start, self.end, self.buff, self.path_arc)
|
|
|
|
|
|
|
|
def set_points_by_ends(self, start, end, buff=0, path_arc=0):
|
2021-08-21 10:35:29 -07:00
|
|
|
vect = end - start
|
|
|
|
dist = get_norm(vect)
|
|
|
|
if np.isclose(dist, 0):
|
|
|
|
self.set_points_as_corners([start, end])
|
|
|
|
return self
|
2020-06-28 10:05:49 -07:00
|
|
|
if path_arc:
|
2021-10-05 14:17:41 -07:00
|
|
|
neg = path_arc < 0
|
|
|
|
if neg:
|
|
|
|
path_arc = -path_arc
|
|
|
|
start, end = end, start
|
2021-08-21 10:35:29 -07:00
|
|
|
radius = (dist / 2) / math.sin(path_arc / 2)
|
|
|
|
alpha = (PI - path_arc) / 2
|
|
|
|
center = start + radius * normalize(rotate_vector(end - start, alpha))
|
|
|
|
|
|
|
|
raw_arc_points = Arc.create_quadratic_bezier_points(
|
|
|
|
angle=path_arc - 2 * buff / radius,
|
|
|
|
start_angle=angle_of_vector(start - center) + buff / radius,
|
|
|
|
)
|
2021-10-05 14:17:41 -07:00
|
|
|
if neg:
|
|
|
|
raw_arc_points = raw_arc_points[::-1]
|
2021-08-21 10:35:29 -07:00
|
|
|
self.set_points(center + radius * raw_arc_points)
|
2017-04-26 16:21:38 -07:00
|
|
|
else:
|
2021-08-21 10:35:29 -07:00
|
|
|
if buff > 0 and dist > 0:
|
|
|
|
start = start + vect * (buff / dist)
|
|
|
|
end = end - vect * (buff / dist)
|
2020-06-28 10:05:49 -07:00
|
|
|
self.set_points_as_corners([start, end])
|
2021-08-21 10:35:29 -07:00
|
|
|
return self
|
2017-04-26 16:40:54 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
def set_path_arc(self, new_value):
|
2018-01-27 13:29:39 +01:00
|
|
|
self.path_arc = new_value
|
2020-02-11 19:55:00 -08:00
|
|
|
self.init_points()
|
2018-01-27 13:29:39 +01:00
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
def set_start_and_end_attrs(self, start, end):
|
2019-02-07 10:59:04 -08:00
|
|
|
# If either start or end are Mobjects, this
|
|
|
|
# gives their centers
|
|
|
|
rough_start = self.pointify(start)
|
|
|
|
rough_end = self.pointify(end)
|
|
|
|
vect = normalize(rough_end - rough_start)
|
|
|
|
# Now that we know the direction between them,
|
2020-02-05 14:46:25 -08:00
|
|
|
# we can find the appropriate boundary point from
|
2019-02-07 10:59:04 -08:00
|
|
|
# start and end, if they're mobjects
|
2021-08-21 10:35:29 -07:00
|
|
|
self.start = self.pointify(start, vect)
|
|
|
|
self.end = self.pointify(end, -vect)
|
2019-02-07 10:59:04 -08:00
|
|
|
|
|
|
|
def pointify(self, mob_or_point, direction=None):
|
2021-01-04 17:25:05 -08:00
|
|
|
"""
|
|
|
|
Take an argument passed into Line (or subclass) and turn
|
|
|
|
it into a 3d point.
|
|
|
|
"""
|
2016-04-17 00:31:38 -07:00
|
|
|
if isinstance(mob_or_point, Mobject):
|
2019-02-07 10:59:04 -08:00
|
|
|
mob = mob_or_point
|
|
|
|
if direction is None:
|
|
|
|
return mob.get_center()
|
|
|
|
else:
|
2021-01-14 14:15:58 -10:00
|
|
|
return mob.get_continuous_bounding_box_point(direction)
|
2020-12-08 09:27:20 -08:00
|
|
|
else:
|
|
|
|
point = mob_or_point
|
|
|
|
result = np.zeros(self.dim)
|
|
|
|
result[:len(point)] = point
|
|
|
|
return result
|
2016-04-17 00:31:38 -07:00
|
|
|
|
2019-04-06 11:52:23 -07:00
|
|
|
def put_start_and_end_on(self, start, end):
|
|
|
|
curr_start, curr_end = self.get_start_and_end()
|
2021-08-21 10:35:29 -07:00
|
|
|
if np.isclose(curr_start, curr_end).all():
|
|
|
|
# Handle null lines more gracefully
|
|
|
|
self.set_points_by_ends(start, end, buff=0, path_arc=self.path_arc)
|
|
|
|
return self
|
2019-06-23 16:25:48 -07:00
|
|
|
return super().put_start_and_end_on(start, end)
|
2019-04-06 11:52:23 -07:00
|
|
|
|
2017-08-30 13:16:08 -07:00
|
|
|
def get_vector(self):
|
|
|
|
return self.get_end() - self.get_start()
|
|
|
|
|
2018-07-17 12:44:18 -07:00
|
|
|
def get_unit_vector(self):
|
2019-02-07 14:52:40 -08:00
|
|
|
return normalize(self.get_vector())
|
2016-04-27 17:35:04 -07:00
|
|
|
|
2019-02-07 14:52:40 -08:00
|
|
|
def get_angle(self):
|
|
|
|
return angle_of_vector(self.get_vector())
|
2016-03-21 19:30:09 -07:00
|
|
|
|
2021-02-07 17:30:49 -08:00
|
|
|
def get_projection(self, point):
|
|
|
|
"""
|
|
|
|
Return projection of a point onto the line
|
|
|
|
"""
|
|
|
|
unit_vect = self.get_unit_vector()
|
|
|
|
start = self.get_start()
|
|
|
|
return start + np.dot(point - start, unit_vect) * unit_vect
|
|
|
|
|
2015-06-13 19:00:23 -07:00
|
|
|
def get_slope(self):
|
2019-02-07 14:52:40 -08:00
|
|
|
return np.tan(self.get_angle())
|
2015-06-13 19:00:23 -07:00
|
|
|
|
2020-08-16 09:45:34 -07:00
|
|
|
def set_angle(self, angle, about_point=None):
|
|
|
|
if about_point is None:
|
|
|
|
about_point = self.get_start()
|
2019-02-03 12:10:55 -08:00
|
|
|
self.rotate(
|
|
|
|
angle - self.get_angle(),
|
2020-08-16 09:45:34 -07:00
|
|
|
about_point=about_point,
|
2019-02-03 12:10:55 -08:00
|
|
|
)
|
2020-08-16 09:45:34 -07:00
|
|
|
return self
|
2017-09-01 18:57:38 -07:00
|
|
|
|
2021-08-26 11:43:19 -07:00
|
|
|
def set_length(self, length, **kwargs):
|
|
|
|
self.scale(length / self.get_length(), **kwargs)
|
2021-11-18 17:51:56 -08:00
|
|
|
return self
|
2019-06-19 16:07:22 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-08-18 12:54:04 -07:00
|
|
|
class DashedLine(Line):
|
|
|
|
CONFIG = {
|
2019-02-07 15:41:54 -08:00
|
|
|
"dash_length": DEFAULT_DASH_LENGTH,
|
|
|
|
"dash_spacing": None,
|
|
|
|
"positive_space_ratio": 0.5,
|
2016-08-18 12:54:04 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-08-18 12:54:04 -07:00
|
|
|
def __init__(self, *args, **kwargs):
|
2020-06-28 10:05:49 -07:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-02-07 15:41:54 -08:00
|
|
|
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)
|
2016-08-18 12:54:04 -07:00
|
|
|
|
2019-02-07 15:41:54 -08:00
|
|
|
def calculate_num_dashes(self, positive_space_ratio):
|
|
|
|
try:
|
|
|
|
full_length = self.dash_length / positive_space_ratio
|
2020-02-05 14:46:25 -08:00
|
|
|
return int(np.ceil(self.get_length() / full_length))
|
2019-02-07 15:41:54 -08:00
|
|
|
except ZeroDivisionError:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def calculate_positive_space_ratio(self):
|
|
|
|
return fdiv(
|
|
|
|
self.dash_length,
|
|
|
|
self.dash_length + self.dash_spacing,
|
|
|
|
)
|
2016-08-18 12:54:04 -07:00
|
|
|
|
|
|
|
def get_start(self):
|
2019-02-07 15:41:54 -08:00
|
|
|
if len(self.submobjects) > 0:
|
|
|
|
return self.submobjects[0].get_start()
|
2017-02-27 15:56:22 -08:00
|
|
|
else:
|
2019-02-07 15:41:54 -08:00
|
|
|
return Line.get_start(self)
|
2016-08-18 12:54:04 -07:00
|
|
|
|
|
|
|
def get_end(self):
|
2019-02-07 15:41:54 -08:00
|
|
|
if len(self.submobjects) > 0:
|
|
|
|
return self.submobjects[-1].get_end()
|
2017-02-27 15:56:22 -08:00
|
|
|
else:
|
2019-02-07 15:41:54 -08:00
|
|
|
return Line.get_end(self)
|
2016-08-18 12:54:04 -07:00
|
|
|
|
2019-02-07 17:53:11 -08:00
|
|
|
def get_first_handle(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.submobjects[0].get_points()[1]
|
2019-02-07 17:53:11 -08:00
|
|
|
|
|
|
|
def get_last_handle(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.submobjects[-1].get_points()[-2]
|
2019-02-07 17:53:11 -08:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-06-15 12:21:29 -07:00
|
|
|
class TangentLine(Line):
|
|
|
|
CONFIG = {
|
|
|
|
"length": 1,
|
|
|
|
"d_alpha": 1e-6
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, vmob, alpha, **kwargs):
|
|
|
|
digest_config(self, kwargs)
|
|
|
|
da = self.d_alpha
|
2020-02-18 22:32:57 -08:00
|
|
|
a1 = clip(alpha - da, 0, 1)
|
|
|
|
a2 = clip(alpha + da, 0, 1)
|
2020-06-28 10:05:49 -07:00
|
|
|
super().__init__(vmob.pfp(a1), vmob.pfp(a2), **kwargs)
|
2019-06-15 12:21:29 -07:00
|
|
|
self.scale(self.length / self.get_length())
|
|
|
|
|
|
|
|
|
2018-11-29 17:29:31 -08:00
|
|
|
class Elbow(VMobject):
|
|
|
|
CONFIG = {
|
|
|
|
"width": 0.2,
|
|
|
|
"angle": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2021-02-26 10:46:19 +01:00
|
|
|
super().__init__(**kwargs)
|
2018-11-29 17:29:31 -08:00
|
|
|
self.set_points_as_corners([UP, UP + RIGHT, RIGHT])
|
|
|
|
self.set_width(self.width, about_point=ORIGIN)
|
|
|
|
self.rotate(self.angle, about_point=ORIGIN)
|
|
|
|
|
|
|
|
|
2015-08-17 11:12:56 -07:00
|
|
|
class Arrow(Line):
|
2021-08-21 10:35:29 -07:00
|
|
|
CONFIG = {
|
|
|
|
"stroke_color": GREY_A,
|
2021-10-05 14:17:41 -07:00
|
|
|
"stroke_width": 5,
|
2021-08-21 10:35:29 -07:00
|
|
|
"tip_width_ratio": 4,
|
|
|
|
"width_to_tip_len": 0.0075,
|
|
|
|
"max_tip_length_to_length_ratio": 0.3,
|
|
|
|
"max_width_to_length_ratio": 10,
|
2021-10-03 10:26:27 -07:00
|
|
|
"buff": 0.25,
|
2021-08-21 10:35:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
def set_points_by_ends(self, start, end, buff=0, path_arc=0):
|
|
|
|
super().set_points_by_ends(start, end, buff, path_arc)
|
|
|
|
self.insert_tip_anchor()
|
|
|
|
return self
|
|
|
|
|
2022-02-11 23:53:21 +08:00
|
|
|
def init_colors(self):
|
|
|
|
super().init_colors()
|
2021-08-21 10:35:29 -07:00
|
|
|
self.create_tip_with_stroke_width()
|
|
|
|
|
|
|
|
def get_arc_length(self):
|
|
|
|
# Push up into Line?
|
|
|
|
arc_len = get_norm(self.get_vector())
|
|
|
|
if self.path_arc > 0:
|
|
|
|
arc_len *= self.path_arc / (2 * math.sin(self.path_arc / 2))
|
|
|
|
return arc_len
|
|
|
|
|
|
|
|
def insert_tip_anchor(self):
|
|
|
|
prev_end = self.get_end()
|
|
|
|
arc_len = self.get_arc_length()
|
|
|
|
tip_len = self.get_stroke_width() * self.width_to_tip_len * self.tip_width_ratio
|
2021-10-03 10:26:27 -07:00
|
|
|
if tip_len >= self.max_tip_length_to_length_ratio * arc_len:
|
2021-08-21 10:35:29 -07:00
|
|
|
alpha = self.max_tip_length_to_length_ratio
|
|
|
|
else:
|
|
|
|
alpha = tip_len / arc_len
|
|
|
|
self.pointwise_become_partial(self, 0, 1 - alpha)
|
|
|
|
self.add_line_to(prev_end)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def create_tip_with_stroke_width(self):
|
|
|
|
width = min(
|
|
|
|
self.max_stroke_width,
|
|
|
|
self.max_width_to_length_ratio * self.get_length(),
|
|
|
|
)
|
|
|
|
widths_array = np.full(self.get_num_points(), width)
|
|
|
|
nppc = self.n_points_per_curve
|
|
|
|
if len(widths_array) > nppc:
|
|
|
|
widths_array[-nppc:] = [
|
|
|
|
a * self.tip_width_ratio * width
|
|
|
|
for a in np.linspace(1, 0, nppc)
|
|
|
|
]
|
|
|
|
self.set_stroke(width=widths_array)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def reset_tip(self):
|
|
|
|
self.set_points_by_ends(
|
|
|
|
self.get_start(),
|
|
|
|
self.get_end(),
|
|
|
|
path_arc=self.path_arc,
|
|
|
|
)
|
|
|
|
self.create_tip_with_stroke_width()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def set_stroke(self, color=None, width=None, *args, **kwargs):
|
|
|
|
super().set_stroke(color=color, width=width, *args, **kwargs)
|
|
|
|
if isinstance(width, numbers.Number):
|
|
|
|
self.max_stroke_width = width
|
|
|
|
self.reset_tip()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _handle_scale_side_effects(self, scale_factor):
|
|
|
|
return self.reset_tip()
|
|
|
|
|
|
|
|
|
|
|
|
class FillArrow(Line):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2020-06-28 10:05:49 -07:00
|
|
|
"fill_color": GREY_A,
|
|
|
|
"fill_opacity": 1,
|
|
|
|
"stroke_width": 0,
|
2019-02-07 17:53:11 -08:00
|
|
|
"buff": MED_SMALL_BUFF,
|
2020-12-04 08:20:59 -08:00
|
|
|
"thickness": 0.05,
|
2020-06-28 10:05:49 -07:00
|
|
|
"tip_width_ratio": 5,
|
|
|
|
"tip_angle": PI / 3,
|
|
|
|
"max_tip_length_to_length_ratio": 0.5,
|
2020-06-29 11:04:23 -07:00
|
|
|
"max_width_to_length_ratio": 0.1,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def set_points_by_ends(self, start, end, buff=0, path_arc=0):
|
2020-12-04 08:20:59 -08:00
|
|
|
# Find the right tip length and thickness
|
2020-06-28 10:05:49 -07:00
|
|
|
vect = end - start
|
2020-06-29 11:04:23 -07:00
|
|
|
length = max(get_norm(vect), 1e-8)
|
2020-12-04 08:20:59 -08:00
|
|
|
thickness = self.thickness
|
|
|
|
w_ratio = fdiv(self.max_width_to_length_ratio, fdiv(thickness, length))
|
2020-06-28 10:05:49 -07:00
|
|
|
if w_ratio < 1:
|
2020-12-04 08:20:59 -08:00
|
|
|
thickness *= w_ratio
|
2020-06-28 10:05:49 -07:00
|
|
|
|
2020-12-04 08:20:59 -08:00
|
|
|
tip_width = self.tip_width_ratio * thickness
|
2020-06-28 10:05:49 -07:00
|
|
|
tip_length = tip_width / (2 * np.tan(self.tip_angle / 2))
|
2020-06-29 11:04:23 -07:00
|
|
|
t_ratio = fdiv(self.max_tip_length_to_length_ratio, fdiv(tip_length, length))
|
2020-06-28 10:05:49 -07:00
|
|
|
if t_ratio < 1:
|
|
|
|
tip_length *= t_ratio
|
|
|
|
tip_width *= t_ratio
|
|
|
|
|
|
|
|
# Find points for the stem
|
|
|
|
if path_arc == 0:
|
|
|
|
points1 = (length - tip_length) * np.array([RIGHT, 0.5 * RIGHT, ORIGIN])
|
2020-12-04 08:20:59 -08:00
|
|
|
points1 += thickness * UP / 2
|
|
|
|
points2 = points1[::-1] + thickness * DOWN
|
2020-06-28 10:05:49 -07:00
|
|
|
else:
|
|
|
|
# Solve for radius so that the tip-to-tail length matches |end - start|
|
|
|
|
a = 2 * (1 - np.cos(path_arc))
|
|
|
|
b = -2 * tip_length * np.sin(path_arc)
|
|
|
|
c = tip_length**2 - length**2
|
|
|
|
R = (-b + np.sqrt(b**2 - 4 * a * c)) / (2 * a)
|
|
|
|
|
|
|
|
# Find arc points
|
2020-06-28 12:15:55 -07:00
|
|
|
points1 = Arc.create_quadratic_bezier_points(path_arc)
|
2020-06-28 10:05:49 -07:00
|
|
|
points2 = np.array(points1[::-1])
|
2020-12-04 08:20:59 -08:00
|
|
|
points1 *= (R + thickness / 2)
|
|
|
|
points2 *= (R - thickness / 2)
|
2020-06-28 10:05:49 -07:00
|
|
|
if path_arc < 0:
|
|
|
|
tip_length *= -1
|
|
|
|
rot_T = rotation_matrix_transpose(PI / 2 - path_arc, OUT)
|
|
|
|
for points in points1, points2:
|
|
|
|
points[:] = np.dot(points, rot_T)
|
|
|
|
points += R * DOWN
|
|
|
|
|
|
|
|
self.set_points(points1)
|
|
|
|
# Tip
|
|
|
|
self.add_line_to(tip_width * UP / 2)
|
|
|
|
self.add_line_to(tip_length * LEFT)
|
2021-01-10 18:51:47 -08:00
|
|
|
self.tip_index = len(self.get_points()) - 1
|
2020-06-28 10:05:49 -07:00
|
|
|
self.add_line_to(tip_width * DOWN / 2)
|
|
|
|
self.add_line_to(points2[0])
|
|
|
|
# Close it out
|
|
|
|
self.append_points(points2)
|
|
|
|
self.add_line_to(points1[0])
|
|
|
|
|
|
|
|
if length > 0:
|
2021-01-13 09:28:52 -10:00
|
|
|
# Final correction
|
|
|
|
super().scale(length / self.get_length())
|
2020-06-28 10:05:49 -07:00
|
|
|
|
|
|
|
self.rotate(angle_of_vector(vect) - self.get_angle())
|
2021-02-06 21:28:53 +05:30
|
|
|
self.rotate(
|
|
|
|
PI / 2 - np.arccos(normalize(vect)[2]),
|
|
|
|
axis=rotate_vector(self.get_unit_vector(), -PI / 2),
|
|
|
|
)
|
2020-06-28 10:05:49 -07:00
|
|
|
self.shift(start - self.get_start())
|
2020-07-22 18:17:08 -07:00
|
|
|
self.refresh_triangulation()
|
2020-06-28 10:05:49 -07:00
|
|
|
|
|
|
|
def reset_points_around_ends(self):
|
2021-01-13 09:28:52 -10:00
|
|
|
self.set_points_by_ends(
|
|
|
|
self.get_start(), self.get_end(), path_arc=self.path_arc
|
|
|
|
)
|
2020-06-28 10:05:49 -07:00
|
|
|
return self
|
2015-08-12 14:24:36 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def get_start(self):
|
2020-08-20 17:13:24 -07:00
|
|
|
nppc = self.n_points_per_curve
|
2021-01-10 18:51:47 -08:00
|
|
|
points = self.get_points()
|
|
|
|
return (points[0] + points[-nppc]) / 2
|
2019-03-24 11:33:04 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def get_end(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.get_points()[self.tip_index]
|
2015-08-12 14:24:36 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def put_start_and_end_on(self, start, end):
|
|
|
|
self.set_points_by_ends(start, end, buff=0, path_arc=self.path_arc)
|
|
|
|
return self
|
2017-08-27 23:52:28 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def scale(self, *args, **kwargs):
|
|
|
|
super().scale(*args, **kwargs)
|
|
|
|
self.reset_points_around_ends()
|
2017-08-27 23:52:28 -07:00
|
|
|
return self
|
|
|
|
|
2020-12-04 08:20:59 -08:00
|
|
|
def set_thickness(self, thickness):
|
|
|
|
self.thickness = thickness
|
2020-06-28 10:05:49 -07:00
|
|
|
self.reset_points_around_ends()
|
|
|
|
return self
|
2016-04-27 17:35:04 -07:00
|
|
|
|
2020-06-28 10:05:49 -07:00
|
|
|
def set_path_arc(self, path_arc):
|
|
|
|
self.path_arc = path_arc
|
|
|
|
self.reset_points_around_ends()
|
2019-03-03 07:42:00 -06:00
|
|
|
return self
|
2019-02-24 11:57:02 -06:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-03-17 23:54:28 -07:00
|
|
|
class Vector(Arrow):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"buff": 0,
|
2016-03-17 23:54:28 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-04-06 11:52:23 -07:00
|
|
|
def __init__(self, direction=RIGHT, **kwargs):
|
2016-07-15 18:16:06 -07:00
|
|
|
if len(direction) == 2:
|
2020-02-13 12:03:54 -08:00
|
|
|
direction = np.hstack([direction, 0])
|
2020-06-29 11:04:23 -07:00
|
|
|
super().__init__(ORIGIN, direction, **kwargs)
|
2016-03-17 23:54:28 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-23 23:36:05 -07:00
|
|
|
class DoubleArrow(Arrow):
|
2019-02-07 21:24:00 -08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
Arrow.__init__(self, *args, **kwargs)
|
|
|
|
self.add_tip(at_start=True)
|
2016-07-12 10:34:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class CubicBezier(VMobject):
|
2020-02-05 14:46:25 -08:00
|
|
|
def __init__(self, a0, h0, h1, a1, **kwargs):
|
2016-04-17 00:31:38 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2020-02-05 14:46:25 -08:00
|
|
|
self.add_cubic_bezier_curve(a0, h0, h1, a1)
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2016-04-17 00:31:38 -07:00
|
|
|
class Polygon(VMobject):
|
2016-01-09 20:10:06 -08:00
|
|
|
def __init__(self, *vertices, **kwargs):
|
2020-06-13 15:21:19 -07:00
|
|
|
self.vertices = vertices
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def init_points(self):
|
|
|
|
verts = self.vertices
|
|
|
|
self.set_points_as_corners([*verts, verts[0]])
|
2015-10-09 19:53:38 -07:00
|
|
|
|
|
|
|
def get_vertices(self):
|
2019-02-05 15:24:51 -08:00
|
|
|
return self.get_start_anchors()
|
2015-12-19 13:06:09 -08:00
|
|
|
|
2019-02-07 21:57:40 -08:00
|
|
|
def round_corners(self, radius=0.5):
|
|
|
|
vertices = self.get_vertices()
|
|
|
|
arcs = []
|
|
|
|
for v1, v2, v3 in adjacent_n_tuples(vertices, 3):
|
|
|
|
vect1 = v2 - v1
|
|
|
|
vect2 = v3 - v2
|
|
|
|
unit_vect1 = normalize(vect1)
|
|
|
|
unit_vect2 = normalize(vect2)
|
|
|
|
angle = angle_between_vectors(vect1, vect2)
|
2019-02-07 22:25:09 -08:00
|
|
|
# Negative radius gives concave curves
|
|
|
|
angle *= np.sign(radius)
|
2019-02-07 21:57:40 -08:00
|
|
|
# Distance between vertex and start of the arc
|
|
|
|
cut_off_length = radius * np.tan(angle / 2)
|
2019-02-07 22:25:09 -08:00
|
|
|
# Determines counterclockwise vs. clockwise
|
|
|
|
sign = np.sign(np.cross(vect1, vect2)[2])
|
2019-02-15 15:16:02 -08:00
|
|
|
arc = ArcBetweenPoints(
|
2019-02-07 21:57:40 -08:00
|
|
|
v2 - unit_vect1 * cut_off_length,
|
|
|
|
v2 + unit_vect2 * cut_off_length,
|
2020-02-05 14:46:25 -08:00
|
|
|
angle=sign * angle,
|
|
|
|
n_components=2,
|
2019-02-15 15:16:02 -08:00
|
|
|
)
|
|
|
|
arcs.append(arc)
|
2019-02-07 22:25:09 -08:00
|
|
|
|
|
|
|
self.clear_points()
|
2019-02-07 21:57:40 -08:00
|
|
|
# To ensure that we loop through starting with last
|
|
|
|
arcs = [arcs[-1], *arcs[:-1]]
|
|
|
|
for arc1, arc2 in adjacent_pairs(arcs):
|
2021-01-10 18:51:47 -08:00
|
|
|
self.append_points(arc1.get_points())
|
2019-02-15 15:16:02 -08:00
|
|
|
line = Line(arc1.get_end(), arc2.get_start())
|
|
|
|
# Make sure anchors are evenly distributed
|
|
|
|
len_ratio = line.get_length() / arc1.get_arc_length()
|
|
|
|
line.insert_n_curves(
|
|
|
|
int(arc1.get_num_curves() * len_ratio)
|
|
|
|
)
|
|
|
|
self.append_points(line.get_points())
|
2019-02-07 21:57:40 -08:00
|
|
|
return self
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2022-02-11 23:53:21 +08:00
|
|
|
class Polyline(Polygon):
|
|
|
|
def init_points(self):
|
|
|
|
self.set_points_as_corners(self.vertices)
|
|
|
|
|
|
|
|
|
2017-01-16 11:43:59 -08:00
|
|
|
class RegularPolygon(Polygon):
|
2016-04-27 17:35:04 -07:00
|
|
|
CONFIG = {
|
2019-02-15 15:16:02 -08:00
|
|
|
"start_angle": None,
|
2016-04-27 17:35:04 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 09:26:18 -08:00
|
|
|
def __init__(self, n=6, **kwargs):
|
2016-04-27 17:35:04 -07:00
|
|
|
digest_config(self, kwargs, locals())
|
2019-02-15 15:16:02 -08:00
|
|
|
if self.start_angle is None:
|
2020-02-05 14:46:25 -08:00
|
|
|
# 0 for odd, 90 for even
|
|
|
|
self.start_angle = (n % 2) * 90 * DEGREES
|
2016-04-27 17:35:04 -07:00
|
|
|
start_vect = rotate_vector(RIGHT, self.start_angle)
|
2017-01-16 11:43:59 -08:00
|
|
|
vertices = compass_directions(n, start_vect)
|
2020-06-13 15:21:19 -07:00
|
|
|
super().__init__(*vertices, **kwargs)
|
2016-04-27 17:35:04 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-02-07 09:26:18 -08:00
|
|
|
class Triangle(RegularPolygon):
|
|
|
|
def __init__(self, **kwargs):
|
2020-06-13 15:21:19 -07:00
|
|
|
super().__init__(n=3, **kwargs)
|
2019-02-07 09:26:18 -08:00
|
|
|
|
|
|
|
|
2019-03-03 07:42:00 -06:00
|
|
|
class ArrowTip(Triangle):
|
|
|
|
CONFIG = {
|
|
|
|
"fill_opacity": 1,
|
2020-06-29 22:53:44 -07:00
|
|
|
"fill_color": WHITE,
|
2019-03-03 07:42:00 -06:00
|
|
|
"stroke_width": 0,
|
2020-06-06 10:56:23 -07:00
|
|
|
"width": DEFAULT_ARROW_TIP_WIDTH,
|
2019-03-03 07:42:00 -06:00
|
|
|
"length": DEFAULT_ARROW_TIP_LENGTH,
|
2020-06-06 10:56:23 -07:00
|
|
|
"angle": 0,
|
2021-06-14 16:13:53 +08:00
|
|
|
"tip_style": 0, # triangle=0, inner_smooth=1, dot=2
|
2019-03-03 07:42:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2020-06-06 10:56:23 -07:00
|
|
|
Triangle.__init__(self, start_angle=0, **kwargs)
|
|
|
|
self.set_height(self.width)
|
|
|
|
self.set_width(self.length, stretch=True)
|
2021-06-14 16:13:53 +08:00
|
|
|
if self.tip_style == 1:
|
2021-04-13 17:40:48 +08:00
|
|
|
self.set_height(self.length * 0.9, stretch=True)
|
|
|
|
self.data["points"][4] += np.array([0.6 * self.length, 0, 0])
|
2021-06-14 16:13:53 +08:00
|
|
|
elif self.tip_style == 2:
|
2021-04-13 17:40:48 +08:00
|
|
|
h = self.length / 2
|
|
|
|
self.clear_points()
|
|
|
|
self.data["points"] = Dot().set_width(h).get_points()
|
2020-06-06 10:56:23 -07:00
|
|
|
self.rotate(self.angle)
|
2019-03-03 07:42:00 -06:00
|
|
|
|
|
|
|
def get_base(self):
|
|
|
|
return self.point_from_proportion(0.5)
|
|
|
|
|
|
|
|
def get_tip_point(self):
|
2021-01-10 18:51:47 -08:00
|
|
|
return self.get_points()[0]
|
2019-03-03 07:42:00 -06:00
|
|
|
|
|
|
|
def get_vector(self):
|
|
|
|
return self.get_tip_point() - self.get_base()
|
|
|
|
|
|
|
|
def get_angle(self):
|
|
|
|
return angle_of_vector(self.get_vector())
|
|
|
|
|
|
|
|
def get_length(self):
|
|
|
|
return get_norm(self.get_vector())
|
|
|
|
|
|
|
|
|
2019-02-05 15:24:51 -08:00
|
|
|
class Rectangle(Polygon):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"color": WHITE,
|
|
|
|
"width": 4.0,
|
2020-06-13 15:21:19 -07:00
|
|
|
"height": 2.0,
|
2018-04-06 13:58:59 -07:00
|
|
|
"mark_paths_closed": True,
|
|
|
|
"close_new_points": True,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-06-13 15:21:19 -07:00
|
|
|
def __init__(self, width=None, height=None, **kwargs):
|
2020-06-05 13:23:15 -07:00
|
|
|
Polygon.__init__(self, UR, UL, DL, DR, **kwargs)
|
2020-06-14 17:42:25 -07:00
|
|
|
|
|
|
|
if width is None:
|
|
|
|
width = self.width
|
|
|
|
if height is None:
|
|
|
|
height = self.height
|
|
|
|
|
|
|
|
self.set_width(width, stretch=True)
|
|
|
|
self.set_height(height, stretch=True)
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-08-07 18:10:00 -07:00
|
|
|
|
|
|
|
class Square(Rectangle):
|
2021-08-19 08:34:53 -07:00
|
|
|
def __init__(self, side_length=2.0, **kwargs):
|
|
|
|
self.side_length = side_length
|
2020-06-14 17:42:25 -07:00
|
|
|
super().__init__(side_length, side_length, **kwargs)
|
2015-10-28 16:03:33 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-04-12 23:19:09 +02:00
|
|
|
class RoundedRectangle(Rectangle):
|
|
|
|
CONFIG = {
|
2018-09-27 17:37:25 -07:00
|
|
|
"corner_radius": 0.5,
|
2018-04-12 23:19:09 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:57:40 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Rectangle.__init__(self, **kwargs)
|
|
|
|
self.round_corners(self.corner_radius)
|