spiral data
by Igor Cuckovic
JavaScript
//http://stackoverflow.com/questions/8757514/matrix-and-algorithm-spiral?rq=1
console.clear();
var svg = d3.select("body").append("svg").attr({
"width": "100%",
"height": "100%"
});
svg.append("rect").attr({
"x": 0,
"y": 0,
"width": "100%",
"height": "100%",
"fill": "#3E3E40"
});
var placeRectangle = function (x1, y1, i1) {
var groups = svg.append("g").attr("transform", function () {
var x = 44 * x1 + 100;
var y = -(44 * y1) + 100;
// console.log([x, y]);
return "translate(" + [x, y] + ")";
})
groups.append("rect").attr({
"x": 0,
"y": 0,
"width": 44,
"height": 44,
"rx": 2,
"ry": 2,
"fill": "none",
"stroke": "#ffffff"
});
groups.append("text").text(i1).attr("transform","translate(17,25)")
}
var x = 0,
y = 0;
// var delta = [0, -1];
var delta = [-1, 0];
var myArray = [];
var calculatePositions = function (width, height) {
x = Math.floor(width / 2);
y = -Math.floor(height / 2);
for (i = 1 ; i <= (width * height); i += 1) {
//console.log([x,y]);
placeRectangle(x, y, i)
console.log([x, y] + " pozicija " + i);
if ((-width / 2 < x <= width / 2) && (-height / 2 < y <= height / 2)) {
//console.log('POINT', x, y);
// console.log([x,y]);
//myArray.push([x, y]);
//$('#result').append('[' + x + ', ' + y + '], ');
}
//[0, -1] [-1, -2] [-1, 1] [2, 1]
// if (/* x === y || (x < 0 && x === -y) || (x > 0 && x === 1 - y) ||*/ (x > 0 && x === 1 + y) || (x < 0 && -x === y) || (y < 0 && y === x - 1)) {
//if (x === y || (x < 0 && x === -y) || (x > 0 && x === 1 - y)) { //3,3 5,5 ...
// if (x === y || (y <= 0 && y === x - 1)) { // 2,2
// if ( (x < 0 && x === -y) || (y <= 0 && y === x - 1) || (x >= 0 &&...