]> git.bts.cx Git - benzene.git/blob - src/bz/math/vector.c
Initial version
[benzene.git] / src / bz / math / vector.c
1 #include <bz/math/vector.h>
2
3 #include <bz/math/math.h>
4
5 void bzVectorCopy(BZVector *vectorOut, const BZVector *vector) {
6 float x = vector->x;
7 float y = vector->y;
8 bzVectorSet(vectorOut, x, y);
9 }
10
11 void bzVectorMakeAngle(BZVector *vectorOut, float angle) {
12 float x = bzCosine(angle);
13 float y = bzSine(angle);
14 bzVectorSet(vectorOut, x, y);
15
16 }
17
18 void bzVectorInverse(BZVector *vectorOut, const BZVector *vector) {
19 float x = -(vector->x);
20 float y = -(vector->y);
21 bzVectorSet(vectorOut, x, y);
22 }
23
24 void bzVectorTangent(BZVector *vectorOut, const BZVector *vector) {
25 float x = -(vector->y);
26 float y = vector->x;
27 bzVectorSet(vectorOut, x, y);
28 }
29
30 void bzVectorAdd(BZVector *vectorOut, const BZVector *vector1, const BZVector *vector2) {
31 float x = vector1->x + vector2->x;
32 float y = vector1->y + vector2->y;
33 bzVectorSet(vectorOut, x, y);
34 }
35
36 void bzVectorSubtract(BZVector *vectorOut, const BZVector *vector1, const BZVector *vector2) {
37 float x = vector1->x - vector2->x;
38 float y = vector1->y - vector2->y;
39 bzVectorSet(vectorOut, x, y);
40 }
41
42 void bzVectorScale(BZVector *vectorOut, const BZVector *vector, float scale) {
43 float x = vector->x * scale;
44 float y = vector->y * scale;
45 bzVectorSet(vectorOut, x, y);
46 }
47
48 void bzVectorNormalized(BZVector *vectorOut, const BZVector *vector) {
49 float magnitude = bzVectorMagnitude(vector);
50 float x = vector->x / magnitude;
51 float y = vector->y / magnitude;
52 bzVectorSet(vectorOut, x, y);
53 }
54
55 float bzVectorMagnitude(const BZVector *vector) {
56 float sqMagnitude = bzVectorMagnitudeSquared(vector);
57 float magnitude = bzSqrt(sqMagnitude);
58 return magnitude;
59 }
60
61 float bzVectorMagnitudeSquared(const BZVector *vector) {
62 return bzVectorDot(vector, vector);
63 }
64
65 float bzVectorDot(const BZVector *vector1, const BZVector *vector2) {
66 float dot = (vector1->x * vector2->x) + (vector1->y * vector2->y);
67 return dot;
68 }
69
70 float bzVectorCross(const BZVector *vector1, const BZVector *vector2) {
71 float crossZ = (vector1->x * vector2->y) - (vector1->y * vector2->x);
72 return crossZ;
73 }
74
75 void bzVectorLerp(BZVector *vectorOut, const BZVector *vector1, const BZVector *vector2, float t) {
76 vectorOut->x = bzLerp(vector1->x, vector2->x, t);
77 vectorOut->y = bzLerp(vector1->y, vector2->y, t);
78 }
79