Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions manim/mobject/geometry/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
class Line(TipableVMobject):
def __init__(
self,
start: Point3D = LEFT,
end: Point3D = RIGHT,
start: Point3D | Mobject = LEFT,
end: Point3D | Mobject = RIGHT,
buff: float = 0,
path_arc: float | None = None,
**kwargs,
Expand All @@ -63,16 +63,32 @@ def generate_points(self) -> None:

def set_points_by_ends(
self,
start: Point3D,
end: Point3D,
start: Point3D | Mobject,
end: Point3D | Mobject,
buff: float = 0,
path_arc: float = 0,
) -> None:
"""Sets the points of the line based on its start and end points.
Unlike :meth:`put_start_and_end_on`, this method respects `self.buff` and
Mobject bounding boxes.

Parameters
----------
start : Point3D | Mobject
The start point or Mobject of the line.
end : Point3D | Mobject
The end point or Mobject of the line.
buff : float, optional
The empty space between start and end and the line, by default 0
path_arc : float, optional
The angle of a circle spanned by this arc, by default 0 which is a straight line.
"""
self._set_start_and_end_attrs(start, end)
if path_arc:
arc = ArcBetweenPoints(self.start, self.end, angle=self.path_arc)
self.set_points(arc.points)
else:
self.set_points_as_corners([start, end])
self.set_points_as_corners([self.start, self.end])

self._account_for_buff(buff)

Expand All @@ -93,7 +109,9 @@ def _account_for_buff(self, buff: float) -> Self:
self.pointwise_become_partial(self, buff_proportion, 1 - buff_proportion)
return self

def _set_start_and_end_attrs(self, start: Point3D, end: Point3D) -> None:
def _set_start_and_end_attrs(
self, start: Point3D | Mobject, end: Point3D | Mobject
) -> None:
# If either start or end are Mobjects, this
# gives their centers
rough_start = self._pointify(start)
Expand Down
19 changes: 15 additions & 4 deletions manim/mobject/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ class GenericGraph(VMobject, metaclass=ConvertToOpenGL):
all other configuration options for a vertex.
edge_type
The mobject class used for displaying edges in the scene.
Must be a subclass of :class:`~.Line` for default updaters to work.
edge_config
Either a dictionary containing keyword arguments to be passed
to the class specified via ``edge_type``, or a dictionary whose
Expand Down Expand Up @@ -1558,7 +1559,12 @@ def _populate_edge_dict(
def update_edges(self, graph):
for (u, v), edge in graph.edges.items():
# Undirected graph has a Line edge
edge.put_start_and_end_on(graph[u].get_center(), graph[v].get_center())
edge.set_points_by_ends(
graph[u].get_center(),
graph[v].get_center(),
buff=self._edge_config.get("buff", 0),
path_arc=self._edge_config.get("path_arc", 0),
)

def __repr__(self: Graph) -> str:
return f"Undirected graph on {len(self.vertices)} vertices and {len(self.edges)} edges"
Expand Down Expand Up @@ -1767,10 +1773,15 @@ def update_edges(self, graph):
deformed.
"""
for (u, v), edge in graph.edges.items():
edge_type = type(edge)
tip = edge.pop_tips()[0]
new_edge = edge_type(self[u], self[v], **self._edge_config[(u, v)])
edge.become(new_edge)
# Passing the Mobject instead of the vertex makes the tip
# stop on the bounding box of the vertex.
edge.set_points_by_ends(
graph[u],
graph[v],
buff=self._edge_config.get("buff", 0),
path_arc=self._edge_config.get("path_arc", 0),
)
edge.add_tip(tip)

def __repr__(self: DiGraph) -> str:
Expand Down