-
Notifications
You must be signed in to change notification settings - Fork 102
/
fixvector3d.c
58 lines (49 loc) · 1.4 KB
/
fixvector3d.c
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
#include "fixvector3d.h"
#include "fixarray.h"
void v3d_add(v3d *dest, const v3d *a, const v3d *b)
{
dest->x = fix16_add(a->x, b->x);
dest->y = fix16_add(a->y, b->y);
dest->z = fix16_add(a->z, b->z);
}
void v3d_sub(v3d *dest, const v3d *a, const v3d *b)
{
dest->x = fix16_sub(a->x, b->x);
dest->y = fix16_sub(a->y, b->y);
dest->z = fix16_sub(a->z, b->z);
}
void v3d_mul_s(v3d *dest, const v3d *a, fix16_t b)
{
dest->x = fix16_mul(a->x, b);
dest->y = fix16_mul(a->y, b);
dest->z = fix16_mul(a->z, b);
}
void v3d_div_s(v3d *dest, const v3d *a, fix16_t b)
{
dest->x = fix16_div(a->x, b);
dest->y = fix16_div(a->y, b);
dest->z = fix16_div(a->z, b);
}
// Norm
fix16_t v3d_norm(const v3d *a)
{
return fa16_norm(&a->x, &a->y - &a->x, 3);
}
void v3d_normalize(v3d *dest, const v3d *a)
{
v3d_div_s(dest, a, v3d_norm(a));
}
// Dot product
fix16_t v3d_dot(const v3d *a, const v3d *b)
{
return fa16_dot(&a->x, &a->y - &a->x, &b->x, &b->y - &b->x, 3);
}
// Cross product
void v3d_cross(v3d *dest, const v3d *a, const v3d *b)
{
v3d tmp;
fa16_unalias(dest, (void**)&a, (void**)&b, &tmp, sizeof(tmp));
dest->x = fix16_sub(fix16_mul(a->y, b->z), fix16_mul(a->z, b->y));
dest->y = fix16_sub(fix16_mul(a->z, b->x), fix16_mul(a->x, b->z));
dest->z = fix16_sub(fix16_mul(a->x, b->y), fix16_mul(a->y, b->x));
}