Skip to content

Instantly share code, notes, and snippets.

@noamraph
Created April 19, 2020 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamraph/22cd3d63266b010f8167fd103c6d7a83 to your computer and use it in GitHub Desktop.
Save noamraph/22cd3d63266b010f8167fd103c6d7a83 to your computer and use it in GitHub Desktop.
Code for producing a figure showing dobble matrix
from itertools import combinations
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.text import Text
def dobble(p):
cards = [[] for i in range(p**2 + p + 1)]
cards[0].append(0)
for i in range(p+1):
for j in range(p):
cards[1 + i*p + j].append(i)
cards[i].append(1 + i*p + j)
for i in range(p):
for j in range(p):
for k in range(p):
cards[1 + p + i*p + k].append(1 + p + j*p + (i*j - k) % p)
return cards
for card0, card1 in combinations(dobble(7), 2):
assert len(set(card0) & set(card1)) == 1
def plot_dobble(p, bw=0.15):
cards = dobble(p)
n = p**2 + p + 1
fig, ax = plt.subplots(figsize=(7,7), subplot_kw=dict(aspect='equal'))
fig.subplots_adjust(0, 0, 1, 1)
ax.set_xlim(-bw, n)
ax.set_ylim(n, -bw)
ax.axis('off')
for i, card in enumerate(cards):
for j in range(n):
ax.add_patch(Rectangle((i, j), 1-bw, 1-bw, linewidth=0, color='k', alpha=0.5 if j in card else 0.1))
def horiz(x0, x1, y, color):
ax.add_patch(Rectangle((x0-bw, y-bw), x1-x0+2*bw, bw, linewidth=0, color=color))
def vert(x, y0, y1, color):
ax.add_patch(Rectangle((x-bw, y0-bw), bw, y1-y0+2*bw, linewidth=0, color=color))
def rect(x, y, size, color):
horiz(x, x+size, y, color)
horiz(x, x+size, y+size, color)
vert(x, y, y+size, color)
vert(x+size, y, y+size, color)
for i in range(p+1):
for j in range(p+1):
rect(1 + j*p, 1 + i*p, p, 'C2')
rect(1+p, 1+p, p**2, 'C1')
for i in range(p):
for j in range(p):
ax.add_artist(Text(1+(1.5+j)*p, 1+(1.5+i)*p, '$C_{{{},{}}}$'.format(i, j), ha='center', va='center', size='xx-large', family='serif'))
return fig, ax
fig, ax = plot_dobble(5)
fig.savefig('/tmp/fig.png', dpi=628/10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment