|
15 | 15 | import itertools as it |
16 | 16 | import sys |
17 | 17 | import typing |
| 18 | +from bisect import bisect_left |
18 | 19 | from typing import Callable, Optional, Sequence, Union |
19 | 20 |
|
20 | 21 | import colour |
@@ -1545,23 +1546,14 @@ def point_from_proportion(self, alpha: float) -> np.ndarray: |
1545 | 1546 | acc_lengths = self.memory["piece_curves"]["acc_lengths"] |
1546 | 1547 | target_length = alpha * acc_lengths[-1] |
1547 | 1548 |
|
1548 | | - num_curves = self.get_num_curves() |
1549 | | - |
1550 | 1549 | # 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) |
1559 | 1551 |
|
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] |
1563 | 1555 | else: |
1564 | | - t = (target_length - acc_lengths[left - 1]) / lengths[left] |
| 1556 | + t = (target_length - acc_lengths[i - 1]) / lengths[i] |
1565 | 1557 | return nth_curve(t) |
1566 | 1558 |
|
1567 | 1559 | def proportion_from_point( |
@@ -1667,6 +1659,10 @@ def get_anchors(self) -> np.ndarray: |
1667 | 1659 | return self.points |
1668 | 1660 | num_curves = self.get_num_curves() |
1669 | 1661 | 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. |
1670 | 1666 | anchors[0::2] = self.get_start_anchors()[:num_curves] |
1671 | 1667 | anchors[1::2] = self.get_end_anchors() |
1672 | 1668 | return anchors |
|
0 commit comments