-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
101 lines (82 loc) · 1.87 KB
/
vector.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
#include "vector.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
vector *new_vector(double x, double y, double z) {
vector *new = (vector *) calloc(1, sizeof(vector));
new->x = x;
new->y = y;
new->z = z;
return new;
}
vector *new_zvector() {
return (vector *) calloc(1, sizeof(vector));
}
vector *cpy_vector(const vector *v) {
vector *new = (vector *) calloc(1, sizeof(vector));
new->x = v->x;
new->y = v->y;
new->z = v->z;
return new;
}
void set(vector *v, double x, double y, double z) {
v->x = x;
v->y = y;
v->z = z;
}
void assign(vector *v, const vector *u) {
v->x = u->x;
v->y = u->y;
v->z = u->z;
}
void add(const vector *l, const vector *r, vector *res) {
set(res, l->x + r->x,
l->y + r->y,
l->z + r->z);
}
void sub(const vector *l, const vector *r, vector *res) {
set(res, l->x - r->x,
l->y - r->y,
l->z - r->z);
}
void mul(const vector *l, double r, vector *res) {
set(res, l->x * r,
l->y * r,
l->z * r);
}
void move(vector *v, const vector *oth) {
v->x += oth->x;
v->y += oth->y;
v->z += oth->z;
}
void scale(vector *v, double factor) {
v->x *= factor;
v->y *= factor;
v->z *= factor;
}
double dot(const vector *l, const vector *r) {
return l->x * r->x +
l->y * r->y +
l->z * r->z;
}
void cross(const vector *l, const vector *r, vector *res) {
res->x = l->y * r->z - l->z * r->y;
res->y = l->z * r->x - l->x * r->z;
res->z = l->x * r->y - l->y * r->x;
}
double mixed(const vector *a, const vector *b, const vector *c) {
return a->x * (b->y * c->z - b->z * c->y) -
a->y * (b->x * c->z - b->z * c->x) +
a->z * (b->x * c->y - b->y * c->x);
}
double len_sq(const vector *v) {
return v->x * v->x +
v->y * v->y +
v->z * v->z;
}
double len(const vector *v) {
return sqrt(len_sq(v));
}
void dump(const vector *v) {
printf("(%g, %g, %g)\n", v->x, v->y, v->z);
}