0

Hello all!

Can someone help with this matrix? it needs to spiral inwards, from bottom right corner, clockwise...

I found this code... and it runs nicely but I need to adapt it in some ways:
- I should be able to provide number of rows and columns
- the grid should start from number 1
- it should start from bottom right corner, clockwise...

I will put the code that works, but not the way I need... can someone at least decipher it for me so I could try to do it??

Thanks

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>

<body>
<script type="text/javascript">

spiralArray = function (edge) {
    var arr = Array(edge),
        x = 0, y = edge,
        total = edge * edge--,
        dx = 1, dy = 0,
        i = 0, j = 0;
    while (y) arr[--y] = [];
    while (i < total) {
        arr[y][x] = i++;
        x += dx; y += dy;
        if (++j == edge) {
            if (dy < 0) {x++; y++; edge -= 2}
            j = dx; dx = -dy; dy = j; j = 0;
       }
    }
    return arr;
}

// T E S T:
arr = spiralArray(edge = 3);
for (y= 0; y < edge; y++) console.log(arr[y].join(" "));

alert(arr.join('\n'))



</script>



</body>
</html>
2
Contributors
1
Reply
15
Views
3 Years
Discussion Span
Last Post by Dani
0

I'm totally confused what on earth you want this to do. When I ran it, it gave me an alert that said:

0,1,2
7,8,3
6,5,4
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.