Skip to content

Commit 276426d

Browse files
committed
Replaced manual binary search with bisect.bisect_left in VMobject.point_from_proportion
1 parent 2fdb879 commit 276426d

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

manim/mobject/types/vectorized_mobject.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import itertools as it
1616
import sys
1717
import typing
18+
from bisect import bisect_left
1819
from typing import Callable, Optional, Sequence, Union
1920

2021
import colour
@@ -1545,23 +1546,14 @@ def point_from_proportion(self, alpha: float) -> np.ndarray:
15451546
acc_lengths = self.memory["piece_curves"]["acc_lengths"]
15461547
target_length = alpha * acc_lengths[-1]
15471548

1548-
num_curves = self.get_num_curves()
1549-
15501549
# Binary search
1551-
left = 0
1552-
right = num_curves - 1
1553-
while right > left:
1554-
mid = (left + right) // 2
1555-
if acc_lengths[mid] >= target_length:
1556-
right = mid
1557-
else:
1558-
left = mid + 1
1550+
i = bisect_left(acc_lengths, target_length)
15591551

1560-
nth_curve = self.get_nth_curve_function(left)
1561-
if left == 0:
1562-
t = target_length / lengths[left]
1552+
nth_curve = self.get_nth_curve_function(i)
1553+
if i == 0:
1554+
t = target_length / lengths[i]
15631555
else:
1564-
t = (target_length - acc_lengths[left - 1]) / lengths[left]
1556+
t = (target_length - acc_lengths[i - 1]) / lengths[i]
15651557
return nth_curve(t)
15661558

15671559
def proportion_from_point(
@@ -1667,6 +1659,10 @@ def get_anchors(self) -> np.ndarray:
16671659
return self.points
16681660
num_curves = self.get_num_curves()
16691661
anchors = np.empty((2 * num_curves, self.dim))
1662+
# Every end anchor ends a Bézier curve, but not every start anchor
1663+
# begins a full Bézier curve (it can be incomplete). So the start
1664+
# anchors must be sliced in case there are lonely points at the end,
1665+
# but the end anchors don't really need to be sliced.
16701666
anchors[0::2] = self.get_start_anchors()[:num_curves]
16711667
anchors[1::2] = self.get_end_anchors()
16721668
return anchors

0 commit comments

Comments
 (0)