aboutsummaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorJoris2022-07-05 22:11:00 +0200
committerJoris2022-07-05 22:11:00 +0200
commit8c1ab4c5756ac43d52bc8773f5e72dde90f79e77 (patch)
treede0db8c859e4204c90ddaa762f4d743fab1dedb2 /src/model
parent1d855b7a50b61650f7ddf40f604c788de1c9e914 (diff)
WIP
Diffstat (limited to 'src/model')
-rw-r--r--src/model/size.ts8
-rw-r--r--src/model/vec2.ts73
2 files changed, 76 insertions, 5 deletions
diff --git a/src/model/size.ts b/src/model/size.ts
new file mode 100644
index 0000000..5e8a778
--- /dev/null
+++ b/src/model/size.ts
@@ -0,0 +1,8 @@
+export interface Size {
+ width: number,
+ height: number,
+}
+
+export function diagonal(size: Size): number {
+ return Math.sqrt(size.width ** 2 + size.height ** 2)
+}
diff --git a/src/model/vec2.ts b/src/model/vec2.ts
index 4bec53d..46ffc7e 100644
--- a/src/model/vec2.ts
+++ b/src/model/vec2.ts
@@ -1,15 +1,78 @@
+import * as Number from 'util/number'
+import * as Size from 'model/size'
+
export interface Vec2 {
x: number,
y: number,
}
-export function zero(): Vec2 {
- return {
- x: 0,
- y: 0,
- }
+export const zero: Vec2 = {
+ x: 0,
+ y: 0,
}
export function equals(v1: Vec2, v2: Vec2): boolean {
return v1.x === v2.x && v1.y === v2.y
}
+
+export function length(v: Vec2): number {
+ return Math.sqrt(v.x ** 2 + v.y ** 2)
+}
+
+export function normalize(v: Vec2): Vec2 {
+ if (v.x === 0 && v.y === 0) {
+ return zero
+ } else {
+ const im = 1 / length(v)
+ return {
+ x: v.x * im,
+ y: v.y * im
+ }
+ }
+}
+
+export function applyOnLength(f: (n: number) => number, v: Vec2): Vec2 {
+ return scale(normalize(v), f(length(v)))
+}
+
+export function sub(v1: Vec2, v2: Vec2): Vec2 {
+ return {
+ x: v1.x - v2.x,
+ y: v1.y - v2.y
+ }
+}
+
+export function div(v: Vec2, k: number): Vec2 {
+ return {
+ x: v.x / k,
+ y: v.y / k
+ }
+}
+
+export function add(v1: Vec2, v2: Vec2): Vec2 {
+ return {
+ x: v1.x + v2.x,
+ y: v1.y + v2.y
+ }
+}
+
+export function scale(v: Vec2, k: number): Vec2 {
+ return {
+ x: v.x * k,
+ y: v.y * k
+ }
+}
+
+export function clamp(v: Vec2, vMin: Vec2, vMax: Vec2): Vec2 {
+ return {
+ x: Number.clamp(v.x, vMin.x, vMax.x),
+ y: Number.clamp(v.y, vMin.y, vMax.y)
+ }
+}
+
+export function project(from: Size.Size, to: Size.Size, v: Vec2): Vec2 {
+ return {
+ x: v.x / from.width * to.width,
+ y: v.y / from.height * to.height
+ }
+}