Skip to content

Instantly share code, notes, and snippets.

@zzandy
Created March 20, 2012 16:43
Show Gist options
  • Save zzandy/2138069 to your computer and use it in GitHub Desktop.
Save zzandy/2138069 to your computer and use it in GitHub Desktop.
hex grid conversions (WIP)
from random import randrange
from math import sqrt, ceil
# expencations
# ex[world] = [screen]
ex12 = [[(1,2), (1,4), (1,6)], [(0,3),(0,5),(0,7)], [(0,4),(0,6),(0,8)], [(-1,5),(-1,7),(-1,9)]]
ex13 = [[(1,3), (1,5), (1,7), (1,9)], [(1,4),(1,6),(1,8),(1,10)], [(0,5),(0,7),(0,9), (0,11)], [(0, 6),(0,8),(0,10), (0,12)]]
ex33 = [[(3,3),(3,4),(2,5),(2,6)],
[(2,3),(2,4),(1,5),(1,6)],
[(1,3),(1,4),(0,5),(0,6)]]
ex34 = [[(3,4),(2,5),(2,6),( 1,7)],
[(2,4),(1,5),(1,6),( 0,7)],
[(1,4),(0,5),(0,6),(-1,7)]]
def printex(ex):
for i in range(len(ex)):
for j in range(len(ex[i])):
print '(%d, %d) -> (%d, %d)' % (i, j, ex[i][j][0], ex[i][j][1])
##printex(ex12)
##print
##printex(ex13)
##print
##print
# world to screen
def scr(y0,x0,yw,xw):
y = y0 - (xw+(x0+1)%2)/2 - yw
x = xw+x0
return (y,x)
def screen(y0,x0,ex):
print '+-------+-------+--------+'
print '| world | guess | screen |'
print '+-------+-------+--------+'
for a,b,c,d in [(y, x, \
scr(y0,x0,y,x)[0] , \
scr(y0,x0,y,x)[1] \
## y0 - int((y+(x0+1)%2)/2), \
## x0 + 2*x + y \
) for y in range(len(ex)) for x in range(len(ex[y]))]:
print "| %2d %2d | %2d %2d | %2d %2d | %s %s" % (a,b,c,d,ex[a][b][0],ex[a][b][1],'*' if ex[a][b][0] == c else ' ','*' if ex[a][b][1] == d else ' ')
print '+-------+-------+--------+'
##screen(3,3,ex33)
##print
##screen(3,4,ex34)
##print
##print
#screen to world
def wld(y0,x0,ys,xs):
y = 2*(y0 - ys) + (1+ys + xs + ys%2)%2 - (x0+1)%2
x = (xs-x0)/2 - (y0 - ys - (x0+1)%2*(xs-x0)%2)
x = xs - x0
y = y0 - (x+(x0+1)%2)/2 - ys
return (y,x)
def world(y0,x0,ex):
print '+--------+-------+-------+'
print '| screen | guess | world |'
print '+--------+-------+-------+'
for yw in range(len(ex)):
for xw in range(len(ex[yw])):
(ys, xs) = (ex[yw][xw])
y = 2*(y0 - ys) + (1+ys + xs + ys%2)%2 - (x0+1)%2
x = (xs-x0)/2 - (y0 - ys - (x0+1)%2*(xs-x0)%2)
(y,x) = wld(y0,x0,ys,xs)
print "| %2d %2d | %2d %2d | %2d %2d | %s %s" % (ys, xs, y,x, yw, xw, '*' if y==yw else ' ', '*' if x == xw else ' ')
print '+--------+-------+-------+'
def crosstest():
y0 = randrange(-9,9)
x0 = randrange(-9,9)
n = 100
for i in range(n):
(x,y) = (randrange(-99, 100), randrange(-99, 100))
(ys,xs) = scr(y0,x0,y,x)
(yw,xw) = wld(y0,x0,ys,xs)
if not xw==x or not yw == y:
print 'no match(wsw): (%d, %d) -> (%d, %d) -> (%d, %d)' % (y,x,ys,xs,yw,xw)
for i in range(n):
(x,y) = (randrange(-99, 100), randrange(-99, 100))
(ys,xs) = wld(y0,x0,y,x)
(yw,xw) = scr(y0,x0,ys,xs)
if not xw==x or not yw == y:
print 'no match(sws): (%d, %d) -> (%d, %d) -> (%d, %d)' % (y,x,ys,xs,yw,xw)
##world(3,3,ex33)
##print
##world(3,4,ex34)
##print
def a(n):
return 3*n*(n+1)
def n(a):
return (sqrt(12 * a + 9) - 3) / 6
def ring(st):
return int(ceil(n(st)))
# world to storage
def store(y,x):
return 0
def fromstore(s):
if s == 0:
return (0,0)
qy = [[0,1],[1,0],[1,-1],[0,-1],[-1,0],[-1,1]]
qx = [[1,-1],[0,-1],[-1,0],[-1,1],[0,1],[1,0]]
r = ring(s)
d = s - a(ring(s)-1)-1
sx = d / r
sn = d % r
# print s, r, d, sx, sn
# todo: matrix
return (qy[sx][0] * r + qy[sx][1] * sn, qx[sx][0] * r + qx[sx][1] * sn)
return (0,0)
def teststore(ex):
print '+--------+-------+-------+'
print '| screen | guess | store |'
print '+--------+-------+-------+'
for (y,x,s) in ex:
st = store(y,x)
print '| %2d %2d | %2d | %2d | %s' % (y,x,st,s,'*' if st==s else ' ')
print '+--------+-------+-------+'
def testback(ex):
print '+-------+-------+--------+'
print '| store | guess | screen |'
print '+-------+-------+--------+'
for (y,x,s) in ex:
(ys,xs) = fromstore(s)
print '| %2d | %2d %2d | %2d %2d | %s %s' % (s, ys, xs, y,x,'*' if ys==y else ' ','*' if x==xs else ' ')
print '+-------+-------+--------+'
exs=[[0,0,0],[0,1,1],[1,0,2],[1,-1,3],[0,-1,4],[-1,0,5],[-1,1,6]]
def crossstore():
n = 100
for i in range(n):
(x,y) = (randrange(-99, 100), randrange(-99, 100))
s = store(y,x)
(xs,ys) = fromstore(s)
if not xs==x or not ys == y:
print 'no match(wsw): (%d, %d) -> %d -> (%d, %d)' % (y,x,s,ys,xs)
for i in range(n):
s = randrange(0, 100)
(ys,xs) = fromstore(s)
s2 = store(ys,xs)
if not s==s2:
print 'no match(sws): %d -> (%d, %d) -> %d' % (s, ys,xs,s2)
crossstore()
print
teststore(exs)
print
testback(exs)
for i in range(7,19):
(y,x) = fromstore(i)
print '%2d %2d %2d %2d %2d %f' % (i, y,x, abs(y)+abs(x), abs(y+x), (abs(y)+abs(x) + abs(y+x))/2)
print
for i in range(19,37):
(y,x) = fromstore(i)
print '%2d %2d %2d %2d %2d %f' % (i, y,x, abs(y)+abs(x), abs(y+x), (abs(y)+abs(x) + abs(y+x))/2)
def fn(s):
r = ring(s)
n = s - a(ring(s)-1)-1
sext = n / r
print r,n,sext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment