Skip to content

Instantly share code, notes, and snippets.

@ChamodDamitha
Last active June 7, 2020 09:59
Show Gist options
  • Save ChamodDamitha/e6c54e73237bb264ad844c24cfd601ed to your computer and use it in GitHub Desktop.
Save ChamodDamitha/e6c54e73237bb264ad844c24cfd601ed to your computer and use it in GitHub Desktop.
from itertools import permutations
def generateNeighbours(point):
perm = set(permutations([-1, -1, -1, 0, 0, 1, 1, 1], 3))
return [(point[0] + p[0], point[1] + p[1], point[2] + p[2]) for p in perm]
def distance(a, b):
return (a[0] - b[0])**2 + (a[1] - b[1])**2 + (a[2] - b[2])**2
def sortLevel(level, point):
dist = [(distance(point, p), p) for p in level]
dist.sort()
return [p[1] for p in dist]
def generateColors(origin, n):
finalList = [origin]
levels = [[origin]]
count = 0
for level in levels:
newLevel = set()
for p in level:
for neighbour in generateNeighbours(p):
newLevel.add(neighbour)
sortedNewLevel = [c for c in sortLevel(
newLevel, origin) if c not in finalList]
levels.append(sortedNewLevel)
finalList += sortedNewLevel
count += len(sortedNewLevel)
if count >= n:
break
return finalList[1:n + 1]
print(generateColors((255, 0, 0), 50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment