How to use kdbush - 8 common examples

To help you get started, we’ve selected a few kdbush examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mattdesl / canvas-sketch / examples / canvas-kdbush.js View on Github external
function makeShapes (props) {
    Random.setSeed(config.seed);
    const count = config.count;
    points = Array.from(new Array(count)).map(() => {
      const center = [ width / 2, height / 2 ];
      const radius = width * config.radius;
      return vec2.add([], center, Random.insideCircle(radius));
    });

    const index = kdbush(points);

    lines.length = 0;
    edges.length = 0;
    const ignorePoints = [];
    points.forEach(point => {
      // skip some targets entirely
      if (Random.chance(config.skipChance)) return;

      const stepCount = config.stepCount;
      let curPoint = point;
      let steps = [ curPoint ];
      for (let i = 0; i < stepCount; i++) {
        const previous = steps.length > 1 ? steps[i - 1] : null;
        const next = walk(index, curPoint, previous, ignorePoints);
        if (!next) break;
        curPoint = next;
github mattdesl / canvas-sketch / examples / experimental / canvas-constellations.js View on Github external
function generate () {
    Random.setSeed(config.seed);
    stars = Array.from(new Array(config.starCount)).map(() => {
      return {
        position: vec2.add([], Random.insideCircle(config.mapSize), [ width / 2, height / 2 ]),
        walked: false,
        radius: Math.abs(Random.gaussian(config.starSizeMean, config.starSizeDeviation))
      };
    });

    // spatial index of all stars
    index = kdbush(stars.map(p => p.position));

    const maxConstellations = Math.floor(stars.length * config.nConstellations);
    const targets = Random.shuffle(stars).slice(0, maxConstellations);

    // for each point, walk to others around it
    constellations = targets.map(initialStar => {
      const stepCount = config.stepCount;
      let maxSteps = stepCount + Math.trunc(Random.gaussian(0, 3));

      let currentStar = initialStar;
      const steps = [ currentStar ];

      const minDegree = 0;
      const maxDegree = 40;
      const minSearchDist = config.minSearchDist;
      const constellationSearchRadius = Math.max(0, config.searchRadius + Random.gaussian(0, 0.1));
github jasonwebb / 2d-space-colonization-experiments / core / Network.js View on Github external
buildSpatialIndices() {
    this.nodesIndex = new KDBush(this.nodes, p => p.position.x, p => p.position.y);
  }
github flekschas / regl-scatterplot / src / index.js View on Github external
const setPoints = newPoints => {
    isInit = false;

    numPoints = newPoints.length;

    stateTex = createStateTexture(newPoints);
    normalPointsIndexBuffer({
      usage: 'static',
      type: 'float',
      data: createPointIndex(numPoints)
    });

    searchIndex = new KDBush(newPoints, p => p[0], p => p[1], 16);

    isInit = true;
  };
github sakitam-fdd / HMap / src / layer / GLLayer.js View on Github external
addFeatures (features) {
    this.features = this.features.concat(features);
    this.originData = kdbush(this.features, (p) => p.getGeometry().getCoordinates()[0], (p) => p.getGeometry().getCoordinates()[1]);
  }
github bcrumbs / booben / app / src / containers / Canvas / content / containers / CanvasContent.js View on Github external
async _getPossibleSnapPoints(x, y) {
    const snapPoints = await this._getAllSnapPoints();
    let possibleSnapPoints;

    if (snapPoints.length < 2) {
      possibleSnapPoints = snapPoints;
    } else {
      if (this._snapPointsIndex === null) {
        this._snapPointsIndex = kdbush(
          snapPoints,
          p => p.x,
          p => p.y,
          64,
          Int32Array,
        );
      }

      const pointsWithinSnapDistance = this._snapPointsIndex
        .within(x, y, SNAP_DISTANCE)
        .map(id => snapPoints[id]);

      if (pointsWithinSnapDistance.length > 0) {
        const closestPoint = _minBy(
          pointsWithinSnapDistance,
          point => distance(x, y, point.x, point.y),
github sakitam-fdd / HMap / src / layer / DozensLayer.js View on Github external
addFeatures (features) {
    this.features = this.features.concat(features);
    this.originData = kdbush(this.features, (p) => p.getGeometry().getCoordinates()[0], (p) => p.getGeometry().getCoordinates()[1]);
  }
github anvaka / playground / nb-layout / src / nbLayout.js View on Github external
function forceMove() {
    var before = getGraphBBox();
    const points = new KDBush(nodeArr, p => p.x, p => p.y);
    nodeArr.forEach((pos, idx) => {
      var sx = 0, sy = 0, count = 0;
      var neighbors = points.within(pos.x, pos.y, edgeLength/2);
      neighbors.forEach(otherIndex => {
        if (otherIndex === idx) return;

        var other = nodeArr[otherIndex];
        var dx = pos.x - other.x;
        var dy = pos.y - other.y;
        var l = Math.sqrt(dx * dx + dy * dy);
        if (l < 1e-10) l = 1;

        var tR = 1;
        pos.incX += pos.x + k4 * (edgeLength - l) * dx/l * tR;
        pos.incY += pos.y + k4 * (edgeLength - l) * dy/l * tR;
        pos.incLength += 1;

kdbush

A very fast static 2D index for points based on kd-tree.

ISC
Latest version published 10 months ago

Package Health Score

71 / 100
Full package analysis

Popular kdbush functions