Read the full blog post at: http://jcla1.com/blog/on-ulam-spirals-and-matrix-generation/.
Last active
December 10, 2015 06:48
-
-
Save jcla1/4397033 to your computer and use it in GitHub Desktop.
On Ulam spirals and matrix generation (blog post)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function check_if_drop_frame(coords, d){ | |
if (d % 2 === 0){ | |
if (coords[0] != d && coords[1] != 1){ | |
return true; | |
} else { | |
return false; | |
} | |
} else if (d % 2 === 1) { | |
if (coords[0] != 1 && coords[1] != d){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function translate(coords, d_old){ | |
if (d_old % 2 === 0){ | |
coords[1] -= 1; | |
} else if (d_old % 2 === 1) { | |
coords[0] -= 1; | |
} | |
return coords; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (check_if_drop_frame(coords, d)){ | |
coords = translate(coords, d); | |
d -= 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function work_out_value(coords, d){ | |
var val = -1; | |
var x = coords[0], | |
y = coords[1]; | |
if (d % 2 === 0){ | |
if (coords[1] === 1){ | |
val = Math.pow(d, 2) - (coords[0] - 1) | |
} else if (coords[1] === d){ | |
val = Math.pow(d, 2) - 2 * (d - 1) - (d - 2) + (x - 1) - 1 | |
} else if (coords[0] === 1){ | |
val = Math.pow(d, 2) - 3 * (d - 1) - (d - 2) + (y - 2) | |
} else if (coords[0] === d){ | |
val = Math.pow(d, 2) - d - (y - 2) | |
} | |
} else if (d % 2 === 1){ | |
if (coords[1] === 1){ | |
val = Math.pow(d, 2) - 2 * (d - 1) - (x - 1) | |
} else if (coords[1] === d){ | |
val = Math.pow(d, 2) - (d - 1) + (x - 1) | |
} else if (coords[0] === 1){ | |
val = Math.pow(d, 2) - 2 * (d - 1) + (y - 1) | |
} else if (coords[0] === d){ | |
val = Math.pow(d, 2) - 3 * (d - 1) - (y - 1) | |
} | |
} | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment