简体   繁体   English

如何生成网格 x,y 坐标

[英]How to generate grid x,y coordinates

I'm creating a simple game, in the game I have a 4 x 4 square, what i would like to do is generate coordinate points to fill up the square with a 2.0 spacing.我正在创建一个简单的游戏,在游戏中我有一个 4 x 4 的正方形,我想做的是生成坐标点以填充 2.0 间距的正方形。

I tried to do it but i got stuck and confused here:我试图这样做,但我在这里陷入困境并感到困惑:

var coords = []
for (var i = 0.2; i < 2; i += 0.4) {
  //round to 1dp
  coords.push(Math.round(i * 10) / 10)
}

console.log(coords) 

I need to fill up the square like a Cartesian plane where the values increase by 0.2, until it reaches 1.8 to the left, top, right and bottom of the square我需要像笛卡尔平面一样填充正方形,其中值增加 0.2,直到它在正方形的左侧、顶部、右侧和底部达到 1.8

There you go.你去吧。 I hope that's what you were asking for.我希望这就是你所要求的。

 var coords = [[], []]; const x = 0; const y = 1; /* Generate X coords */ for (var i = -1.8; i < 2; i += 0.4) { let c = Math.round(i * 10) / 10; coords[x].push(c); if(c + 0.2 == 0) coords[x].push(0); } /* Generate Y coords */ for (var i = -1.8; i < 2; i += 0.4) { let c = Math.round(i * 10) / 10; coords[y].push(c); if(c + 0.2 == 0) coords[y].push(0); } console.log(coords);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM