-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec.c
101 lines (84 loc) · 2.11 KB
/
vec.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
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
/*
* Filename : vec.c
* Last Modified: 19 June 2020
* Owner : Group 20
*
* Description:
* This file contains the definitions of the functions whose declarations are
* specified in vec.h.
*
* Other:
* This file is formatted with a tab indent size of 4 and a character
* restriction of 80/line.
*/
/*------------------------------Standard Headers------------------------------*/
#include <math.h>
/*----------------------------User-defined Headers----------------------------*/
#include "vec.h"
/*----------------------------Function Definitions----------------------------*/
/*================================Constructors================================*/
void vec_set_zero(vec *vp) {
vec_set(vp, 0.0, 0.0, 0.0);
}
void vec_set(vec *vp, const float x, const float y, const float z) {
vp->x = x; vp-> y = y; vp->z = z;
}
/*==============================Unary Operations==============================*/
vec id(vec v) {
return v;
}
vec neg(vec v) {
v.x = -v.x;
v.y = -v.y;
v.z = -v.z;
return v;
}
float len(vec v) {
return sqrt(sq_len(v));
}
float sq_len(vec v) {
return pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2);
}
vec unit(vec v) {
const float s = 1.0 / len(v);
return mul_c(v, s);
}
/*=============================Binary Operations==============================*/
vec add(vec v1, vec v2) {
vec res;
vec_set(&res, v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
return res;
}
vec sub(vec v1, vec v2) {
return add(v1, neg(v2));
}
vec mul_v(vec v1, vec v2) {
vec res;
vec_set(&res, v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
return res;
}
vec mul_c(vec v, const float s) {
v.x *= s;
v.y *= s;
v.z *= s;
return v;
}
vec div_v(vec v1, vec v2) {
vec res;
vec_set(&res, v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
return res;
}
vec div_c(vec v, const float s) {
return mul_c(v, 1.0/s);
}
/*==========================Special Binary Functions==========================*/
float dot(vec v1, vec v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
vec cross(vec v1, vec v2) {
vec res;
vec_set(&res, v1.y * v2.z - v2.y * v1.z,
v2.x * v1.z - v1.x * v2.z,
v1.x * v2.y - v2.x * v1.y);
return res;
}