CorentinTh / quadtree-js

A simple quadtree implementation for javascript and typescript (nodejs or browser).
MIT License
76 stars 6 forks source link

add GENERIC for type Box to quadtree #21

Closed nhhockeyplayer closed 4 years ago

nhhockeyplayer commented 4 years ago

nice work its nice to see a typescript quadtree the d3 one falls short on usability sadly and works easy for brush's only sorry Mike

my code has its own custom Box and IBox interference not pleasurable with name collision and your Box which doesnt even operate properties no offense

Be nice if I could just instantiate the QuadTree and make it Box safe for all and base the generic Box on a seamless interface using a configurable Box type that mandates only left, top, right, bottom interface so we can plug in our own Box types setting aside width/height its all about location to avoid added sums/diffs of width height

maybe I will do this myself, I will keep you posted if I produce a forked patch

export class QuadTree<T extends Point> implements IPoint, Iterator<T> {
...
}

and

public quadTree: QuadTree = new QuadTree<Box>({left: 0, top: 0, right: 800, bottom: 800} as Point)

then

this.quadTree.add (box as Box) this.quadTree.remove (box as box) this.quadTree.addAll (boxes as Box[]) this.quadTree.removeAll (boxes as Box[]) this.quadTree.search (box as Box)

where Box is a box class provided by the user of the library

great 1st stab I think there is potential for this to become THE quadtree for the future

CorentinTh commented 4 years ago

Hi @nhhockeyplayer Thank you for those kind words and for your interest in Quadtree.

First, if you need to prevent type interferences with the imported classes, you can us the as keyword during import, for exemple:

import {Box as QTBox} from 'js-quadtree'

// Your custom box
class Box {
    constructor(
        private top: number,
        private left: number,
        private bottom: number,
        private right: number,
    ) {}
}

const a = new Box(0, 0, 10, 10);
const b = new QTBox(0, 0, 10, 10);

Then, I'm not really sure that I've correctly understand what you ment by wanting to generify the Box in the quadtree class. If the quadtree can't really know the structure of the box, it will have trouble during the division part (see the divide method).

If you point is to get a box defined by top, left, bottom, right (or x1, y1, x2, y2) to work, I can think the following solution: allow the input container to accept a type union between {x, y, w, h} and {t, l, b, r} and then, during the divide method, have a condition to split the space according to the type of the container.

nhhockeyplayer commented 4 years ago

i gave example code

at definition time use provides their own

enjoy