Merge pull request #582 from stephenwild/master

Documentation for TipableVMobject
This commit is contained in:
Devin Neal 2019-06-13 00:48:15 -07:00 committed by GitHub
commit ab75015099
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 21 deletions

3
.gitignore vendored
View file

@ -12,6 +12,7 @@ ben_cairo_test.py
.floo .floo
.flooignore .flooignore
.vscode .vscode
.vs
*.xml *.xml
*.iml *.iml
media media
@ -23,4 +24,4 @@ dist/
manim.egg-info/ manim.egg-info/
primes.py primes.py
/media_dir.txt /media_dir.txt

View file

@ -26,6 +26,24 @@ DEFAULT_ARROW_TIP_LENGTH = 0.35
class TipableVMobject(VMobject): class TipableVMobject(VMobject):
"""
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
"""
CONFIG = { CONFIG = {
"tip_length": DEFAULT_ARROW_TIP_LENGTH, "tip_length": DEFAULT_ARROW_TIP_LENGTH,
# TODO # TODO
@ -35,12 +53,15 @@ class TipableVMobject(VMobject):
"stroke_width": 0, "stroke_width": 0,
} }
} }
"""
Meant simply for shard functionality between # Adding, Creating, Modifying tips
Arc and Line
"""
def add_tip(self, tip_length=None, at_start=False): def add_tip(self, tip_length=None, at_start=False):
"""
Adds a tip to the TipableVMobject instance, recognising
that the endpoints might need to be switched if it's
a 'starting tip' or not.
"""
tip = self.create_tip(tip_length, at_start) tip = self.create_tip(tip_length, at_start)
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)
@ -48,11 +69,19 @@ class TipableVMobject(VMobject):
return self return self
def create_tip(self, tip_length=None, at_start=False): def create_tip(self, tip_length=None, at_start=False):
"""
Stylises the tip, positions it spacially, and returns
the newly instantiated tip to the caller.
"""
tip = self.get_unpositioned_tip(tip_length) tip = self.get_unpositioned_tip(tip_length)
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, tip_length=None):
"""
Returns a tip that has been stylistically configured,
but has not yet been given a position in space.
"""
if tip_length is None: if tip_length is None:
tip_length = self.get_default_tip_length() tip_length = self.get_default_tip_length()
color = self.get_color() color = self.get_color()
@ -85,6 +114,7 @@ class TipableVMobject(VMobject):
# Zero length, put_start_and_end_on wouldn't # Zero length, put_start_and_end_on wouldn't
# work # work
return self return self
if at_start: if at_start:
self.put_start_and_end_on( self.put_start_and_end_on(
tip.get_base(), self.get_end() tip.get_base(), self.get_end()
@ -102,7 +132,34 @@ class TipableVMobject(VMobject):
self.tip = tip self.tip = tip
return self return self
# 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
def get_tips(self): def get_tips(self):
"""
Returns a VGroup (collection of VMobjects) containing
the TipableVMObject instance's tips.
"""
result = VGroup() result = VGroup()
if hasattr(self, "tip"): if hasattr(self, "tip"):
result.add(self.tip) result.add(self.tip)
@ -111,6 +168,8 @@ class TipableVMobject(VMobject):
return result return result
def get_tip(self): def get_tip(self):
"""Returns the TipableVMobject instance's (first) tip,
otherwise throws an exception."""
tips = self.get_tips() tips = self.get_tips()
if len(tips) == 0: if len(tips) == 0:
raise Exception("tip not found") raise Exception("tip not found")
@ -142,23 +201,7 @@ class TipableVMobject(VMobject):
start, end = self.get_start_and_end() start, end = self.get_start_and_end()
return get_norm(start - end) return get_norm(start - end)
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
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
class Arc(TipableVMobject): class Arc(TipableVMobject):