Skip to content

Instantly share code, notes, and snippets.

@jcla1
Last active December 10, 2015 06:48
Show Gist options
  • Save jcla1/4397033 to your computer and use it in GitHub Desktop.
Save jcla1/4397033 to your computer and use it in GitHub Desktop.
On Ulam spirals and matrix generation (blog post)
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;
}
}
}
function translate(coords, d_old){
if (d_old % 2 === 0){
coords[1] -= 1;
} else if (d_old % 2 === 1) {
coords[0] -= 1;
}
return coords;
}
while (check_if_drop_frame(coords, d)){
coords = translate(coords, d);
d -= 1;
}
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