Skip to content

Commit fb86190

Browse files
authored
Merge pull request #13355 from anntzer/beziersegment
Simplify and generalize BezierSegment.
2 parents 85efd95 + 9ad23b7 commit fb86190

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

examples/user_interfaces/embedding_in_wx4_sgskip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, canvas):
2525
# We use a stock wx bitmap, but you could also use your own image file.
2626
bmp = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR)
2727
tool = self.AddTool(wx.ID_ANY, 'Click me', bmp,
28-
'Activate custom contol')
28+
'Activate custom control')
2929
self.Bind(wx.EVT_TOOL, self._on_custom, id=tool.GetId())
3030

3131
def _on_custom(self, evt):

lib/matplotlib/bezier.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
A module providing some utility functions regarding Bezier path manipulation.
33
"""
44

5+
import math
6+
57
import numpy as np
68

79
import matplotlib.cbook as cbook
@@ -167,36 +169,26 @@ def find_bezier_t_intersecting_with_closedpath(
167169

168170
class BezierSegment:
169171
"""
170-
A 2-dimensional Bezier segment.
172+
A D-dimensional Bezier segment.
171173
172174
Parameters
173175
----------
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.
178178
"""
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.])}
184179

185180
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
193187

194188
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))
200192

201193

202194
@cbook._rename_parameter("3.1", "tolerence", "tolerance")

0 commit comments

Comments
 (0)