Skip to content

Commit

Permalink
Add Vec3.fromPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Immugio committed Apr 2, 2024
1 parent 408e784 commit 62684d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion src/__tests__/Vec3.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import { Vec3 } from "../Vec3";
import { Point3 } from "../Point3";

describe("Vec3", () => {
it.each([
[new Vec3(0, 0, 1), undefined, false],
[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)
]);
});
});

0 comments on commit 62684d7

Please sign in to comment.