I need an algorithm to find the center coordinates of each of the cells in the following grid.
The very middle of the grid is 0,0 coordinates. The width is 250 and height is 250.
Note: the y axis is inverted and needs to be like that.
Here is what I tried with some informations found online, but it is not quite it.
const size = 250
const divisions = 5
let step = size / divisions;
let halfSize = size / 2;
let vertices = [];
let arr = [];
for (let i = 0, z = -halfSize + step; i < divisions - 1; i++, z += step ) {
//x axis
vertices.push(-halfSize + (step/2), 0, z - (step/2), halfSize - (step/2), 0, z - (step/2));
//z axis
vertices.push(z - (step/2), 0, -halfSize + (step/2), z - (step/2), 0, halfSize - (step/2));
}
for (let i = 0; i < vertices.length; i += 3) {
const position = {}
position.x = vertices[i];
position.y = vertices[i + 1];
position.z = vertices[i + 2];
arr.push(position)
}
expected coordinates (in no particular order). In this instance I am simply swapping the y and z values for my needs.
[
{
"x": -100,
"y": 0,
"z": -100
},
{
"x": -50,
"y": 0,
"z": -100
},
{
"x": 0,
"y": 0,
"z": -100
},
{
"x": 50,
"y": 0,
"z": -100
},
{
"x": 100,
"y": 0,
"z": -100
},
{
"x": -100,
"y": 0,
"z": -50
},
{
"x": -50,
"y": 0,
"z": -50
},
{
"x": 0,
"y": 0,
"z": -50
},
{
"x": 50,
"y": 0,
"z": -50
},
{
"x": 100,
"y": 0,
"z": -50
},
{
"x": -100,
"y": 0,
"z": 0
},
{
"x": -50,
"y": 0,
"z": 0
},
{
"x": 0,
"y": 0,
"z": 0
},
{
"x": 50,
"y": 0,
"z": 0
},
{
"x": 100,
"y": 0,
"z": 0
},
{
"x": -100,
"y": 0,
"z": 50
},
{
"x": -50,
"y": 0,
"z": 50
},
{
"x": 0,
"y": 0,
"z": 50
},
{
"x": 50,
"y": 0,
"z": 50
},
{
"x": 100,
"y": 0,
"z": 50
},
{
"x": -100,
"y": 0,
"z": 100
},
{
"x": -50,
"y": 0,
"z": 100
},
{
"x": 0,
"y": 0,
"z": 100
},
{
"x": 50,
"y": 0,
"z": 100
},
{
"x": 100,
"y": 0,
"z": 100
}
]
