Skip to content

Commit

Permalink
feat(distance): add INeighborhood.includesPosition()
Browse files Browse the repository at this point in the history
- update all impls: Nearest/KNearest/Radial
  • Loading branch information
postspectacular committed Jun 9, 2023
1 parent 065e40c commit 2018e41
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/distance/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export interface INeighborhood<P, T> extends IReset {
*/
includesDistance(d: number, eucledian?: boolean): boolean;

/**
* Computes distance metric between `pos` and this neighborhood's target
* pos. Returns true if result is <= current radius.
*
* @param pos
*/
includesPosition(pos: P): boolean;

/**
* Computes distance metric between `pos` and this neighborhood's target
* pos. If result distance is <= current radius, adds `val` to neighborhood
Expand Down
4 changes: 4 additions & 0 deletions packages/distance/src/knearest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export class KNearest<D, T>
return (eucledian ? this.dist.to(d) : d) <= this._currR;
}

includesPosition(pos: D) {
return this.dist.metric(this.target, pos) < this._currR;
}

consider(pos: D, val: T) {
const d = this.dist.metric(this.target, pos);
if (d <= this._currR) {
Expand Down
4 changes: 4 additions & 0 deletions packages/distance/src/nearest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class Nearest<D, T>
return (eucledian ? this.dist.to(d) : d) <= this._currR;
}

includesPosition(pos: D) {
return this.dist.metric(this.target, pos) < this._currR;
}

consider(pos: D, val: T) {
const d = this.dist.metric(this.target, pos);
if (d <= this._currR) {
Expand Down
4 changes: 4 additions & 0 deletions packages/distance/src/radial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class Radial<D, T>
return (eucledian ? this.dist.to(d) : d) <= this._r;
}

includesPosition(pos: D) {
return this.dist.metric(this.target, pos) < this._r;
}

consider(pos: D, val: T) {
const d = this.dist.metric(this.target, pos);
if (d <= this._r) {
Expand Down

0 comments on commit 2018e41

Please sign in to comment.