From f1a6535d77545c6c0e452dee32abbabbd65d8ca1 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Thu, 15 May 2025 18:44:03 +0200 Subject: [PATCH 1/7] Add angleTo --- index.js | 4 ++++ test/test.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/index.js b/index.js index 9a0f76b..5f2f035 100644 --- a/index.js +++ b/index.js @@ -133,6 +133,10 @@ class Vec3 { euclideanMod(this.z, other.z)) } + angleTo (other) { + return Math.acos(this.dot(other) / (this.norm() * other.norm())) + } + distanceTo (other) { const dx = other.x - this.x const dy = other.y - this.y diff --git a/test/test.js b/test/test.js index 1c24eec..a3fefc8 100644 --- a/test/test.js +++ b/test/test.js @@ -182,6 +182,15 @@ describe('vec3', function () { assert.strictEqual(v2.y, 1.5) assert.strictEqual(v2.z, 1.9) }) + it('angleTo', function () { + const v1 = new Vec3(1, 1, 1) + const v2 = new Vec3(1, 0, 0) + const angle1 = v1.angleTo(v2) + const angle2 = v2.angleTo(v1) + const expected = 0.9553166181 + assert.strictEqual(angle1, angle2) + assert.strictEqual(Math.round(angle1 * 100000), Math.round(expected * 100000)) + }) it('distanceTo', function () { const v1 = new Vec3(1, 1, 1) const v2 = new Vec3(2, 2, 2) From 9be28e6e977732635d795afa66712ccb195c190f Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Thu, 15 May 2025 19:03:27 +0200 Subject: [PATCH 2/7] Update readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 389c9b5..e2dc109 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # vec3 + [![NPM version](https://img.shields.io/npm/v/vec3.svg)](http://npmjs.com/package/vec3) [![Build Status](https://github.com/PrismarineJS/node-vec3/workflows/CI/badge.svg)](https://github.com/PrismarineJS/node-vec3/actions?query=workflow%3A%22CI%22) @@ -7,7 +8,7 @@ ## Usage ```js -var v = require('vec3'); +var v = require("vec3"); var v1 = v(1, 2, 3); console.log(v1); // prints "(1, 2, 3)" @@ -18,7 +19,7 @@ console.log(v2); // prints "(1, 2, 4)" Or: ```js -var Vec3 = require('vec3').Vec3; +var Vec3 = require("vec3").Vec3; var v1 = new Vec3(1, 2, 3); // etc... @@ -55,6 +56,7 @@ More available functions are listed below in Test Coverage. ✔ minus ✔ scaled ✔ abs + ✔ angleTo ✔ distanceTo ✔ distanceSquared ✔ equals From 99762c91f26defca7b5972d8d540e5095c1f0283 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Thu, 15 May 2025 19:15:29 +0200 Subject: [PATCH 3/7] Add angleTo in index.d.ts --- index.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.d.ts b/index.d.ts index b40b99f..d4b5386 100644 --- a/index.d.ts +++ b/index.d.ts @@ -126,6 +126,11 @@ export class Vec3 { */ modulus(other: Vec3): Vec3; + /** + * Return the angle to another vector + */ + angleTo(other: Vec3): number; + /** * Return the euclidean distance to another vector */ From 41d6fd558b192a78d7d0d6054905126c1a460656 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Fri, 6 Jun 2025 10:26:39 +0200 Subject: [PATCH 4/7] Improve description Co-authored-by: Augustin Mauroy <97875033+AugustinMauroy@users.noreply.github.com> --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index d4b5386..808f982 100644 --- a/index.d.ts +++ b/index.d.ts @@ -127,7 +127,7 @@ export class Vec3 { modulus(other: Vec3): Vec3; /** - * Return the angle to another vector + * Return the angle between another vector and this one */ angleTo(other: Vec3): number; From 7322f0aa24ab710bd40c45cd185b4d155802b1d6 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Fri, 6 Jun 2025 10:48:38 +0200 Subject: [PATCH 5/7] Add test type --- test/index.test-d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/index.test-d.ts b/test/index.test-d.ts index 896d491..8915c15 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -39,6 +39,7 @@ expectType(vec.scaled(2)); expectType(vec.abs()); expectType(vec.volume()); expectType(vec.modulus(vec)); +expectType(vec.angleTo(vec)); expectType(vec.distanceTo(vec)); expectType(vec.distanceSquared(vec)); expectType(vec.equals(vec)); From 7b0b70b5fdc3eea6bc8fa553fb675f5ffef49d56 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Fri, 6 Jun 2025 10:55:25 +0200 Subject: [PATCH 6/7] Add angleTo test with null vectors --- test/test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test.js b/test/test.js index a3fefc8..72cb437 100644 --- a/test/test.js +++ b/test/test.js @@ -191,6 +191,15 @@ describe('vec3', function () { assert.strictEqual(angle1, angle2) assert.strictEqual(Math.round(angle1 * 100000), Math.round(expected * 100000)) }) + it('angleToNullVectors', function () { + const v1 = new Vec3(0, 0, 0) + const v2 = new Vec3(0, 0, 0) + const angle1 = v1.angleTo(v2) + const angle2 = v2.angleTo(v1) + const expected = NaN + assert.strictEqual(angle1, angle2) + assert.strictEqual(Math.round(angle1 * 100000), Math.round(expected * 100000)) + }) it('distanceTo', function () { const v1 = new Vec3(1, 1, 1) const v2 = new Vec3(2, 2, 2) From 461d7b88519768aea24a67b017d3e2861c705c89 Mon Sep 17 00:00:00 2001 From: Giulio Malventi Date: Fri, 6 Jun 2025 11:45:19 +0200 Subject: [PATCH 7/7] Specify unit --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 808f982..d0d0ef9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -127,7 +127,7 @@ export class Vec3 { modulus(other: Vec3): Vec3; /** - * Return the angle between another vector and this one + * Return the angle in rad between another vector and this one */ angleTo(other: Vec3): number;