简体   繁体   English

如何在以下网格中找到每个单元格的中心坐标?

[英]How to find the center coords of each cell in the following grid?

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.网格的最中间是 0,0 坐标。 The width is 250 and height is 250.宽度为 250,高度为 250。

Note: the y axis is inverted and needs to be like that.注意: y轴是倒置的,需要这样。

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.在这种情况下,我只是根据我的需要交换yz值。

[
    {
        "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
    }
]

在此处输入图片说明

It's a little unclear what you mean since if you already know your starting position, the cell sizes, and the map sizes, you can figure out all the cells' positions by incrementing/decrementing by cell size in both axes.有点不清楚你的意思,因为如果你已经知道你的起始位置、单元格大小和地图大小,你可以通过在两个轴上增加/减少单元格大小来计算所有单元格的位置。

Since no code has been shared and no further details given, I'll assume that's all you want so here's how you can set it up to get all the cells' center coordinates.由于没有共享代码,也没有给出更多细节,我假设这就是你想要的,所以这里是你如何设置它以获得所有单元格的中心坐标。

const cellSize = 50;
const mapWidth = 250;
const mapHeight = 250;

let rows = mapWidth / cellSize;
let cols = mapHeight / cellSize;

/* Let's define 'extents' - as in, how many squares left/right 
 or top/bottom of a 0,y or x,0 square. This will be useful in a bit*/

let yExtent = Math.floor(rows / 2); // in the example grid, this would just be 2
let xExtent = Math.floor(cols / 2);

We have your cellSize and some other data programmatically based on your defined constraints (so you can adjust as needed).我们根据您定义的约束以编程方式获得您的 cellSize 和其他一些数据(因此您可以根据需要进行调整)。

I'm assuming you're drawing this grid in HTML5 canvas, hence why your default positioning of the grid starts at the top left, and so every cell's x/y starts at the top left of the cell.我假设您在 HTML5 画布中绘制此网格,因此为什么网格的默认位置从左上角开始,因此每个单元格的 x/y 都从单元格的左上角开始。

So we're defining a cell's center coordinate with a function as:因此,我们使用函数定义单元格的中心坐标为:

getCellCenter(cellX, cellY) {
return [cellX + cellSize/2, cellY + cellSize/2] // [center X, center Y]
}

where cellX,cellY is the top-left point (or the starting draw position) of the grid cell.其中 cellX,cellY 是网格单元的左上点(或起始绘制位置)。

Now all you'd do is loop through your whole grid with the assumption that 0,0 is at the center.现在你要做的就是循环遍历整个网格,假设 0,0 位于中心。 I do this in a lot of my HTML5 canvas grid-based games over the last 6-7 years and when I have to render things per-tile in such a grid, I follow this loop:在过去的 6 到 7 年里,我在许多基于 HTML5 画布网格的游戏中都这样做了,当我必须在这样的网格中按图块渲染事物时,我遵循以下循环:

(Note: You can do this much more efficiently or linearly, but since some context is missing, this will likely be the simplest to understand) (注意:您可以更有效或线性地执行此操作,但由于缺少某些上下文,这可能是最容易理解的)

// Loop through rows from -2 row to +2 row
for (let y = -yExtent; y <= yExtent; y++) {
  for (let x = -xExtent; x <= xExtent; x++) {
    cellCenter = getCellCenter(x * cellSize, y * cellSize)
  }
}

Does this make sense?这有意义吗?

So for the cell that's at -2,-2 (top left cell in grid coordinates), its actual pixel coordinates would be因此,对于位于 -2,-2(网格坐标中左上角的单元格)的单元格,其实际像素坐标将为

Now, if your whole map grid is positioned specifically somewhere in your canvas, then what you want to do is apply an offset X and offset Y to all your calculations现在,如果您的整个地图网格专门位于画布中的某个位置,那么您要做的就是将偏移量 X 和偏移量 Y 应用于所有计算

eg.例如。

cellCenter = getCellCenter(x * cellSize - offsetX, y * cellSize - offsetY)

(Or you can add the offsets, depending on how you want to position it). (或者您可以添加偏移量,具体取决于您要如何定位它)。

This should be generic enough that if the true pixel coordinates of the middle of the grid is 0,0, and not just grid coordinates (which is the starting assumption of the above algorithm, so as to not give you a full answer but a way to tune it to get what you want), you can adjust as needed.这应该足够通用,如果网格中间的真实像素坐标是 0,0,而不仅仅是网格坐标(这是上述算法的开始假设,以免给你一个完整的答案,而是一种方法调整它以获得您想要的),您可以根据需要进行调整。

I don't get the swapping of the z and y but here's a way to get the array you want.我没有得到 z 和 y 的交换,但这是一种获取所需数组的方法。 Keep in mind I didn't know how you grid was being created so I used a ES6 class and captured each cells center point.请记住,我不知道您的网格是如何创建的,因此我使用了 ES6 类并捕获了每个单元格的中心点。 Then I just pushed them to an array while swapping the y and z.然后我只是在交换 y 和 z 的同时将它们推送到一个数组。

 let canvas = document.getElementById("canvas"); let ctx = canvas.getContext("2d"); canvas.width = 250; canvas.height = 250; let cellSize = 50; let grid = []; let centerPoints = []; class Cell { constructor(x, y) { this.x = x; this.y = y; this.z = 0; this.width = cellSize; this.height = cellSize; this.c = "black"; this.center = { x: this.x + this.width / 2 - canvas.width/2, y: this.y + this.height / 2 - canvas.height/2}; } draw() { ctx.strokeStyle = this.c; ctx.strokeRect(this.x, this.y, this.width, this.height); } createArray() { centerPoints.push({'x': this.center.x, 'y': this.z, 'z': this.center.y}) } } function createGrid() { for (let y = 0; y < canvas.height; y += cellSize) { for (let x = 0; x < canvas.width; x += cellSize) { grid.push(new Cell(x, y)); } } } createGrid(); function drawGrid() { for (let i = 0; i < grid.length; i++) { grid[i].draw(); grid[i].createArray(); } } drawGrid(); console.log(centerPoints)
 body { margin: 0; padding: 0; box-sizing: border-box; }
 <canvas id="canvas"></canvas>

WITHOUT CANVAS没有帆布

 gridWidth = 250; gridHeight = 250; let cellSize = 50; let grid = []; let centerPoints = []; class Cell { constructor(x, y) { this.x = x; this.y = y; this.z = 0; this.width = cellSize; this.height = cellSize; this.c = "black"; this.center = { x: this.x + this.width / 2 - gridWidth/2, y: this.y + this.height / 2 - gridHeight/2}; } createArray() { centerPoints.push({'x': this.center.x, 'y': this.z, 'z': this.center.y}) } } function createGrid() { for (let y = 0; y < gridHeight; y += cellSize) { for (let x = 0; x < gridWidth; x += cellSize) { grid.push(new Cell(x, y)); } } } createGrid(); function drawGrid() { for (let i = 0; i < grid.length; i++) { grid[i].createArray(); } } drawGrid(); console.log(centerPoints)

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

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