Representation of 2D maps with float data
// From http://www.redblobgames.com/ // Copyright 2015 Red Blob Games <redblobgames@gmail.com> // License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html> /** Representation of 2D maps with float data * * Fields: * w:int, h:int, size:int -- read-only * data:array[size]:float -- read/write * Methods: * id(x:int, y:int):int -- tells you where in data[] (x,y) goes * x(id:int):int -- tells you the x for a given id * y(id:int):int -- tells you the y for a given id * get(x:int, y:int):float -- getter * set(x:int, y:int, value:float) -- setter * * It is fine to directly read/write from the data array. */ export default class Map2D { constructor(w, h) { this.w = w; this.h = h; this.size = w * h; this.data = new Float32Array(this.size); } id(x, y) { return x + this.w * y; } x(id) { return id % this.w; } y(id) { return (id / this.w) | 0; } get(x, y) { return this.data[this.id(x, y)]; } set(x, y, value) { this.data[this.id(x, y)] = value; } }