Skip to content

Instantly share code, notes, and snippets.

@kordless
Last active October 13, 2017 04:09
Show Gist options
  • Save kordless/d577a8485ed753dcbb62a2321be5a2bb to your computer and use it in GitHub Desktop.
Save kordless/d577a8485ed753dcbb62a2321be5a2bb to your computer and use it in GitHub Desktop.
Nodebox Visualization for Increasing Binary Prime Spiral
import pyprimes
import random
size(250,250)
speed(20)
def setup():
print "running"
global r
r = 0
global pixels
pixels = []
def draw():
x = 125
y = 125
global r
global pixels
# how many primes this run?
# loosly defines increasing size of spiral
r = r + 1
# create a list of 1s and 0s representing X many primes
# spiral object is an array of strings of either '1' or '0'
# pops pull off "last" digit of largest prime which is plotted
# from the middle of the image outward to smallest prime, 2
obj2 = []
for t in range(r):
p = pyprimes.nth_prime(t+1) # generate r many primes
# p = t*3 # various compartive functions for testing results
fh = "{0:b}".format(p) # binary this prime
# print fh
for i in fh:
obj2.append(i) # append to spiral object
# bit to test for rotation add/sub state (sub x, sub y, add x, add y)
aors = 1
f = 0 # f is spiral factor (length of a side segment)
# print obj2
# grab the first digit
b = obj2.pop()
# start tracking history of pixels
global pixels
pcount = len(pixels)
pcurrent = 1
# do stuff for center pixel
if pcount > 0:
if b == '1':
pixels[0]['count1'] += 1
fill(0.9)
else:
pixels[0]['count0'] += 1
fill(0.1)
else:
if b == '1':
pixels.append({'count1': 1, 'count0': 0})
else:
pixels.append({'count0': 1, 'count1': 0})
rect(x,y,2,2)
while len(obj2) > f:
f = f + 1
if aors == 1: # subtraction phase
aors = 0;
# run ys
for i in range(f):
y = y - 2
if len(obj2) > 0:
b = obj2.pop()
else:
break
if b == '1':
fill(0.1)
else:
fill(0.9)
rect(x,y,2,2)
# run xs
for i in range(f):
x = x - 2
if len(obj2) > 0:
b = obj2.pop()
else:
break
if b == '1':
fill(0.1)
else:
fill(0.9)
rect(x,y,2,2)
else: # addition phase
aors = 1
# run ys
for i in range(f):
y = y + 2
if len(obj2) > 0:
b = obj2.pop()
pcurrent = pcurrent + 1
else:
break
if b == '1':
fill(0.1)
else:
fill(0.9)
rect(x,y,2,2)
# run xs
for i in range(f):
x = x + 2
if len(obj2) > 0:
b = obj2.pop()
pcurrent = pcurrent + 1
else:
break
if b == '1':
fill(0.1)
else:
fill(0.9)
rect(x,y,2,2)
@kordless
Copy link
Author

updated to actually work. need to upload pyprimes.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment