|
2 | 2 | A module providing some utility functions regarding Bezier path manipulation.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import math |
| 6 | + |
5 | 7 | import numpy as np
|
6 | 8 |
|
7 | 9 | import matplotlib.cbook as cbook
|
@@ -167,36 +169,26 @@ def find_bezier_t_intersecting_with_closedpath(
|
167 | 169 |
|
168 | 170 | class BezierSegment:
|
169 | 171 | """
|
170 |
| - A 2-dimensional Bezier segment. |
| 172 | + A D-dimensional Bezier segment. |
171 | 173 |
|
172 | 174 | Parameters
|
173 | 175 | ----------
|
174 |
| - control_points : array-like (N, 2) |
175 |
| - A list of the (x, y) positions of control points of the Bezier line. |
176 |
| - This must contain N points, where N is the order of the Bezier line. |
177 |
| - 1 <= N <= 3 is supported. |
| 176 | + control_points : (N, D) array |
| 177 | + Location of the *N* control points. |
178 | 178 | """
|
179 |
| - # Higher order Bezier lines can be supported by simplying adding |
180 |
| - # corresponding values. |
181 |
| - _binom_coeff = {1: np.array([1., 1.]), |
182 |
| - 2: np.array([1., 2., 1.]), |
183 |
| - 3: np.array([1., 3., 3., 1.])} |
184 | 179 |
|
185 | 180 | def __init__(self, control_points):
|
186 |
| - _o = len(control_points) |
187 |
| - self._orders = np.arange(_o) |
188 |
| - |
189 |
| - _coeff = BezierSegment._binom_coeff[_o - 1] |
190 |
| - xx, yy = np.asarray(control_points).T |
191 |
| - self._px = xx * _coeff |
192 |
| - self._py = yy * _coeff |
| 181 | + n = len(control_points) |
| 182 | + self._orders = np.arange(n) |
| 183 | + coeff = [math.factorial(n - 1) |
| 184 | + // (math.factorial(i) * math.factorial(n - 1 - i)) |
| 185 | + for i in range(n)] |
| 186 | + self._px = np.asarray(control_points).T * coeff |
193 | 187 |
|
194 | 188 | def point_at_t(self, t):
|
195 |
| - """Return the point (x, y) at parameter *t*.""" |
196 |
| - tt = ((1 - t) ** self._orders)[::-1] * t ** self._orders |
197 |
| - _x = np.dot(tt, self._px) |
198 |
| - _y = np.dot(tt, self._py) |
199 |
| - return _x, _y |
| 189 | + """Return the point on the Bezier curve for parameter *t*.""" |
| 190 | + return tuple( |
| 191 | + self._px @ (((1 - t) ** self._orders)[::-1] * t ** self._orders)) |
200 | 192 |
|
201 | 193 |
|
202 | 194 | @cbook._rename_parameter("3.1", "tolerence", "tolerance")
|
|
0 commit comments