canvas = document.getElementById('canvas')
ctx = canvas.getContext('2d')
scoreArea = document.getElementById('scoreArea')
pastedData = document.getElementById('pastedData')
timeout = 0
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
primesSquared = []
for (i=0; i<primes.length; i++) {
primesSquared.push(primes[i] * primes[i])
}
width = canvas.width
height = canvas.height
area = width * height
scoringFilter = calculateScoringFilter()
function calculateScore() {
var percentage = Math.floor(currentIndex / 65536 * 10000) / 100
scoreArea.value = 'Calculating:' + percentage + '%'
for (var t=0; t<1000; t++) {
if (currentIndex < 65536) {
partialScore += pixelScore(currentIndex)
currentIndex += 1
} else {
scoreArea.value = 'Score: ' + partialScore
return
}
}
timeout = setTimeout(calculateScore, 10)
}
function pixelScore(i) {
var score = 0
var x = i % width
var y = Math.floor(i / width)
var a, b, xd, yd, j, k, d, m
for (k=0; k<scoringFilter.length; k++) {
bundle = scoringFilter[k]
m = bundle[0]
a = m % width
b = Math.floor(m / width)
j = ((x+a) % width) + width * ((y+b) % height)
d = bundle[1]
score += Math.abs(pixels[i] - pixels[j]) / d
}
return score
}
function display(pixels) {
for (var i=0; i<area; i++) {
ctx.fillStyle = pixels[i] ? 'white' : 'black'
ctx.fillRect(i % width, Math.floor(i / width), 1, 1)
}
}
function stop() {
clearTimeout(timeout)
scoreArea.value = 'Calculation cancelled.'
}
function isPrimeSquared(n) {
return primesSquared.indexOf(n) > -1
}
function applyToImage() {
var potentialPixels = []
var pastedString = pastedData.value
if (pastedString.length !== 65536) {
scoreArea.value = 'Incorrect length:' + pastedString.length
return
}
var i, digit
for (i=0; i<pastedString.length; i++) {
digit = pastedString.substring(i, i + 1)
if (digit === '0') {
potentialPixels.push(0)
} else if (digit === '1') {
potentialPixels.push(1)
} else {
scoreArea.value = 'Invalid character ' + i + ':' + digit
return
}
}
pixels = potentialPixels
display(pixels)
scoreArea.value = 'Calculating score...'
partialScore = 0
currentIndex = 0
timeout = setTimeout(calculateScore, 10)
}
function calculateScoringFilter() {
var scoringFilter = []
var i, x, y, xd, yd
for (i=0; i<area; i++) {
x = i % width
y = Math.floor(i / width)
xd = Math.min(x,(-x+width)%width)
yd = Math.min(y,(-y+height)%height)
dSquared = xd*xd + yd*yd
if (xd && yd && isPrimeSquared(dSquared)) {
d = Math.sqrt(dSquared)
scoringFilter.push([i, d])
}
}
return scoringFilter
}
<canvas id='canvas' width='256' height='256'></canvas>
<br>
<input id='scoreArea' rows='1' readonly></input>
<br>
<button onclick='applyToImage()'>Apply string to image</button>
<button onclick='stop()'>Stop</button>
<br>
<br>
Paste in string of 65,536 zeroes and ones here:
<br>
<input id='pastedData'></input>