summaryrefslogtreecommitdiff
path: root/src/egg/math/eggVector.cpp
blob: 7eb2e4df45877a96e3e2993f7c6f228734ecdcae (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
#include "egg/math/eggVector.h"

namespace EGG {

const Vector2f Vector2f::zero(0.0f, 0.0f);
const Vector2f Vector2f::ex(1.0f, 0.0f);
const Vector2f Vector2f::ey(0.0f, 1.0f);

const Vector3f Vector3f::zero(0.0f, 0.0f, 0.0f);
const Vector3f Vector3f::ex(1.0f, 0.0f, 0.0f);
const Vector3f Vector3f::ey(0.0f, 1.0f, 0.0f);
const Vector3f Vector3f::ez(0.0f, 0.0f, 1.0f);

const Vector3s Vector3s::zero(0, 0, 0);
const Vector3s Vector3s::ex(1, 0, 0);
const Vector3s Vector3s::ey(0, 1, 0);
const Vector3s Vector3s::ez(0, 0, 1);

f32 Vector3f::normalise() {
    f32 len = length();
    if (len > 0.0f) {
        x *= 1.0f / len;
        y *= 1.0f / len;
        z *= 1.0f / len;
    }
    return len;
}

f32 Vector3f::setLength(const Vector3f &src, f32 len) {
    const f32 mag = src.length();

    if (mag <= FLT_EPSILON) {
        z = 0.0f;
        y = 0.0f;
        x = 0.0f;
        return 0.0f;
    }

    x = src.x * (len / mag);
    y = src.y * (len / mag);
    z = src.z * (len / mag);

    return mag;
}

f32 Vector3f::setLength(f32 len) {
    const f32 mag = length();

    if (mag <= FLT_EPSILON) {
        return 0.0f;
    }

    x *= len / mag;
    y *= len / mag;
    z *= len / mag;

    return mag;
}

} // namespace EGG