{"id":16170414,"url":"https://github.com/corentinth/quadtree-js","last_synced_at":"2025-04-21T11:33:28.869Z","repository":{"id":32654190,"uuid":"138935057","full_name":"CorentinTh/quadtree-js","owner":"CorentinTh","description":"A simple quadtree implementation for javascript and typescript (nodejs or browser).","archived":false,"fork":false,"pushed_at":"2023-01-06T00:05:24.000Z","size":880,"stargazers_count":79,"open_issues_count":16,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-01T12:54:36.244Z","etag":null,"topics":["intersects","javascript","js","node-module","nodejs","quadtree","quadtree-js","range","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CorentinTh.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-27T21:31:37.000Z","updated_at":"2025-03-11T15:13:55.000Z","dependencies_parsed_at":"2023-01-14T21:50:32.560Z","dependency_job_id":null,"html_url":"https://github.com/CorentinTh/quadtree-js","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fquadtree-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fquadtree-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fquadtree-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CorentinTh%2Fquadtree-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CorentinTh","download_url":"https://codeload.github.com/CorentinTh/quadtree-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250048101,"owners_count":21366176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["intersects","javascript","js","node-module","nodejs","quadtree","quadtree-js","range","typescript"],"created_at":"2024-10-10T03:18:40.877Z","updated_at":"2025-04-21T11:33:28.481Z","avatar_url":"https://github.com/CorentinTh.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cdiv align=\"center\"\u003e\n\n  ![logo](.github/logo.png)\n  \n\u003c/div\u003e\n\n\n\u003cdiv align=\"center\"\u003e\n\n  [![Weekly Downloads](https://img.shields.io/npm/dw/js-quadtree.svg)](https://www.npmjs.com/package/js-quadtree) \n  [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/CorentinTh/quadtree-js/Node%20CI)](https://github.com/CorentinTh/quadtree-js/actions?query=workflow%3A%22Node+CI%22) \n  [![Coverage Status](https://codecov.io/gh/CorentinTh/quadtree-js/branch/master/graph/badge.svg)](https://codecov.io/gh/CorentinTh/quadtree-js) \n  [![npm bundle size](https://img.shields.io/bundlephobia/minzip/js-quadtree.svg)](https://www.npmjs.com/package/js-quadtree) \n  [![GitHub package.json version](https://img.shields.io/github/package-json/v/CorentinTh/quadtree-js.svg)](https://github.com/CorentinTh/quadtree-js/blob/master/package.json) \n  [![Dependencies](https://img.shields.io/badge/dependencies-0-green)](https://www.npmjs.com/package/js-quadtree) \n  [![Licence Badge](https://img.shields.io/github/license/CorentinTh/quadtree-js.svg)](LICENCE)\n    \n\u003c/div\u003e\n\nA powerful quadtree implementation in javascript. It can be used for nodejs or directly in the browser.\n\n## Installation\n### Node JS\n**Quadtree-js** can be installed using yarn or npm.\n\n```bash\nnpm install js-quadtree\n# or\nyarn add js-quadtree\n```\n\nAnd import :\n\n```javascript\n// EMAScript import\nimport {QuadTree, Box, Point, Circle} from 'js-quadtree';\n// Or Common JS:\nconst {QuadTree, Box, Point, Circle} = require('js-quadtree');\n\nconst quadtree = new QuadTree(new Box(0, 0, 1000, 1000));\n\nquadtree.insert(new Point(100, 200, {custom: 'data'}));\n\nconst results = quadtree.query(new Circle(150, 150, 100));\n```\n### Browser\n\nYou can use the CDN:\n```html\n\u003cscript src=\"https://unpkg.com/js-quadtree\"\u003e\u003c/script\u003e\n```\nAnd everything is globally accessible and **prefixed with `QT`**:\n```javascript\nconst quadtree = new QT.QuadTree(new QT.Box(0, 0, 1000, 1000));\n\nquadtree.insert(new QT.Point(100, 200, {custom: 'data'}));\n\nconst results = quadtree.query(new QT.Circle(150, 150, 100));\n```\n \n## Usage\n### Creation\n```javascript\n// Create the bounding area of the quadtree (x, y, width, height)\nconst boundingArea = new Box(0, 0, 1000, 1000);\n\n// Instantiate  the new quadtree\nconst quadtree = new QuadTree(boundingArea);\n```\n\nYou can also specify the following optional parameters:\n```javascript\n// Create the bounding area of the quadtree (x, y, width, height)\nconst boundingArea = new Box(0, 0, 1000, 1000);\n\nconst config = {\n    capacity: 10,            // Specify the maximum amount of point per node (default: 4)\n    removeEmptyNodes: true,  // Specify if the quadtree has to remove subnodes if they are empty (default: false).\n    maximumDepth: 5,         // Specify the maximum depth of the quadtree. -1 for no limit (default: -1).\n    // Specify a custom method to compare point for removal (default: (point1, point2) =\u003e point1.x === point2.x \u0026\u0026 point1.y === point2.y).\n    arePointsEqual: (point1, point2) =\u003e point1.data.foo === point2.data.foo      \n};\n\n// An array of point to insert directly (same as quadtree.insert(points) )\nconst points = [new Point(10, 10), new Point(52, 64)];\n\nconst quadtree = new QuadTree(boundingArea, config, points);\n```\n\n### Insert\n\nYou can insert a **Point** element, an array of **Point** element, your **own element** as long as it has an **x** and a **y** property or an array of custom element.\n\n```javascript\nconst point = new Point(10, 25);\n\nconst pointArray = [\n    new Point(45, 22),\n    new Point(30, 60),\n    new Point(14, 12)\n];\n\nconst customPoint = {\n    x: 94,\n    y: 23,\n    customField:{}\n};\n\nquadtree.insert(point);\nquadtree.insert(pointArray);\nquadtree.insert(customPoint);\n```\n\nYou can add your data in a **Point** element:\n```javascript\nconst myData = {\n    foo: 'bar'\n};\n\nconst point = new Point(50, 50, myData);\n\nconsole.log(point.data.foo); // 'bar'\n```\n\n### Remove\n\nAs the **insert** method, you can remove a **Point** element, an array of **Point** element, your **own element** as long as it has an **x** and a **y** property or an array of custom element.\n\nBy default, points having the same **x** and **y** values will be removed. To override this behavior, add a method under `arePointsEqual` in the config of the quadtree that takes two points in parameters and return a boolean if the points are equal.\nExample: `const quadtree = new QuadTree(boundingArea, {arePointsEqual: (point1, point2) =\u003e point1.data.foo === point2.data.foo});` \n\n```javascript\nconst point = new Point(10, 25);\n\nconst pointArray = [\n    new Point(45, 22),\n    new Point(30, 60),\n    new Point(14, 12)\n];\n\nconst customPoint = {\n    x: 94,\n    y: 23\n};\n\nquadtree.remove(point);\nquadtree.remove(pointArray);\nquadtree.remove(customPoint);\n```\n\n**Note**: it doesn't have to be the same object, the test is done with the coordinates.\n\n### Query\n\nUse the **query** method to get all the point within a range.\n\n\n```javascript\n// This will return all the points in the given Box (x, y, width, height)\nconst points = quadtree.query(new Box(10, 10, 100, 100));\n```\n\n```javascript\n// This will return all the points in the given Circle (x, y, radius)\nconst points = quadtree.query(new Circle(10, 10, 100));\n```\n\nYou can use a **Box** or a **Circle** as a range or even your own range element as long as it has the following methods:\n* **contains**: return true if a point is within this range, false otherwise.\n* **intersects**: return true if a **Box** intersects with this range, false otherwise.\nSee [the Box definition](src/Box.js) for a good example.\n\n### Get all the point\n\nIf want to retrieve all the point, you can use this method:\n\n```javascript\nconst points = quadtree.getAllPoints();\n```\n\n**Note**: you may want to store your points in a side array since, it have to look trough all the child nodes.\n\n### Get Tree\n\nYou can get the amount of points by nodes with the `getTree()` method.\n\n```javascript\nconst qt = new QuadTree(new Box(0, 0, 10, 10));\n\nqt.insert(new Point(5, 5));\nqt.insert(new Point(6, 5));\nqt.insert(new Point(4, 5));\nqt.insert(new Point(3, 5));\nqt.insert(new Point(2, 5));\nqt.insert(new Point(1, 5));\n\nconsole.log(qt.getTree());\n\n// {\n//     ne: 2, \n//     nw: {\n//         ne: 0, \n//         nw: 0, \n//         se: 3, \n//         sw: 2\n//     }, \n//     se: 2,\n//     sw: 3\n// }\n```\n\n### Clear\n\nUse this method to clear the quadtree. It remove all the points and sub-nodes.\n\n```javascript\nquadtree.clear();\n```\n\n## Contribute\n**Pull requests are welcome !** Feel free to contribute.\n\n## Credits\nCoded with ❤️ by [Corentin Thomasset](//corentin-thomasset.fr).\n\n## License\n\nThis project is under the [MIT license](LICENSE).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorentinth%2Fquadtree-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorentinth%2Fquadtree-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorentinth%2Fquadtree-js/lists"}