使用 Javascript 的嵌套数组循环在所有象限中围绕 (0,0) 生成和排序笛卡尔坐标

Generate and sort cartesian coordinates around (0,0) in all quadrants using Javascript's nested array loops

我有一个变量 obj,它的元素计数需要笛卡尔坐标。

所以我想生成如下矩阵。

obj = 9,obj = 3 的平方根,3x3 矩阵
(-1,1) (0,1) (1,1)
(-1,0) (0,0) (1,0)
(-1,-1) (0,-1) (1,-1)
obj = 25,obj = 5 的平方根,5x5 矩阵
(-2,2) (-1,2) (0,2) (1,2) (2,2)
(-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)
(-2,-2) (-1,-2) (0,-2) (1,-2) (2,-2)
obj = 49, obj 的平方根 = 7, 7x7 矩阵
(-3,3) (-2,3) (-1,3) (0,3) (1,3) (2,3) (3,3)
(-3,2) (-2,2) (-1,2) (0,2) (1,2) (2,2) (3,2)
(-3,1) (-2,1) (-1,1) (0,1) (1,1) (2,1) (3,1)
(-3,0) (-2,0) (-1,0) (0,0) (1,0) (2,0) (3,0)
(-3,-1) (-2,-1) (-1,-1) (0,-1) (1,-1) (2,-1) (3,-1)
(-3,-2) (-2,-2) (-1,-2) (0,-2) (1,-2) (2,-2) (3,-2)
(-3,-3) (-2,-3) (-1,-3) (0,-3) (1,-3) (2,-3) (3,-3)

我所做的是硬编码第一组,即当 obj 值为 9 时在循环内创建,并将它们推送到名为 coordinates.

的列表中

然后我所做的就是通过传递 Math.sqrt(obj).

来调用循环

问题:

注:

我想知道围绕 (0, 0) 创建笛卡尔坐标的不同方法。显然,我在构建逻辑方面所做的所有努力最终都以缺少元素或重复元素告终。当 obj 值增加时,很难实际检查所有坐标是否正确。

我想创建一个任意值的笛卡尔坐标矩阵。目前我坚持使用奇数的平方(当数字小于或大于奇数的平方时,我计划用 0 轴代替)。

接近 ideas/concepts 进行测试:

由于我是 图形编程 的初学者,我想知道更好的方法来完成这项工作。这里还有一些我刚刚想出的方法。我不确定这是否有效,但我会添加更新。

  1. I could maybe create a cross for just the 0's (x,y) axis. And then try to create the rest of the elements by subtracting or adding to each coordinate in the axis.

    As there are 4 quadrants, I could create 4 individual loops that creates just that particular quadrant's missing coordinates.

    (0,1)
    (-1,0) (0,0) (1,0)
    (0,-1)
  2. Another approach could be like to sort the coordinates and then check to see the distance between 2 adjacent coordinates if it is greater than 1 create a new element, else continue checking.

当前代码:

My demo code at JSFiddle

const speak = 'these are the COORDINATES you are looking for!'
// 9, 25, 49, 81, 121 => substitutable values for variable 'obj'
const obj = 49 // loop using this variable
const coordinates = []

// hardcodes
const start = [0,0]
const points = []
/* points.push(start) */


/**
 * FIX!.
 *
 * needs to also create coordinates from initial coordinate substracted
 * by more than 1, currently it gets the previous element by substracting 1,
 * we need to get previous elements of the previous elements based on number
 * of elements.
 */

// creating array from coordinates in all quadrants
function demo (n) {
    // pushing initial coordinates
    for (let i = 1; i <= Math.sqrt(n); i++) {
    
    coordinates.push([-i, i], [i-1, i], [i, i], [-i, i-1], [i-1, i-1], [i, i-1], [-i, -i], [i-1, -i],  [i, -i])
    for (let j = 3; j < Math.sqrt(n); j++) {
        coordinates.push([-i, i-j], [i-j, i-j], [i, i-j], [i-j, -i])
    }
  }
  // pushing missing coordinates
/*   for (let i = 1; i <= Math.sqrt(n); i++) {
    coordinates.push([i-2, i], [-i, i-2], [i-2, i-2], [i, i-2])
  } */

for (let i = 0; i < obj; i++) {
      points.push(coordinates[i])
  }
}

demo(obj)
// sorting multidimensional array
points.sort(function (a, b) {
  return a[1] - b[1]
})

/* // print array as row and column of coordinates
for (let x = 0; x < Math.sqrt(obj); x++) {
  let el = []
  for (let i = 0; i < Math.sqrt(obj); i++){
    el.push(points[i + Math.sqrt(obj) * x])
  }
  console.log(el)
*/
}

如果我没理解错的话,你希望坐标的顺序是左上角在前,右下角在后,对吧?

你可以这样试试

let
  size = 81, //ie a 7x7 grid,
  rc = Math.floor(Math.sqrt(size))  //number of rows/columns
  max = Math.floor(rc / 2),  //maximum x and y coordinates
  min = -1 * max;  //minimim x and y coordinates
  coords = [] //the array of coordinates
  
//as the positive y coordinates should be first, iterate from max down to min
for (let y = max; y >= min; y--) 
  //for each row, iterate the x cooridinates from min up to max
  for (let x = min; x <= max; x++)
    coords.push([x,y]);
    
    
    
for (let i = 0; i < rc; i++) {
  let row = coords.slice(i*rc, (i+1)*rc);  //get one full row of coordinates
  console.log(row.map(x => formatCoordinate(x)).join(""));  //and display it
}


function formatCoordinate(x) {
  return "|" + `${x[0]}`.padStart(3, " ") + "/" + `${x[1]}`.padStart(3, " ") + "|"  
}

另一种方法是,将您的坐标以任意顺序放入数组中,然后对值进行排序。但是你必须按 xy 坐标排序,

let
  size = 81, //ie a 7x7 grid,
  rc = Math.floor(Math.sqrt(size))  //number of rows/columns
  max = Math.floor(rc / 2),  //maximum x and y coordinates
  min = -1 * max;  //minimim x and y coordinates
  coords = [] //the array of coordinates

//coords will be [[-3, -3], [-3, -2], [-3, -1] ..., [3, 3]]
for (let i = min; i <= max; i++)
  for (let j = min; j <= max; j++)
    coords.push([i,j]);
    


//sort coords to be [[-3, 3], [-2, 3], [-1, 3], ... [3, -3]]
coords.sort((a, b) => {
  if (a[1] != b[1])  //if y coordinates are different
    return b[1] - a[1];  //higher y coordinates come first
   
  return a[0] - b[0];  //lower x coordinates come firs
})

for (let i = 0; i < rc; i++) {
  let row = coords.slice(i*rc, (i+1)*rc);  //get one full row of coordinates
  console.log(row.map(x => formatCoordinate(x)).join(""));  //and display it
}

function formatCoordinate(x) {
  return "|" + `${x[0]}`.padStart(3, " ") + "/" + `${x[1]}`.padStart(3, " ") + "|"  
}

这两种方法都假设 size 是奇数的平方,但是您当然可以随意调整它们,即原则上您只需要设置 minmax 到你想要的任何值,这两种方法都会创建一个坐标的正方形 [[min/max] ... [max/min]].