summaryrefslogtreecommitdiff
path: root/frontend/_shared/math/vec2.js
blob: 11e71cd62f01e7ed2431834997d0f41965a2bfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function vec2(x = 0, y = 0) {
    return { x: x, y: y};
}

function addVec2(v1, v2) {
    return {
        x: v1.x + v2.x,
        y: v1.y + v2.y
    };
}

function subVec2(v1, v2) {
    return {
        x: v1.x - v2.x,
        y: v1.y - v2.y
    };
}

function scaleVec2(v, s) {
    return {
        x: v.x * s,
        y: v.y * s
    };
}

function dot2(v1, v2) {
    return v1.x * v2.x + v1.y * v2.y;
}

function length2(v) {
    return Math.sqrt(v.x * v.x + v.y * v.y);
}

function normalize2(v) {
    const lLength = length2(v);
    const lInverseLength = lLength === 0 ? 1.0 : 1.0 / length2(v);
    return { x: v.x * lInverseLength, y: v.y * lInverseLength };
}

function vec2str(v) {
    return `(${v.x.toFixed(2)}, ${v.y.toFixed(2)})`;
}

function getPerp2(v) {
    return {
        x: -v.y,
        y: v.x
    };
}

function negate2(v) {
    return {
        x: -v.x,
        y: -v.y
    };
}

// Algorithm plucked from: https://matthew-brett.github.io/teaching/rotation_2d.html
function rotateAboutOrigin2(v, angle) {
    return {
        x: v.x * Math.cos(angle) - v.y * Math.sin(angle),
        y: v.x * Math.sin(angle) + v.y * Math.cos(angle),
    };
}