-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutandshift.py
102 lines (76 loc) · 2.3 KB
/
cutandshift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
""" implementing a brainstormed cut-and-shift operation on polygons
working from code snagged from Gary Whitehead in masto conversation:
https://mastodon.social/@grwster/109734946667028881
"""
from cortexdraw import *
circles = []
patches = []
squaresize = 2
width = squaresize * 2
height = squaresize * 2
def op2(m):
tophalf = int(height/2)
bottomhalf = height
for i in range(tophalf):
m[i].insert(0, 0)
m[i].pop()
for i in range(tophalf + 1, bottomhalf):
m[i].append(0)
m[i].pop(0)
def op1(m):
for i in range(11):
newgrid = [[row[i] for row in m] for i in range(width)]
op2(newgrid)
m = [[row[i] for row in newgrid] for i in range(width)]
# construct a 2D array of zero values and stock it with square shape number values
# in the center
# - NOTE: squaresize is expected to be a multiple of 2!
def makearray(squaresize):
gsize = squaresize * 2
grid = []
# create an array of zeros big enough to allow space to shift the square's bits around
for i in range(gsize):
newl = []
for j in range(gsize):
newl.append(0)
grid.append(newl)
# and then add squares digits to the center
dxy = int(squaresize/2)
for i in range(1, squaresize*squaresize + 1):
grid[i % squaresize + dxy][int(i/squaresize)] = i
return grid
def newpatch(dx, dy):
offx = dx * width + 1
offy = dy * height + 1
for x in range(len(m)):
for y in range(len(m[x])):
r = 1
if m[x][y] > 0:
# r = 0.1
circles.append(([x+offx, y+offy], r))
for c, r in circles:
ax.add_patch(mpatches.Arc(c, r, r, color=[0, 0, 0]))
m = makearray(squaresize)
print(m)
fig, ax = plt.subplots(figsize=(10, 10), frameon=False)
newpatch(0, 0)
for i in range(1, 6, 2):
op1(m)
print(m)
newpatch(i, 0)
op2(m)
newpatch(i+1, 0)
# for i in range(6):
# newpatch(i,0)
plt.grid(False)
plt.axis('off')
ax.set_aspect('equal')
x_bounds = [-1, width * 6 + 1]
y_bounds = [-1, height * 1 + 1]
ax.set_xlim(x_bounds)
ax.set_ylim(y_bounds)
collection = PatchCollection(patches, match_original=True)
ax.add_collection(collection)
plt.savefig('cutandshift.svg', bbox_inches='tight', pad_inches=0)
plt.show()
vpypeout(['cutandshift.svg'])