Advertisement
Guest User

Orbit

a guest
Oct 5th, 2016
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function orbitOfMatrix([size,coordinates]){
  2.     [size,coordinates] = [size,coordinates].map(item=> item.split(' ').map(Number));
  3.     let matrix = [];
  4.     //fill matrix with zeroes
  5.     for(let i=0;i<size[0];i++) {
  6.         let rs = [];
  7.         for(let j=0;j<size[1];j++) {
  8.             rs.push(0);
  9.         }
  10.         matrix.push(rs);
  11.     }
  12.     let number = 2;
  13.     let colEnd = coordinates[1];
  14.     let rowEnd = coordinates[0];
  15.     let colStart = coordinates[1];
  16.     let rowStart = coordinates[0];
  17.     matrix[coordinates[0]][coordinates[1]] = 1;
  18.     let counterRowEnd = rowEnd;
  19.     let counterColEnd = colEnd;
  20.     let counterRowStart = rowEnd;
  21.     let counterColStart = colEnd;
  22.     while(true)
  23.     {
  24.         counterRowEnd++;
  25.         counterColEnd++;
  26.         counterRowStart--;
  27.         counterColStart--;
  28.         //check if out of matrix size
  29.         if(counterRowEnd > size[0]-1 && counterColEnd > size[1]-1 && counterRowStart < 0 && counterColStart < 0)break;
  30.         if(rowEnd >= size[0]-1) rowEnd = size[0]-1;
  31.         else rowEnd++;
  32.         if(colEnd >= size[1]-1) colEnd = size[1]-1;
  33.         else colEnd++;
  34.         if(rowStart == 0) rowStart = 0;
  35.         else rowStart--;
  36.         if(colStart == 0) colStart = 0;
  37.         else colStart--;
  38.          for (let i = rowStart; i <= rowEnd; i++) {
  39.             for (let j = colStart; j <= colEnd; j++) {
  40.                 if(matrix[i][j] == 0){matrix[i][j] = number;}
  41.             }
  42.          }
  43.         number++;
  44.     }
  45.     for(let row of matrix){
  46.         console.log(row.join(' '));
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement