Source: DistanceMap.js

/**
 * manages the underlying distance map
 * @param {number} width - width of the map
 * @param {number} height - height of the map
 * @param {number} distance_map_divisor - resolution of velocity map = 1/distance_map_divisor
 * @constructor
 */
var DistanceMap = function (width, height, distance_map_divisor) {

	var parent = this;

    this.widthOfMap = width;
    this.heightOfMap = height;
    this.DISTANCE_MAP_DIVISOR = distance_map_divisor;

    //create 2 dimensional array for easier accessing.
    var mapData = new Array(height);

    for (var i = 0; i < width; i++) {
        mapData[i] = new Array(height);
    }

    /**
     * inits the Distancemap
     */
    this.initDistanceMap = function () {
		for (var i = 0; i < parent.heightOfMap; i++){
			for (var j = 0; j < parent.widthOfMap; j++){
				//0 means not occupied
			    mapData[j][i] = new DistMapEntry(j, i, 0);
			}
		}
    }

    /**
     *
     * @param {number} x - x coordinate
     * @param {number} y - y coordinate
     * @returns {DistMapEntry} returns DistanceMapEntry or null if out of map
     */
    this.getDistanceMapEntry = function (x, y) {
        if(x < 0 || y < 0 || x > parent.widthOfMap-1 || y > parent.heightOfMap-1)
            {
                //console.log("WARNING: getDistanceMapEntry: Out of distanceMap. Return null for point x: " + x + " y " + y);
                return null;
            }
        else {
            return mapData[x][y];
        }
    }

    /**
     * set a map point to 1 (occupied) or 0 (free)
     * @param {number} x - x coordinate
     * @param {number} y - y coordinate
     * @param {number} value - 1 (occupied) or 0 (free)
     */
    this.setDistanceMapValue = function (x, y, value) {

        if(x < 0 || y < 0 || x > parent.widthOfMap-1 || y > parent.heightOfMap-1)
            {
                console.log("WARNING: setDistanceMapValue: Out of distanceMap. Nothing set at x: " + x + " y: " + y);
            }
        else {
            mapData[x][y].v = value;
        }
    }

    /**
     *
     * @returns {number} the distancemap divisor
     */
    this.getDistanceMapDivisor = function()
    {
        return parent.DISTANCE_MAP_DIVISOR;
    }

    /**
     * reinits the distancemap
     */
    this.resetDistanceMap = function()
    {
        parent.initDistanceMap();
    }
}