a | var mySpiral =function(a,b,c,d){
var solution = [];
var lengthOfSegment = 0;
var sens = 1;
var add=-1;
var spiralGrid=function( Width,Height,Row,Col,currentPositionOnSegment){
var index = Height*(Row-1)+Col;
solution.push(index);
if (solution.length==Width*Height) return;
if(lengthOfSegment==currentPositionOnSegment && sens==-1){
sens=1;
currentPositionOnSegment=0;
Col+=add;
add*=-1;
++lengthOfSegment;
} else if (lengthOfSegment!=currentPositionOnSegment && sens==-1){
++currentPositionOnSegment;
Col+=add;
}else if(lengthOfSegment==currentPositionOnSegment && sens==1){
sens=-1;
currentPositionOnSegment=0;
Row+=add;
}else if(lengthOfSegment!=currentPositionOnSegment && sens==1){
++currentPositionOnSegment;
Row+=add;
}
spiralGrid(Width,Height,Row,Col,currentPositionOnSegment);
}
spiralGrid(a,b,c,d,0);
return solution;
}
var first = mySpiral(5,5,3,3);
| ready |
b | var arraySpiralFromGridOfWidth = function(width, height,line,column)
{
var returnArray = [];
var currentDirectionIndex = 0;
var currentInt;
var nextPivotal = 3;
var numOfPivotal = 1;
line++;
for(var i=1;i<=width*height; i++){
if(nextPivotal == i){
numOfPivotal++;
nextPivotal += Math.ceil(numOfPivotal/2);
currentDirectionIndex++;
}
switch (currentDirectionIndex%4) {
case 0:
line--;
break;
case 1:
column--;
break;
case 2:
line++;
break;
case 3:
column++;
break;
}
currentInt = (line-1)*width+column;
if(currentInt<=0||line>height||column>width||!column||!line){
i--;
} else {
returnArray.push(currentInt);
}
}
return returnArray;
}
arraySpiralFromGridOfWidth(5,5,3,3)
| ready |