From 97a7529081c2bfa6bddf5f2fd8d32f3b01e17219 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Wed, 29 Apr 2020 12:07:25 +0200 Subject: [PATCH] =?UTF-8?q?feat(=E2=A8=AF):=20New=20vector=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/Vectors.ts | 23 ++++++++++++++++++--- packages/core/src/__tests__/Vectors.test.ts | 4 ++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/core/src/Vectors.ts b/packages/core/src/Vectors.ts index 7d832ca9..02d7d927 100644 --- a/packages/core/src/Vectors.ts +++ b/packages/core/src/Vectors.ts @@ -47,8 +47,11 @@ const apply = (fn: Fn, ...vectors: Adaptable[]) => ({ const add = (...vectors: BinArgOp) => apply(Animated.add, ...vectors); const sub = (...vectors: BinArgOp) => apply(Animated.sub, ...vectors); -const dot = (...vectors: BinArgOp) => apply(Animated.multiply, ...vectors); const div = (...vectors: BinArgOp) => apply(Animated.divide, ...vectors); +const mul = (...vectors: BinArgOp) => apply(Animated.multiply, ...vectors); +const pow = (...vectors: [Adaptable, number]) => + apply(Animated.pow, ...vectors); +const sqrt = (...vectors: SingleArgOp) => apply(Animated.sqrt, ...vectors); const cos = (...vectors: SingleArgOp) => apply(Animated.cos, ...vectors); const sin = (...vectors: SingleArgOp) => apply(Animated.sin, ...vectors); const min = (vector: Adaptable, value: Animated.Adaptable) => @@ -58,7 +61,7 @@ const max = (vector: Adaptable, value: Animated.Adaptable) => const clamp = (value: Adaptable, minVec: Adaptable, maxVec: Adaptable) => apply(clamp1, value, minVec, maxVec); -const invert = (a: Adaptable) => dot(-1, a); +const invert = (a: Adaptable) => mul(-1, a); const set = (a: Vector>, b: Adaptable) => block([ @@ -66,6 +69,14 @@ const set = (a: Vector>, b: Adaptable) => Animated.set(a.y, isAdaptable(b) ? b : b.y), ]); +const length = (v: Vector) => + Animated.sqrt(Animated.add(Animated.pow(v.x, 2), Animated.pow(v.y, 2))); +const normalize = (v: Vector) => div(v, length(v)); +const dot = (v1: Vector, v2: Vector) => + add(Animated.multiply(v1.x, v2.x), Animated.multiply(v1.y, v2.y)); +const cross = (v1: Vector, v2: Vector) => + sub(Animated.multiply(v1.x, v2.y), Animated.multiply(v1.y, v2.x)); + export const vec = { create, createValue, @@ -74,8 +85,11 @@ export const vec = { sub, dot, div, - multiply: dot, + mul, + multiply: mul, divide: div, + pow, + sqrt, set, clamp, apply, @@ -83,4 +97,7 @@ export const vec = { max, cos, sin, + length, + normalize, + cross, }; diff --git a/packages/core/src/__tests__/Vectors.test.ts b/packages/core/src/__tests__/Vectors.test.ts index 985d729d..37fdbcf6 100644 --- a/packages/core/src/__tests__/Vectors.test.ts +++ b/packages/core/src/__tests__/Vectors.test.ts @@ -70,8 +70,8 @@ test("multiply", () => { const c = vec.create(1, 1); const d = vec.create(2, 2); const result = vec.createValue(2, 2); - expect(vec.dot(a, b, c, d)).toEqual(result); - expect(vec.dot(2, 1, 1, 1)).toEqual(result); + expect(vec.mul(a, b, c, d)).toEqual(result); + expect(vec.mul(2, 1, 1, 1)).toEqual(result); }); test("divide", () => {