diff --git a/README.md b/README.md index c735b79..a80f8b6 100644 --- a/README.md +++ b/README.md @@ -74,9 +74,10 @@ other types may be faster in certain cases (e.g. `Int32Array` when your data is - `ArrayBufferType`: the array buffer type used to store data (`ArrayBuffer` by default); you may prefer `SharedArrayBuffer` if you want to share the index between threads (multiple `Worker`, `SharedWorker` or `ServiceWorker`). -#### `index.add(minX, minY, maxX, maxY)` +#### `index.add(minX, minY[, maxX, maxY])` Adds a given rectangle to the index. Returns a zero-based, incremental number that represents the newly added rectangle. +If not provided, `maxX` and `maxY` default to `minX` and `minY` (essentially adding a point). #### `index.finish()` diff --git a/index.js b/index.js index 8390a8a..ac6394f 100644 --- a/index.js +++ b/index.js @@ -119,7 +119,7 @@ export default class Flatbush { * @param {number} maxY * @returns {number} A zero-based, incremental number that represents the newly added rectangle. */ - add(minX, minY, maxX, maxY) { + add(minX, minY, maxX = minX, maxY = minY) { const index = this._pos >> 2; const boxes = this._boxes; this._indices[index] = index; diff --git a/test.js b/test.js index 4e19250..cbc478e 100644 --- a/test.js +++ b/test.js @@ -146,6 +146,13 @@ test('reconstructs an index from a Uint8Array', () => { assert.notEqual(index.byteOffset, index2.byteOffset); }); +test('defaults to adding a point when not providing maxX/maxY', () => { + const index = new Flatbush(1); + index.add(10, 10); + index.finish(); + assert.deepEqual(index.search(0, 0, 20, 20), [0]); +}); + test('throws an error if added less items than the index size', () => { assert.throws(() => { const index = new Flatbush(data.length / 4);