Change TipableVMobject to take in all tip_config at one spot

This commit is contained in:
Grant Sanderson 2020-06-06 10:56:23 -07:00
parent 274ebe7a52
commit 4385fc75d0
2 changed files with 28 additions and 31 deletions

View file

@ -23,6 +23,7 @@ DEFAULT_DOT_RADIUS = 0.08
DEFAULT_SMALL_DOT_RADIUS = 0.04 DEFAULT_SMALL_DOT_RADIUS = 0.04
DEFAULT_DASH_LENGTH = 0.05 DEFAULT_DASH_LENGTH = 0.05
DEFAULT_ARROW_TIP_LENGTH = 0.35 DEFAULT_ARROW_TIP_LENGTH = 0.35
DEFAULT_ARROW_TIP_WIDTH = 0.35
class TipableVMobject(VMobject): class TipableVMobject(VMobject):
@ -42,56 +43,46 @@ class TipableVMobject(VMobject):
* Getters * Getters
- Straightforward accessors, returning information pertaining - Straightforward accessors, returning information pertaining
to the TipableVMobject instance's tip(s), its length etc to the TipableVMobject instance's tip(s), its length etc
""" """
CONFIG = { CONFIG = {
"tip_length": DEFAULT_ARROW_TIP_LENGTH, "tip_config": {
# TODO
"normal_vector": OUT,
"tip_style": {
"fill_opacity": 1, "fill_opacity": 1,
"stroke_width": 0, "stroke_width": 0,
} },
"normal_vector": OUT,
} }
# Adding, Creating, Modifying tips # Adding, Creating, Modifying tips
def add_tip(self, at_start=False, **kwargs):
def add_tip(self, tip_length=None, at_start=False):
""" """
Adds a tip to the TipableVMobject instance, recognising Adds a tip to the TipableVMobject instance, recognising
that the endpoints might need to be switched if it's that the endpoints might need to be switched if it's
a 'starting tip' or not. a 'starting tip' or not.
""" """
tip = self.create_tip(tip_length, at_start) tip = self.create_tip(at_start, **kwargs)
self.reset_endpoints_based_on_tip(tip, at_start) self.reset_endpoints_based_on_tip(tip, at_start)
self.asign_tip_attr(tip, at_start) self.asign_tip_attr(tip, at_start)
self.add(tip) self.add(tip)
return self return self
def create_tip(self, tip_length=None, at_start=False): def create_tip(self, at_start=False, **kwargs):
""" """
Stylises the tip, positions it spacially, and returns Stylises the tip, positions it spacially, and returns
the newly instantiated tip to the caller. the newly instantiated tip to the caller.
""" """
tip = self.get_unpositioned_tip(tip_length) tip = self.get_unpositioned_tip(**kwargs)
self.position_tip(tip, at_start) self.position_tip(tip, at_start)
return tip return tip
def get_unpositioned_tip(self, tip_length=None): def get_unpositioned_tip(self, **kwargs):
""" """
Returns a tip that has been stylistically configured, Returns a tip that has been stylistically configured,
but has not yet been given a position in space. but has not yet been given a position in space.
""" """
if tip_length is None: config = self.get_style()
tip_length = self.get_default_tip_length() config.update(self.tip_config)
color = self.get_color() config.update(kwargs)
style = { return ArrowTip(**config)
"fill_color": color,
"stroke_color": color
}
style.update(self.tip_style)
tip = ArrowTip(length=tip_length, **style)
return tip
def position_tip(self, tip, at_start=False): def position_tip(self, tip, at_start=False):
# Last two control points, defining both # Last two control points, defining both
@ -102,10 +93,7 @@ class TipableVMobject(VMobject):
else: else:
handle = self.get_last_handle() handle = self.get_last_handle()
anchor = self.get_end() anchor = self.get_end()
tip.rotate( tip.rotate(angle_of_vector(handle - anchor) - PI - tip.get_angle())
angle_of_vector(handle - anchor) -
PI - tip.get_angle()
)
tip.shift(anchor - tip.get_tip_point()) tip.shift(anchor - tip.get_tip_point())
return tip return tip
@ -744,14 +732,16 @@ class ArrowTip(Triangle):
CONFIG = { CONFIG = {
"fill_opacity": 1, "fill_opacity": 1,
"stroke_width": 0, "stroke_width": 0,
"width": DEFAULT_ARROW_TIP_WIDTH,
"length": DEFAULT_ARROW_TIP_LENGTH, "length": DEFAULT_ARROW_TIP_LENGTH,
"start_angle": PI, "angle": 0,
} }
def __init__(self, **kwargs): def __init__(self, **kwargs):
Triangle.__init__(self, **kwargs) Triangle.__init__(self, start_angle=0, **kwargs)
self.set_width(self.length) self.set_height(self.width)
self.set_height(self.length, stretch=True) self.set_width(self.length, stretch=True)
self.rotate(self.angle)
def get_base(self): def get_base(self):
return self.point_from_proportion(0.5) return self.point_from_proportion(0.5)

View file

@ -29,7 +29,10 @@ class NumberLine(Line):
"line_to_number_direction": DOWN, "line_to_number_direction": DOWN,
"line_to_number_buff": MED_SMALL_BUFF, "line_to_number_buff": MED_SMALL_BUFF,
"include_tip": False, "include_tip": False,
"tip_config": {}, # TODO "tip_config": {
"width": 0.25,
"length": 0.25,
},
"decimal_number_config": { "decimal_number_config": {
"num_decimal_places": 0, "num_decimal_places": 0,
"height": 0.25, "height": 0.25,
@ -60,6 +63,10 @@ class NumberLine(Line):
if self.include_tip: if self.include_tip:
self.add_tip() self.add_tip()
self.tip.set_stroke(
self.stroke_color,
self.stroke_width,
)
if self.include_ticks: if self.include_ticks:
self.add_ticks() self.add_ticks()
if self.include_numbers: if self.include_numbers: