aboutsummaryrefslogtreecommitdiff
path: root/test-client/util.ts
blob: 6febac376389dd26fdbeab3486e80b21fb692f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export interface V2 { x: number, y: number }
export function length(p: V2): number { return Math.sqrt(p.x * p.x + p.y * p.y) }
export function normalize_mut(p: V2) { const l = length(p); if (l == 0) return; p.x /= l; p.y /= l }
export function normalize(p: V2): V2 { let l = length(p); if (l == 0) l = 1; return { x: p.x / l, y: p.y / l } }
export function lerp_exp(current: number, target: number, dt: number): number {
    return target + (current - target) * Math.exp(-dt)
}
export function lerp_v2(a: V2, b: V2, t: number): V2 {
    return {
        x: a.x * (1 - t) + b.x * t,
        y: a.y * (1 - t) + b.y * t,
    }
}
export function lerp_exp_v2_mut(current: V2, target: V2, dt: number) {
    current.x = target.x + (current.x - target.x) * Math.exp(-dt)
    current.y = target.y + (current.y - target.y) * Math.exp(-dt)
}
export function floor_v2(p: V2): V2 { return { x: Math.floor(p.x), y: Math.floor(p.y) } }
export function ceil_v2(p: V2): V2 { return { x: Math.ceil(p.x), y: Math.ceil(p.y) } }
export function add_v2(p: V2, o: V2 | number) {
    if (typeof o == "number") return { x: p.x + o, y: p.y + o }
    else return { x: p.x + o.x, y: p.y + o.y }
}