2017-11-23 3 views
0

これについて数学の周りに頭を浮かべようとしています。ブロックが中心から遠く離れていることを知りたいグリッドがあります。行列のグリッド(中心が0の場合)

私は現在、以下れる機能を持っている:私は出力にグリッドに何をしたいです

let grid = []; 

function buildGrid(c, r) { 
    for(let i = 1; i <= r; i++) { 
     for(let j = 1; j <= c; j++) { 
     } 
    } 
} 
buildGrid(5, 3); 

は次のとおりです。

let grid = [{x: -2, y: -1}, 
      {x: -1, y: -1}, 
      {x: 0, y: -1}, 
      {x: 1, y: -1}, 
      {x: 2, y: -1}, 
      {x: -2, y: 0}, 
      {x: -1, y: 0}, 
      {x: 0, y: 0}, 
      {x: 1, y: 0}, 
      {x: 2, y: 0}, 
      {x: -2, y: 1}, 
      {x: -1, y: 1}, 
      {x: 0, y: 1}, 
      {x: 1, y: 1}, 
      {x: 2, y: 1}]; 

それとも

-2, -1 | -1, -1 | 0, -1 | 1, -1 | 2, -1 
-------|--------|-------|-------|------- 
-2, 0 | -1, 0 | 0, 0 | 1, 0 | 2, 0 
-------|--------|-------|-------|------- 
-2, 1 | -1, 1 | 0, 1 | 1, 1 | 2, 1 

すべてのヘルプ大変感謝します。

答えて

1

buildGrid(5, 3)またはbuildGrid(7, 5)またはbuildGrid(5, 3)のような肯定的な答えは、他の条件が追加されていないことを意味します。私はあなた自身でこれを行うことができると仮定します。あなたが取得し、サンプルデータですべての条件を共有する場合。

var c_less = (c-1)/2;

function buildGrid(c, r) { 
 
    var c_less = (c-1)/2;// You need to modify here to add all condition 
 
    var r_less = (r-1)/2;// You need to modify here to add all condition 
 
    //console.log(c_less); 
 
    var newArr = []; 
 
    for(let j = -r_less; j <= r_less; j++) { 
 
    var str = []; 
 
    for(let i = -c_less; i <= c_less; i++) { 
 
     str.push({x:i,y:j}); 
 
    } 
 
    newArr.push(str); 
 
    } 
 
    document.querySelector("#output").innerHTML = JSON.stringify(newArr); 
 
    console.log(newArr); 
 
} 
 
buildGrid(5, 3);
<div id="output"></div>

は//あなたはこれを解決する

+0

絶対に天才の方法の他のすべての条件を追加するために、ここで変更する必要があります。ありがとう! – Tom

関連する問題