Показать сообщение отдельно
  #4 (permalink)  
Старый 27.10.2023, 20:22
Аватар для ruslan_mart
Профессор
Отправить личное сообщение для ruslan_mart Посмотреть профиль Найти все сообщения от ruslan_mart
 
Регистрация: 30.04.2012
Сообщений: 3,018

Наверное как-то так

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.init();
  }
  
  init() {
    const { x, y, z } = this;
    
    this.buffer = Array.from(Array(z), (_, i) => {
      return Array.from(Array(y), (_, j) => {
        return Array.from(Array(x), (_, k) => {
          return (j * x + k + 1) + (((x * y * z) / z) * i);
        });
      });
    });
  }
  
  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);
Ответить с цитированием