Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 18 additions & 15 deletions manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,30 +1941,33 @@ def surround(
return self

def put_start_and_end_on(self, start: Point3DLike, end: Point3DLike) -> Self:
curr_start, curr_end = self.get_start_and_end()
curr_vect = curr_end - curr_start
if np.all(curr_vect == 0):
# TODO: this looks broken. It makes self.points a Point3D instead
# of a Point3D_Array. However, modifying this breaks some tests
# where this is currently expected.
Comment thread
GoThrones marked this conversation as resolved.
self.points = np.array(start)
current_start, current_end = self.get_start_and_end()
current_vector = current_end - current_start
if np.all(current_vector == 0):
warnings.warn(
"put_start_and_end_on has been called on a closed loop or zero-length mobject. "
f"{type(self).__name__} will be shifted to start point instead.",
stacklevel=2,
)
Comment thread
GoThrones marked this conversation as resolved.
self.shift(np.asarray(start) - current_start)
return self
target_vect = np.asarray(end) - np.asarray(start)

target_vector = np.asarray(end) - np.asarray(start)
axis = (
normalize(np.cross(curr_vect, target_vect))
if np.linalg.norm(np.cross(curr_vect, target_vect)) != 0
normalize(np.cross(current_vector, target_vector))
if np.linalg.norm(np.cross(current_vector, target_vector)) != 0
else OUT
)
self.scale(
np.linalg.norm(target_vect) / np.linalg.norm(curr_vect),
about_point=curr_start,
np.linalg.norm(target_vector) / np.linalg.norm(current_vector),
about_point=current_start,
)
self.rotate(
angle_between_vectors(curr_vect, target_vect),
about_point=curr_start,
angle_between_vectors(current_vector, target_vector),
about_point=current_start,
axis=axis,
)
self.shift(start - curr_start)
self.shift(np.asarray(start) - current_start)
return self

# Background rectangle
Expand Down
32 changes: 20 additions & 12 deletions manim/mobject/opengl/opengl_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import random
import sys
import types
import warnings
from collections.abc import Callable, Iterable, Iterator, Sequence
from functools import partialmethod, wraps
from math import ceil
Expand Down Expand Up @@ -2134,26 +2135,33 @@ def surround(
return self

def put_start_and_end_on(self, start: Point3DLike, end: Point3DLike) -> Self:
curr_start, curr_end = self.get_start_and_end()
curr_vect = curr_end - curr_start
if np.all(curr_vect == 0):
raise Exception("Cannot position endpoints of closed loop")
target_vect = np.array(end) - np.array(start)
current_start, current_end = self.get_start_and_end()
current_vector = current_end - current_start
if np.all(current_vector == 0):
warnings.warn(
"put_start_and_end_on has been called on a closed loop or zero-length mobject. "
f"{type(self).__name__} will be shifted to start point instead.",
stacklevel=2,
)
Comment thread
GoThrones marked this conversation as resolved.
self.shift(np.asarray(start) - current_start)
return self

target_vector = np.asarray(end) - np.asarray(start)
axis = (
normalize(np.cross(curr_vect, target_vect))
if np.linalg.norm(np.cross(curr_vect, target_vect)) != 0
normalize(np.cross(current_vector, target_vector))
if np.linalg.norm(np.cross(current_vector, target_vector)) != 0
else OUT
)
self.scale(
float(np.linalg.norm(target_vect) / np.linalg.norm(curr_vect)),
about_point=curr_start,
np.linalg.norm(target_vector) / np.linalg.norm(current_vector),
about_point=current_start,
)
self.rotate(
angle_between_vectors(curr_vect, target_vect),
about_point=curr_start,
angle_between_vectors(current_vector, target_vector),
about_point=current_start,
axis=axis,
)
self.shift(start - curr_start)
self.shift(np.asarray(start) - current_start)
return self

# Color functions
Expand Down
4 changes: 3 additions & 1 deletion tests/module/mobject/graphing/test_number_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,6 @@ def test_start_and_end_at_same_point():
line = DashedLine(np.zeros(3), np.zeros(3))
line.put_start_and_end_on(np.zeros(3), np.array([0, 0, 0]))

np.testing.assert_array_equal(np.round(np.zeros(3), 4), np.round(line.points, 4))
np.testing.assert_array_equal(
np.round(line.points, 4), np.round(np.zeros((4, 3)), 4)
)
Loading