aboutsummaryrefslogtreecommitdiff
path: root/mons_math/src/vec4.c
blob: d4474ee4f6dfe57f9e7cfab75cf83968f683e6aa (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "mons_math/vec4.h"
#include "mons_math/vec3.h"
#include "mons_math/util.h"
#include <math.h>
#include <stdio.h>

mons_vec4 mons_vec4_add(mons_vec4 a, mons_vec4 b) {
    mons_vec4 result = {
        a.x + b.x,
        a.y + b.y,
        a.z + b.z,
        a.w + b.w,
    };
    return result;
}

void mons_vec4_add_inplace(mons_vec4 *a, mons_vec4 b) {
    a->x += b.x;
    a->y += b.y;
    a->z += b.z;
    a->w += b.w;
}

mons_vec4 mons_vec4_sub(mons_vec4 a, mons_vec4 b) {
    mons_vec4 result = {
        a.x - b.x,
        a.y - b.y,
        a.z - b.z,
        a.w - b.w,
    };
    return result;
}

void mons_vec4_sub_inplace(mons_vec4 *a, mons_vec4 b) {
    a->x -= b.x;
    a->y -= b.y;
    a->z -= b.z;
    a->w -= b.w;
}

mons_vec4 mons_vec4_mul_f(mons_vec4 a, float b) {
    mons_vec4 result = {
        a.x * b,
        a.y * b,
        a.z * b,
        a.w * b,
    };
    return result;
}

mons_vec4 mons_vec4_mul_i(mons_vec4 a, int b) {
    mons_vec4 result = {
        a.x * b,
        a.y * b,
        a.z * b,
        a.w * b,
    };
    return result;
}

void mons_vec4_mul_f_inplace(mons_vec4 *a, float b) {
    a->x *= b;
    a->y *= b;
    a->z *= b;
    a->w *= b;
}

void mons_vec4_mul_i_inplace(mons_vec4 *a, int b) {
    a->x *= b;
    a->y *= b;
    a->z *= b;
    a->w *= b;
}

mons_vec4 mons_vec4_div_f(mons_vec4 a, float b) {
    return (mons_vec4) {
        a.x / b,
        a.y / b,
        a.z / b,
        a.w / b,
    };
}

mons_vec4 mons_vec4_div_i(mons_vec4 a, int b) {
    return (mons_vec4) {
        a.x / b,
        a.y / b,
        a.z / b,
        a.w / b,
    };
}

void mons_vec4_div_f_inplace(mons_vec4 *a, float b) {
    a->x /= b;
    a->y /= b;
    a->z /= b;
    a->w /= b;
}

void mons_vec4_div_i_inplace(mons_vec4 *a, int b) {
    a->x /= b;
    a->y /= b;
    a->z /= b;
    a->w /= b;
}

float mons_vec4_dot(mons_vec4 a, mons_vec4 b) {
    return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
}

float mons_vec4_len(mons_vec4 a) { return sqrt(mons_vec4_len_squared(a)); }

float mons_vec4_len_squared(mons_vec4 a) {
    return (a.x * a.x) + (a.y * a.y) + (a.z * a.z) + (a.w * a.w);
}

mons_vec3 mons_vec4_truncate(mons_vec4 a) {
    mons_vec3 result = {a.x, a.y, a.z};
    return result;
}

int mons_vec4_equal(mons_vec4 a, mons_vec4 b) {
    return mons_float_approx_equal(a.x, b.x)
        && mons_float_approx_equal(a.y, b.y)
        && mons_float_approx_equal(a.z, b.z)
        && mons_float_approx_equal(a.w, b.w);
}

mons_vec4 mons_vec4_negate(mons_vec4 a) {
    return mons_vec4_mul_i(a, -1);
}

void mons_vec4_negate_inplace(mons_vec4 *a) {
    mons_vec4_mul_i_inplace(a, -1);
}

mons_vec4 mons_vec4_normalize(mons_vec4 a) {
    float len = mons_vec4_len(a);
    return mons_vec4_div_f(a, len);
}

void mons_vec4_normalize_inplace(mons_vec4 *a) {
    float len = mons_vec4_len(*a);
    mons_vec4_div_f_inplace(a, len);
}

void mons_vec4_print(mons_vec4 vec) {
    printf("[%f, %f, %f, %f]", vec.x, vec.y, vec.z, vec.w);
}