Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit bc54836

Browse files
author
Release Manager
committed
Trac #31646: clean BezierPath constructor
From http://ask.sagemath.org/question/56608/possible-problem-in- bezier_pathpy/ URL: https://trac.sagemath.org/31646 Reported by: vdelecroix Ticket author(s): Vincent Delecroix Reviewer(s): Travis Scrimshaw
2 parents bd3334c + 33a9e9d commit bc54836

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

src/sage/plot/bezier_path.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#
1919
# http://www.gnu.org/licenses/
2020
#*****************************************************************************
21-
from copy import deepcopy
2221
from sage.plot.primitive import GraphicPrimitive_xydata
2322
from sage.misc.decorators import options, rename_keyword
2423
from sage.plot.colors import to_mpl_color
@@ -35,7 +34,7 @@ class BezierPath(GraphicPrimitive_xydata):
3534
3635
sage: from sage.plot.bezier_path import BezierPath
3736
sage: BezierPath([[(0,0), (.5,.5),(1,0)],[(.5,1),(0,0)]], {'linestyle':'dashed'})
38-
Bezier path from (0, 0) to (0, 0)
37+
Bezier path from (0.0, 0.0) to (0.0, 0.0)
3938
4039
We use :func:`bezier_path` to actually plot Bezier curves::
4140
@@ -56,17 +55,44 @@ def __init__(self, path, options):
5655
5756
sage: from sage.plot.bezier_path import BezierPath
5857
sage: BezierPath([[(0,0),(.5,.5),(1,0)],[(.5,1),(0,0)]], {'linestyle':'dashed'})
59-
Bezier path from (0, 0) to (0, 0)
58+
Bezier path from (0.0, 0.0) to (0.0, 0.0)
59+
60+
sage: BezierPath([[(0,0), (1,2), (3,6), (2,-1), (3,3)]], {})
61+
Traceback (most recent call last):
62+
...
63+
ValueError: invalid input for BezierPath
64+
65+
TESTS:
66+
67+
Check :trac:`31646`::
68+
69+
sage: from sage.plot.bezier_path import BezierPath
70+
sage: p2d = [[(3,0),(4,1),(2,1),(3,0)], [(2,2),(3,1),(2,1)]]
71+
sage: P = BezierPath(p2d, {})
72+
sage: P.path
73+
[array([[3., 0.], [4., 1.], [2., 1.], [3., 0.]]),
74+
array([[2., 2.], [3., 1.], [2., 1.]])]
6075
"""
6176
import numpy as np
62-
self.path = deepcopy(path)
63-
codes = [1] + (len(self.path[0])-1)*[len(self.path[0])]
64-
vertices = self.path[0]
65-
for curve in self.path[1:]:
66-
vertices += curve
67-
codes += (len(curve)) * [len(curve)+1]
77+
78+
self.path = [np.array(l, float) for l in path]
79+
80+
# In oder to feed later to matplotlib.path.Path we convert in the following form
81+
# - vertices: an Nx2 float array of vertices
82+
# - codes: an N-length uint8 array of vertex types, or None
83+
# where each code could be MOVETO (=1), LINETO (=2), CURVE3 (=3), CURVE4 (=4)
84+
self.vertices = np.concatenate(self.path)
85+
N, _ = self.vertices.shape
86+
codes = np.zeros((N,), np.uint8)
87+
k = 0
88+
for i, curve in enumerate(self.path):
89+
code = len(curve) + (i > 0)
90+
if code < 2 or code > 4:
91+
raise ValueError('invalid input for BezierPath')
92+
codes[k:k+len(curve)] = code
93+
k += len(curve)
94+
codes[0] = 1 # MOVETO
6895
self.codes = codes
69-
self.vertices = np.array(vertices, float)
7096
GraphicPrimitive_xydata.__init__(self, options)
7197

7298
def _allowed_options(self):
@@ -178,9 +204,11 @@ def _repr_(self):
178204
sage: from sage.plot.bezier_path import BezierPath
179205
sage: B = BezierPath([[(0,0),(.5,.5),(1,0)],[(.5,1),(0,0)]], {'linestyle':'dashed'})
180206
sage: B._repr_()
181-
'Bezier path from (0, 0) to (0, 0)'
207+
'Bezier path from (0.0, 0.0) to (0.0, 0.0)'
182208
"""
183-
return "Bezier path from %s to %s" % (self.path[0][0], self.path[-1][-1])
209+
x0, y0 = self.vertices[0]
210+
x1, y1 = self.vertices[-1]
211+
return "Bezier path from (%s, %s) to (%s, %s)" % (x0, y0, x1, y1)
184212

185213
def _render_on_subplot(self, subplot):
186214
"""

0 commit comments

Comments
 (0)