6
6
import numpy as np
7
7
from matplotlib .path import Path
8
8
9
+
9
10
class HatchPatternBase :
10
11
"""
11
12
The base class for a hatch pattern.
12
13
"""
13
14
pass
14
15
16
+
15
17
class HorizontalHatch (HatchPatternBase ):
16
18
def __init__ (self , hatch , density ):
17
19
self .num_lines = (hatch .count ('-' ) + hatch .count ('+' )) * density
18
20
self .num_vertices = self .num_lines * 2
19
21
20
22
def set_vertices_and_codes (self , vertices , codes ):
21
- steps , stepsize = np .linspace (0.0 , 1.0 , self .num_lines , False , retstep = True )
22
- steps += stepsize / 2.
23
+ steps , stepsize = np .linspace (0.0 , 1.0 , self .num_lines , False ,
24
+ retstep = True )
25
+ steps += stepsize / 2.
23
26
vertices [0 ::2 , 0 ] = 0.0
24
27
vertices [0 ::2 , 1 ] = steps
25
28
vertices [1 ::2 , 0 ] = 1.0
26
29
vertices [1 ::2 , 1 ] = steps
27
30
codes [0 ::2 ] = Path .MOVETO
28
31
codes [1 ::2 ] = Path .LINETO
29
32
33
+
30
34
class VerticalHatch (HatchPatternBase ):
31
35
def __init__ (self , hatch , density ):
32
36
self .num_lines = (hatch .count ('|' ) + hatch .count ('+' )) * density
33
37
self .num_vertices = self .num_lines * 2
34
38
35
39
def set_vertices_and_codes (self , vertices , codes ):
36
- steps , stepsize = np .linspace (0.0 , 1.0 , self .num_lines , False , retstep = True )
37
- steps += stepsize / 2.
40
+ steps , stepsize = np .linspace (0.0 , 1.0 , self .num_lines , False ,
41
+ retstep = True )
42
+ steps += stepsize / 2.
38
43
vertices [0 ::2 , 0 ] = steps
39
44
vertices [0 ::2 , 1 ] = 0.0
40
45
vertices [1 ::2 , 0 ] = steps
41
46
vertices [1 ::2 , 1 ] = 1.0
42
47
codes [0 ::2 ] = Path .MOVETO
43
48
codes [1 ::2 ] = Path .LINETO
44
49
50
+
45
51
class NorthEastHatch (HatchPatternBase ):
46
52
def __init__ (self , hatch , density ):
47
- self .num_lines = (hatch .count ('/' ) + hatch .count ('x' ) + hatch .count ('X' )) * density
53
+ self .num_lines = (hatch .count ('/' ) + hatch .count ('x' ) +
54
+ hatch .count ('X' )) * density
48
55
if self .num_lines :
49
56
self .num_vertices = (self .num_lines + 1 ) * 2
50
57
else :
@@ -59,9 +66,11 @@ def set_vertices_and_codes(self, vertices, codes):
59
66
codes [0 ::2 ] = Path .MOVETO
60
67
codes [1 ::2 ] = Path .LINETO
61
68
69
+
62
70
class SouthEastHatch (HatchPatternBase ):
63
71
def __init__ (self , hatch , density ):
64
- self .num_lines = (hatch .count ('\\ ' ) + hatch .count ('x' ) + hatch .count ('X' )) * density
72
+ self .num_lines = (hatch .count ('\\ ' ) + hatch .count ('x' ) +
73
+ hatch .count ('X' )) * density
65
74
self .num_vertices = (self .num_lines + 1 ) * 2
66
75
if self .num_lines :
67
76
self .num_vertices = (self .num_lines + 1 ) * 2
@@ -77,8 +86,10 @@ def set_vertices_and_codes(self, vertices, codes):
77
86
codes [0::2 ] = Path .MOVETO
78
87
codes [1 ::2 ] = Path .LINETO
79
88
89
+
80
90
class Shapes (HatchPatternBase ):
81
91
filled = False
92
+
82
93
def __init__ (self , hatch , density ):
83
94
if self .num_rows == 0 :
84
95
self .num_shapes = 0
@@ -103,38 +114,45 @@ def set_vertices_and_codes(self, vertices, codes):
103
114
if row % 2 == 0 :
104
115
cols = np .linspace (0.0 , 1.0 , self .num_rows + 1 , True )
105
116
else :
106
- cols = np .linspace (offset / 2.0 , 1.0 - offset / 2.0 , self .num_rows , True )
117
+ cols = np .linspace (offset / 2.0 , 1.0 - offset / 2.0 ,
118
+ self .num_rows , True )
107
119
row_pos = row * offset
108
120
for col_pos in cols :
109
- vertices [cursor :cursor + shape_size ] = shape_vertices + (col_pos , row_pos )
110
- codes [cursor :cursor + shape_size ] = shape_codes
121
+ vertices [cursor :cursor + shape_size ] = (shape_vertices +
122
+ (col_pos , row_pos ))
<
6D40
td data-grid-cell-id="diff-9fa22d18bda986f54317196aa0414a09252e8421779c006ed9ac51bf229ed1e9-110-123-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
123
+ codes [cursor :cursor + shape_size ] = shape_codes
111
124
cursor += shape_size
112
125
if not self .filled :
113
- vertices [cursor :cursor + shape_size ] = inner_vertices + (col_pos , row_pos )
114
- codes [cursor :cursor + shape_size ] = shape_codes
126
+ vertices [cursor :cursor + shape_size ] = (inner_vertices +
127
+ (col_pos , row_pos ))
128
+ codes [cursor :cursor + shape_size ] = shape_codes
115
129
cursor += shape_size
116
130
131
+
117
132
class Circles (Shapes ):
118
133
def __init__ (self , hatch , density ):
119
134
path = Path .unit_circle ()
120
135
self .shape_vertices = path .vertices
121
136
self .shape_codes = path .codes
122
137
Shapes .__init__ (self , hatch , density )
123
138
139
+
124
140
class SmallCircles (Circles ):
125
141
size = 0.2
126
142
127
143
def __init__ (self , hatch , density ):
128
144
self .num_rows = (hatch .count ('o' )) * density
129
145
Circles .__init__ (self , hatch , density )
130
146
147
+
131
148
class LargeCircles (Circles ):
132
149
size = 0.35
133
150
134
151
def __init__ (self , hatch , density ):
135
152
self .num_rows = (hatch .count ('O' )) * density
136
153
Circles .__init__ (self , hatch , density )
137
154
155
+
138
156
class SmallFilledCircles (SmallCircles ):
139
157
size = 0.1
140
158
filled = True
@@ -143,6 +161,7 @@ def __init__(self, hatch, density):
143
161
self .num_rows = (hatch .count ('.' )) * density
144
162
Circles .__init__ (self , hatch , density )
145
163
164
+
146
165
class Stars (Shapes ):
147
166
size = 1.0 / 3.0
148
167
filled = True
@@ -166,23 +185,24 @@ def __init__(self, hatch, density):
166
185
Stars
167
186
]
168
187
188
+
169
189
def get_path (hatchpattern , density = 6 ):
170
190
"""
171
191
Given a hatch specifier, *hatchpattern*, generates Path to render
172
192
the hatch in a unit square. *density* is the number of lines per
173
193
unit square.
174
194
"""
175
- size = 1.0
176
195
density = int (density )
177
196
178
- patterns = [hatch_type (hatchpattern , density ) for hatch_type in _hatch_types ]
197
+ patterns = [hatch_type (hatchpattern , density )
198
+ for hatch_type in _hatch_types ]
179
199
num_vertices = sum ([pattern .num_vertices for pattern in patterns ])
180
200
181
201
if num_vertices == 0 :
182
202
return Path (np .empty ((0 , 2 )))
183
203
184
204
vertices = np .empty ((num_vertices , 2 ))
185
- codes = np .empty ((num_vertices ,), np .uint8 )
205
+ codes = np .empty ((num_vertices ,), np .uint8 )
186
206
187
207
cursor = 0
188
208
for pattern in patterns :
0 commit comments