5.0.0 • Published 3 years ago

@ignatisd/nearest-neighbor v5.0.0

Weekly downloads
251
License
MIT
Repository
github
Last release
3 years ago

Nearest Neighbor

view on npm license Tests codecov semantic-release Commitizen friendly

NearestNeighbor

Brute forcing the nearest neighbor problem.

Author: Ignatios Drakoulas
License: MIT

NearestNeighbor~Point

Kind: inner class of NearestNeighbor

new Point(x, y)

Represents a point in two dimensions.

ParamTypeDescription
xNumberabscissa
yNumberordinate

NearestNeighbor~sortBruteForce(neighbors, distanceFn) ⇒ Array.<Point> | Array.<any>

Sorts all the neighbors by their respective distance starting from the first

Kind: inner method of NearestNeighbor

ParamTypeDescription
neighborsArray.<Point> | Array.<any>The array of Point elements
distanceFnfunctionOptional, distSquared will be used if none provided

Example

var nn = require("NearestNeighbor.js");
var neighbors = [
      {x: 5, y: 5},
      {x: 3, y: 3},
      {x: 4, y: 4},
      {x: 1, y: 1},
      {x: 2, y: 2},
      //other points
 ];
var ordered_neighbors = nn.sortBruteForce(neighbors);
// ordered_neighbors now contain the neighbors, in the order they ought to be visited.

NearestNeighbor~getNearestIndex(home, neighbors, distanceFn) ⇒ number

Returns the nearest, relative to the provided home point, neighbor's index from the array of neighbors

Kind: inner method of NearestNeighbor

ParamTypeDescription
homePoint | anyThe reference Point
neighborsArray.<Point> | Array.<any>The array of Point elements
distanceFnfunctionOptional, distSquared will be used if none provided

Example

var nn = require("NearestNeighbor.js");
var home = {x: 6, y: 6};
var neighbors = [
      {x: 4, y: 4},
      {x: 3, y: 3},
      {x: 5, y: 5},
      {x: 1, y: 1},
      {x: 2, y: 2},
      //other points
 ];
var index = nn.getNearestIndex(home, neighbors);
// index will equal 2, because {x: 5, y: 5} is
// the nearest neighbor for {x: 6, y: 6} which is the home point

NearestNeighbor~getNearest(home, neighbors, distanceFn) ⇒ Point | any

Returns the nearest neighbor, relative to the provided home point, from the given array of neighbors

Kind: inner method of NearestNeighbor

ParamTypeDescription
homePoint | anyThe reference Point
neighborsArray.<Point> | Array.<any>The array of Point elements
distanceFnfunctionOptional, distSquared will be used if none provided

Example

var nn = require("NearestNeighbor.js");
var home = {x: 6, y: 6};
var neighbors = [
      {x: 5, y: 5},
      {x: 3, y: 3},
      {x: 4, y: 4},
      {x: 1, y: 1},
      {x: 2, y: 2},
      //other points
 ];
var neighbor = nn.getNearest(home, neighbors);
// neighbor will equal {x: 5, y: 5}, because {x: 5, y: 5} is
// the nearest neighbor for {x: 6, y: 6} which is the home point

Copyright © 2021 Ignatios Drakoulas

5.0.0

3 years ago

4.0.0

3 years ago

3.0.0

3 years ago

3.0.0-next.2

3 years ago

3.0.0-next.1

3 years ago

2.0.0

3 years ago

1.0.0

3 years ago