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.
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
javascript
|
show 3 more comments
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.
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
javascript
Whats the question?
– Liam
Jan 2 at 13:23
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26
|
show 3 more comments
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.
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
javascript
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.
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
javascript
javascript
asked Jan 2 at 13:22
HarryHarry
17817
17817
Whats the question?
– Liam
Jan 2 at 13:23
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26
|
show 3 more comments
Whats the question?
– Liam
Jan 2 at 13:23
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26
Whats the question?
– Liam
Jan 2 at 13:23
Whats the question?
– Liam
Jan 2 at 13:23
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26
|
show 3 more comments
1 Answer
1
active
oldest
votes
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);
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007153%2fhow-to-generate-grid-x-y-coordinates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
add a comment |
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);
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
add a comment |
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);
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);
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);
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);
edited Jan 2 at 14:03
answered Jan 2 at 13:49


Oen44Oen44
1,8191919
1,8191919
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
add a comment |
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
That was helpful but what i need is a bit different here i made the question more clear stackoverflow.com/q/54007719/5008598
– Harry
Jan 2 at 14:04
1
1
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
This is the same question with the same problem. My code can solve your problem, just change values.
– Oen44
Jan 2 at 14:06
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
Sorry I deleted the other question, and this is what i needed thank you.
– Harry
Jan 2 at 15:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54007153%2fhow-to-generate-grid-x-y-coordinates%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Whats the question?
– Liam
Jan 2 at 13:23
Why are you rounding, 0.2 + 0.4 is never going to overflow into multiple decimal places.
– Liam
Jan 2 at 13:24
Are you using canvas to generate that grid or HTML elements?
– Oen44
Jan 2 at 13:24
I just need to generate the coordinates
– Harry
Jan 2 at 13:25
Just asking for the way to show the results.
– Oen44
Jan 2 at 13:26