# Talk:Generating all integer lattice points (Python)

From LiteratePrograms

To do 1: generalize to *n* dimensions

To do 2: figure out how to do it by Euclidean distance. I'm kind of stuck here -- any ideas? Maybe something similar to the classical functional algorithm for Hamming numbers is possible? Perhaps first generate all combinations having the next distance (if so, how?) and for each combination generate each of its permutations? Help would be appreciated. Fredrik 04:37, 26 July 2006 (PDT)

- I've now implemented the case
*n*=2 with the simplest geometrical approach I could think of... the solution seems decent to me, but perhaps*d*should be increased (dynamically) for efficiency. Fredrik 07:30, 26 July 2006 (PDT)

## [edit] Animation code

Code used to view animation/generate the animation frames:

frompygameimport*frompygame.drawimportrectfrompygame.imageimportsavefromtimeimportsleepfromrandomimportrandrange midpoint, gridsize = 200, 20 colors = [(255,127,0),(247,216,4),(0,143,210),(0,214,236),(78,44,163)] display.init() screen = display.set_mode((midpoint*2, midpoint*2), 0, 32) screen.fill((255, 255, 255))defblitpoint(x, y, color, frame): xpos = midpoint + gridsize*x ypos = midpoint + gridsize*y rect(screen, color, Rect(xpos-9,ypos-9,18,18)) display.flip() save(screen, "lattice%03i.bmp"%frame)defplotlattice(generator):forxinrange(0, midpoint*2+1, gridsize):foryinrange(0, midpoint*2+1, gridsize): rect(screen, (128, 128, 128), Rect(x-1,y-1,2,2)) frame = 0forpingenerator(8): blitpoint(p[0],p[1],colors[(abs(p[0])+abs(p[1]))%5],frame) frame += 1whileTrue: ev = event.poll()ifev.type == NOEVENT:breakifev.type == QUITorev.type == KEYDOWN:raiseSystemExit sleep(0.05)

For the Euclidean plot, I changed the color selection code to `colors[int(distance(p[0],p[1])**0.5+0.5)%5]`. Fredrik 07:53, 26 July 2006 (PDT)