diff --git a/src/Vec3.ts b/src/Vec3.ts index ebbb4d2..b1fa797 100644 --- a/src/Vec3.ts +++ b/src/Vec3.ts @@ -18,6 +18,14 @@ export class Vec3 extends Vector3 { return new Vec3(point?.x, point?.y, point?.z); } + /** + * Creates a new Vec3[] array from arguments of {x, y, z} objects. + * @param points - The ...{x, y, z} instances. + */ + public static fromPoints(...points: Point3[]): Vec3[] { + return points?.map(p => Vec3.fromPoint(p)) ?? []; + } + /** * Moves this Vec3 instance towards the target Vec3 by the given amount. * @param target - The target Vec3. diff --git a/src/__tests__/Vec3.spec.ts b/src/__tests__/Vec3.spec.ts index d33c0cd..56cc2ac 100644 --- a/src/__tests__/Vec3.spec.ts +++ b/src/__tests__/Vec3.spec.ts @@ -1,4 +1,5 @@ import { Vec3 } from "../Vec3"; +import { Point3 } from "../Point3"; describe("Vec3", () => { it.each([ @@ -6,7 +7,21 @@ describe("Vec3", () => { [new Vec3(0, 0, 1), 1, true], [new Vec3(0, 0, 1), 0.99, false], [new Vec3(0, 0, 0), 0, true], - ])(`%p isNear() to {"x": 0, "y": 0, "z": 0}, width maxDistance %p should return %p`, (v, maxDistance, expected) => { + ])("%p isNear() to {\"x\": 0, \"y\": 0, \"z\": 0}, width maxDistance %p should return %p", (v, maxDistance, expected) => { expect(v.isNear(new Vec3(0, 0), maxDistance)).toBe(expected); }); + + it("should return an array of Vec3 instances when valid Point3 arguments are passed", () => { + const point1: Point3 = { x: 1, y: 2, z: 3 }; + const point2: Point3 = { x: 4, y: 5, z: 6 }; + const point3: Point3 = { x: 7, y: 8, z: 9 }; + + const result = Vec3.fromPoints(point1, point2, point3); + + expect(result).toEqual([ + new Vec3(1, 2, 3), + new Vec3(4, 5, 6), + new Vec3(7, 8, 9) + ]); + }); }); \ No newline at end of file