Skip to content

Instantly share code, notes, and snippets.

@davidnuon
Created October 19, 2012 18:13
Show Gist options
  • Save davidnuon/3919739 to your computer and use it in GitHub Desktop.
Save davidnuon/3919739 to your computer and use it in GitHub Desktop.
Checking if two cube nets are of the same
# Sample nets for cubes
net_T = [(0,0), (0,2), (0,1), (1,1), (2,1),(3,1)]
net_t = [(1,0), (1,2), (1,1), (0,1), (2,1),(3,1)]
# Another net
good = [(0,3), (0,5), (0,4), (1,4), (2,4),(3,4)]
deviant = [(0,3), (0,4), (0,5), (1,4), (2,4),(1,3)]
# Grid to layout the net
space = map(lambda x : [0,0,0,0,0,0], xrange(0, 5))
# Check if 2 nets are of the same type
def iso(a, b):
# Sort the 2d coords
net1 = sorted(a)
net2 = sorted(b)
# If they're not the same length, end
if len(net1) != len(net2):
return False
# Go through both of the nets and differences in the lists
# of coords. Since they're sorted, we just need to check if each
# difference is the same.
for i in xrange(0, len(net1) - 1):
delta1 = (net1[i][0] - net1[i+1][0], net1[i][1] - net1[i+1][1])
delta2 = (net2[i][0] - net2[i+1][0], net2[i][1] - net2[i+1][1])
# If the differences aren't the same, end
if delta1 != delta2:
return False
return True
def map_net(net):
global space
for n in net:
space[n[0]][n[1]] = '1'
def clear():
global space
space = map(lambda x : [0,0,0,0,0,0], xrange(0, 5))
def spit():
global space
for s in space:
for k in s:
print k,
print
print iso(good, good) # True
print iso(deviant, deviant) # True
print
print iso(net_t, net_T) # False
print iso(good, deviant) # False
print
print iso(good, net_T) # True
print iso(good, net_t) # False
print iso(deviant, net_T) # False
print iso(deviant, net_t) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment