delaunay.js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Johan Karlsson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,73 @@
1
+ import Triangle from './triangle.js';
2
+
3
+ export default function bowyerWatson (superTriangle, pointList) {
4
+ // pointList is a set of coordinates defining the
5
+ // points to be triangulated
6
+ let triangulation = [];
7
+
8
+ // add super-triangle to triangulation
9
+ // must be large enough to completely contain all
10
+ // the points in pointList
11
+ triangulation.push(superTriangle);
12
+
13
+ // add all the points one at a time to the triangulation
14
+ pointList.forEach(point => {
15
+ let badTriangles = [];
16
+
17
+ // first find all the triangles that are no
18
+ // longer valid due to the insertion
19
+ triangulation.forEach(triangle => {
20
+ if(triangle.pointIsInsideCircumcircle(point)) {
21
+ badTriangles.push(triangle);
22
+ }
23
+ });
24
+ let polygon = [];
25
+
26
+ // find the boundary of the polygonal hole
27
+ badTriangles.forEach(triangle => {
28
+ triangle.edges().forEach(edge => {
29
+ let edgeIsShared = false;
30
+ badTriangles.forEach(otherTriangle => {
31
+ if(triangle !== otherTriangle && otherTriangle.hasEdge(edge)) {
32
+ edgeIsShared = true;
33
+ }
34
+ });
35
+ if(!edgeIsShared) {
36
+ //edge is not shared by any other
37
+ // triangles in badTriangles
38
+ polygon.push(edge);
39
+ }
40
+ });
41
+ });
42
+
43
+ // remove them from the data structure
44
+ badTriangles.forEach(triangle => {
45
+ let index = triangulation.indexOf(triangle);
46
+ if (index > -1) {
47
+ triangulation.splice(index, 1);
48
+ }
49
+ });
50
+
51
+ // re-triangulate the polygonal hole
52
+ polygon.forEach(edge => {
53
+ //form a triangle from edge to point
54
+ let newTri = new Triangle(edge[0], edge[1], point);
55
+ triangulation.push(newTri);
56
+ });
57
+ });
58
+
59
+ // done inserting points, now clean up
60
+ let i = triangulation.length;
61
+ while(i--) {
62
+ let triangle = triangulation[i];
63
+ if(triangle.sharesAVertexWith(superTriangle)) {
64
+ //remove triangle from triangulation
65
+ let index = triangulation.indexOf(triangle);
66
+ if (index > -1) {
67
+ triangulation.splice(index, 1);
68
+ }
69
+ }
70
+ }
71
+
72
+ return triangulation;
73
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "delaunay.js",
3
+ "version": "1.0.0",
4
+ "description": "Delaunay triangulation using the Bowyer-Watson algorithm, in JavaScript ",
5
+ "keywords": [
6
+ "triangulation",
7
+ "delaunay",
8
+ "bowyer-watson"
9
+ ],
10
+ "homepage": "https://github.com/DonKarlssonSan/Delaunay.js#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/DonKarlssonSan/Delaunay.js/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/DonKarlssonSan/Delaunay.js.git"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Johan Karlsson (DonKarlssonSan)",
20
+ "type": "module",
21
+ "main": "bowyer-watson.js",
22
+ "scripts": {
23
+ "test": "echo \"Error: no test specified\" && exit 1"
24
+ },
25
+ "dependencies": {
26
+ "vectory-lib": "^0.0.7"
27
+ }
28
+ }
package/readme.md ADDED
@@ -0,0 +1,52 @@
1
+ # Installation
2
+
3
+ ```
4
+ npm install delaunay
5
+ ```
6
+
7
+ # Usage
8
+
9
+ The `bowyerWatson` function takes two parameters:
10
+ - superTriangle is a triangle that encloses all the points in the list, an instance of Triangle
11
+ - pointList is a list of points, instances of Vector
12
+
13
+ ```
14
+ let triangles = bowyerWatson(superTriangle, pointList);
15
+ ```
16
+
17
+ A more complete example:
18
+
19
+ ```JavaScript
20
+ import bowyerWatson from './bowyer-watson.js';
21
+ import Triangle from './triangle.js';
22
+ import Vector from 'vectory-lib';
23
+
24
+ function getRandomPoints() {
25
+ let points = [];
26
+ let nrOfPoints = 10;
27
+ for(let i = 0; i < nrOfPoints; i++) {
28
+ points.push(new Vector(
29
+ Math.random() * 100,
30
+ Math.random() * 100
31
+ ));
32
+ }
33
+ return points;
34
+ }
35
+
36
+ let pointList = getRandomPoints();
37
+
38
+ let superTriangle = new Triangle(
39
+ new Vector(-1000, 1000),
40
+ new Vector(1000, 1000),
41
+ new Vector(0, -1000)
42
+ );
43
+
44
+ let triangles = bowyerWatson(superTriangle, pointList);
45
+ ```
46
+
47
+ # Referenses
48
+ https://en.wikipedia.org/wiki/Delaunay_triangulation
49
+
50
+ https://en.wikipedia.org/wiki/Bowyer%E2%80%93Watson_algorithm
51
+
52
+ https://en.wikipedia.org/wiki/Circumscribed_circle
package/test.js ADDED
@@ -0,0 +1,29 @@
1
+ import bowyerWatson from './bowyer-watson.js';
2
+ import Triangle from './triangle.js';
3
+ import Vector from 'vectory-lib';
4
+
5
+ function getRandomPoints() {
6
+ let points = [];
7
+ let nrOfPoints = 10;
8
+ for(let i = 0; i < nrOfPoints; i++) {
9
+ points.push(new Vector(
10
+ Math.random() * 100,
11
+ Math.random() * 100
12
+ ));
13
+ }
14
+ return points;
15
+ }
16
+
17
+ let pointList = getRandomPoints();
18
+
19
+ let superTriangle = new Triangle(
20
+ new Vector(-1000, 1000),
21
+ new Vector(1000, 1000),
22
+ new Vector(0, -1000)
23
+ );
24
+
25
+ let triangles = bowyerWatson(superTriangle, pointList);
26
+
27
+ triangles.forEach(triangle => {
28
+ console.log(triangle.vertexes());
29
+ });
package/triangle.js ADDED
@@ -0,0 +1,86 @@
1
+ import Vector from 'vectory-lib';
2
+
3
+ export default class Triangle {
4
+ constructor(a, b, c) {
5
+ this.a = a;
6
+ this.b = b;
7
+ this.c = c;
8
+ }
9
+
10
+ vertexes() {
11
+ return [this.a, this.b, this.c];
12
+ }
13
+
14
+ edges() {
15
+ return [
16
+ [this.a, this.b],
17
+ [this.b, this.c],
18
+ [this.c, this.a]
19
+ ];
20
+ }
21
+
22
+ sharesAVertexWith(triangle) {
23
+ // TODO: optimize me please!
24
+ for(let i = 0; i < 3; i++) {
25
+ for(let j = 0; j < 3; j++) {
26
+ let v = this.vertexes()[i];
27
+ let vv = triangle.vertexes()[j];
28
+ if(v.equals(vv)) {
29
+ return true;
30
+ }
31
+ }
32
+ }
33
+ return false;
34
+ }
35
+
36
+ hasEdge(edge) {
37
+ for(let i = 0; i < 3; i++) {
38
+ let e = this.edges()[i];
39
+ if(e[0].equals(edge[0]) && e[1].equals(edge[1]) ||
40
+ e[1].equals(edge[0]) && e[0].equals(edge[1])) {
41
+ return true;
42
+ }
43
+ }
44
+ return false;
45
+ }
46
+
47
+ get circumcenter() {
48
+ if(!this._circumcenter) {
49
+ let d = 2 * (this.a.x * (this.b.y - this.c.y) +
50
+ this.b.x * (this.c.y - this.a.y) +
51
+ this.c.x * (this.a.y - this.b.y));
52
+
53
+ let x = 1 / d * ((this.a.x * this.a.x + this.a.y * this.a.y) * (this.b.y - this.c.y) +
54
+ (this.b.x * this.b.x + this.b.y * this.b.y) * (this.c.y - this.a.y) +
55
+ (this.c.x * this.c.x + this.c.y * this.c.y) * (this.a.y - this.b.y));
56
+
57
+ let y = 1 / d * ((this.a.x * this.a.x + this.a.y * this.a.y) * (this.c.x - this.b.x) +
58
+ (this.b.x * this.b.x + this.b.y * this.b.y) * (this.a.x - this.c.x) +
59
+ (this.c.x * this.c.x + this.c.y * this.c.y) * (this.b.x - this.a.x));
60
+ this._circumcenter = new Vector(x, y);
61
+ }
62
+
63
+ return this._circumcenter;
64
+
65
+ }
66
+
67
+ get centroid() {
68
+ if(!this._centroid) {
69
+ this._centroid = this.a.add(this.b).add(this.c).div(3);
70
+ }
71
+ return this._centroid;
72
+ }
73
+
74
+ get circumradius() {
75
+ if(!this._circumradius) {
76
+ this._circumradius = this.circumcenter.sub(this.a).getLength();
77
+ }
78
+ return this._circumradius;
79
+ }
80
+
81
+ pointIsInsideCircumcircle(point) {
82
+ let dist = point.sub(this.circumcenter).getLength();
83
+
84
+ return dist < this.circumradius;
85
+ }
86
+ }