Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Scene: Vector2.cross, as three.js has it (#133)

Kevin Rajan4c916c4e66e1
diff --git a/packages/scene/src/math.ts b/packages/scene/src/math.ts
index 297c521..98bf0a4 100644
--- a/packages/scene/src/math.ts
+++ b/packages/scene/src/math.ts
@@ -676,6 +676,9 @@ export class Vector2 {
dot(v: Vector2): number {
return this.x * v.x + this.y * v.y;
}
+ cross(v: Vector2): number {
+ return this.x * v.y - this.y * v.x;
+ }
length(): number {
return Math.hypot(this.x, this.y);
}
diff --git a/packages/scene/test/three-compat-vector2.test.ts b/packages/scene/test/three-compat-vector2.test.ts
new file mode 100644
index 0000000..6505faa
--- /dev/null
+++ b/packages/scene/test/three-compat-vector2.test.ts
@@ -0,0 +1,15 @@
+// Vector2 signed cross-product parity with three.js r187.
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("Vector2.cross returns the signed 2D cross product, as three.js r187 does", () => {
+ const a = new THREE.Vector2(3, 4);
+ const b = new THREE.Vector2(-2, 5);
+ assert.equal(a.cross(b), 23);
+ assert.equal(b.cross(a), -23);
+ assert.equal(a.cross(new THREE.Vector2(6, 8)), 0);
+ assert.equal(new THREE.Vector2().cross(a), 0);
+ assert.deepEqual(a.toArray(), [3, 4]);
+ assert.deepEqual(b.toArray(), [-2, 5]);
+});