Skip to content

Commit

Permalink
Add math workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
G43riko committed Oct 12, 2024
1 parent 734627b commit f369052
Show file tree
Hide file tree
Showing 7 changed files with 560 additions and 3 deletions.
5 changes: 2 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"workspace": ["./workspaces/enums", "./workspaces/utils"],
"workspace": ["./workspaces/enums", "./workspaces/utils", "./workspaces/math"],
"tasks": {
"bench": "deno bench",
"bench:utils": "deno bench --filter utils",
"check": "deno check workspaces/**/src/**/*.ts",
"doc": "deno doc --html --name=\"@g43/tools\" --output=docs workspaces/enums/src/index.ts workspaces/utils/src/index.ts",
"doc:enums": "deno doc --html --name=\"@g43/enums\" --output=docs workspaces/enums/src/index.ts",
"doc": "deno doc --html --name=\"@g43/tools\" --output=docs workspaces/math/src/index.ts workspaces/enums/src/index.ts workspaces/utils/src/index.ts",
"test": "deno test workspaces/**/src/**/*.spec.ts",
"test:doc": "deno test --doc workspaces/**/src/**/*.ts",
"test:coverage": "deno test --clean --coverage workspaces/**/src/**/*.spec.ts",
Expand Down
7 changes: 7 additions & 0 deletions workspaces/math/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/G43riko/GTools/blob/master/LICENSE)
[![JSR](https://jsr.io/badges/@g43/math)](https://jsr.io/@g43/math)
[![JSR Score](https://jsr.io/badges/@g43/math/score)](https://jsr.io/@g43/math)

# #g43/enums

[Documentation](https://g43riko.github.io/GTools/)
5 changes: 5 additions & 0 deletions workspaces/math/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@g43/math",
"version": "0.0.1",
"exports": "./src/index.ts"
}
Empty file added workspaces/math/src/index.ts
Empty file.
8 changes: 8 additions & 0 deletions workspaces/math/src/simple-vector2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface SimpleVector2 {
x: number;
y: number;
}
export interface ReadonlySimpleVector2 {
readonly x: number;
readonly y: number;
}
52 changes: 52 additions & 0 deletions workspaces/math/src/vector2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { assertEquals } from "@std/assert";
import { Vector2 } from "./vector2.ts";

const vec0_0 = new Vector2();
const vec5_0 = new Vector2(5, 0);
const vec0_5 = new Vector2(0, 5);
const vec5_5 = new Vector2(5, 5);
const vecm5_0 = new Vector2(-5, 0);
const vec0_m5 = new Vector2(0, -5);
const vecm5_m5 = new Vector2(-5, -5);

Deno.test("Vector2.prototype.lerp", () => {
assertEquals({ ...Vector2.lerp({ x: 0, y: 0 }, { x: 10, y: 10 }, 0) }, { x: 0, y: 0 });
assertEquals({ ...Vector2.lerp({ x: 0, y: 0 }, { x: 10, y: 10 }, 0.25) }, { x: 2.5, y: 2.5 });
assertEquals({ ...Vector2.lerp({ x: 0, y: 0 }, { x: 10, y: 10 }, 0.5) }, { x: 5, y: 5 });
assertEquals({ ...Vector2.lerp({ x: 0, y: 0 }, { x: 10, y: 10 }, 0.75) }, { x: 7.5, y: 7.5 });
assertEquals({ ...Vector2.lerp({ x: 0, y: 0 }, { x: 10, y: 10 }, 1) }, { x: 10, y: 10 });
})
Deno.test("Vector2.prototype.min and Vector2.prototype.max", () => {
assertEquals(5, vec5_0.max);
assertEquals(0, vec5_0.min);
});

Deno.test("Vector2.prototype.avg", () => {
assertEquals(vec5_0.avg, 2.5);
assertEquals(vec0_5.avg, 2.5)
})

Deno.test("Vector2.prototype.dist", () => {
assertEquals(vec0_0.dist(vec5_0), 5);
assertEquals(vec5_0.dist(vec0_0), 5);
});

Deno.test("Vector2.sqrtDist", () => {
assertEquals(Vector2.sqrtDist(vec0_0, vec5_0), 25);
assertEquals(Vector2.sqrtDist(vec5_0, vec0_0), 25);
});
/*
Deno.test("Vector2.prototype.angle", () => {
assertEquals(Math.abs(vec5_0.angle(vec0_5) - Math.PI / 4), 0);
assertEquals(Math.abs(vec0_5.angle(vec5_0) - Math.PI / 4), 0);
});
Deno.test("Vector2.angle", () => {
assertEquals(Math.abs(Vector2.angle(vec5_0, vec0_5) - Math.PI / 4), 0);
assertEquals(Math.abs(Vector2.angle(vec0_5, vec5_0) - Math.PI / 4), 0);
});
*/
Deno.test("Vector2.prototype.normalize", () => {
assertEquals(vec5_0.normalize().toString(), "[1, 0]");
assertEquals(vec0_5.normalize().toString(), "[0, 1]");
});
Loading

0 comments on commit f369052

Please sign in to comment.