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
|
#pragma once
#include <math.h>
typedef struct { double x, y, z; } Vec3;
static inline double vec3_magnitude(Vec3 v) {
return hypot(v.x, hypot(v.y, v.z));
}
static inline Vec3 vec3_diff(Vec3 a, Vec3 b) {
return (Vec3){.x = a.x - b.x,
.y = a.y - b.y,
.z = a.z - b.z};
}
static inline Vec3 vec3_normalize(Vec3 v) {
if (v.x == 0.0 || v.y == 0.0 || v.z == 0.0)
return (Vec3){.x = 0.0,
.y = 0.0,
.z = 0.0};
double len = sqrt(v.x * v.x
+ v.y * v.y
+ v.z * v.z);
return (Vec3){.x = v.x / len,
.y = v.y / len,
.z = v.z / len};
}
static inline Vec3 vec3_mul(Vec3 v, double n) {
return (Vec3) {.x = v.x * n,
.y = v.y * n,
.z = v.z * n};
}
static inline Vec3 vec3_add(Vec3 a, Vec3 b) {
return (Vec3) {.x = a.x + b.x,
.y = a.y + b.y,
.z = a.z + b.z};
}
|