Skip to content

Commit 2485486

Browse files
committed
add maxX/maxY defaults to add for simpler point indexing, close #42
1 parent 1b0b90b commit 2485486

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ other types may be faster in certain cases (e.g. `Int32Array` when your data is
7474
- `ArrayBufferType`: the array buffer type used to store data (`ArrayBuffer` by default);
7575
you may prefer `SharedArrayBuffer` if you want to share the index between threads (multiple `Worker`, `SharedWorker` or `ServiceWorker`).
7676

77-
#### `index.add(minX, minY, maxX, maxY)`
77+
#### `index.add(minX, minY[, maxX, maxY])`
7878

7979
Adds a given rectangle to the index. Returns a zero-based, incremental number that represents the newly added rectangle.
80+
If not provided, `maxX` and `maxY` default to `minX` and `minY` (essentially adding a point).
8081

8182
#### `index.finish()`
8283

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export default class Flatbush {
119119
* @param {number} maxY
120120
* @returns {number} A zero-based, incremental number that represents the newly added rectangle.
121121
*/
122-
add(minX, minY, maxX, maxY) {
122+
add(minX, minY, maxX = minX, maxY = minY) {
123123
const index = this._pos >> 2;
124124
const boxes = this._boxes;
125125
this._indices[index] = index;

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ test('reconstructs an index from a Uint8Array', () => {
146146
assert.notEqual(index.byteOffset, index2.byteOffset);
147147
});
148148

149+
test('defaults to adding a point when not providing maxX/maxY', () => {
150+
const index = new Flatbush(1);
151+
index.add(10, 10);
152+
index.finish();
153+
assert.deepEqual(index.search(0, 0, 20, 20), [0]);
154+
});
155+
149156
test('throws an error if added less items than the index size', () => {
150157
assert.throws(() => {
151158
const index = new Flatbush(data.length / 4);

0 commit comments

Comments
 (0)