Показать сообщение отдельно
  #3 (permalink)  
Старый 27.10.2023, 19:54
Аспирант
Отправить личное сообщение для firep91613 Посмотреть профиль Найти все сообщения от firep91613
 
Регистрация: 24.10.2023
Сообщений: 58

ruslan_mart,
благодарю за ответ.

function prettyPrint(data) {
  console.log(JSON.stringify(data));
}

class Matrix3D {
  constructor(len) {
    this.x = len.x;
    this.y = len.y;
    this.z = len.z;
    this.buffer = [];
  }

  init() {
    const matrix3D = [];

    for (let i = 0; i < this.z; i++) {
      let z = [];

      for (let j = 0; j < this.y; j++) {
        let y = [];

        for (let k = 0; k < this.x; k++) {
          y[k] = (j * this.x + k + 1) + (((this.x * this.y * this.z) / this.z) * i);
        }

        z.push(y);
      }

      matrix3D.push(z);
    }

    this.buffer = matrix3D;

    prettyPrint(matrix3D);
  }

  set(coords, value) {
    this.buffer[coords.z][coords.y][coords.x] = value;
  }
  
  get(coords) {
    return this.buffer[coords.z][coords.y][coords.x];
  }
}

const matrix = new Matrix3D({x: 2, y: 2, z: 2});
matrix.init();
matrix.set({x: 1, y: 1, z: 1}, 10);

prettyPrint(matrix.get({x: 1, y: 1, z: 1}));
prettyPrint(matrix.buffer);


Как можно сделать лучше нумерацию? Сейчас у меня так:
y[k] = (j * this.x + k + 1) + (((this.x * this.y * this.z) / this.z) * i);

Выглядит как-то громозко...

И как можно сделать без метода init? Ну т.е. new Matrix3D и все. Сразу buffer заполнить.
Ответить с цитированием