]>
git.bts.cx Git - benzene.git/blob - src/bz/math/vector.c
1 #include <bz/math/vector.h>
3 #include <bz/math/math.h>
5 void bzVectorCopy(BZVector
*vectorOut
, const BZVector
*vector
) {
8 bzVectorSet(vectorOut
, x
, y
);
11 void bzVectorMakeAngle(BZVector
*vectorOut
, float angle
) {
12 float x
= bzCosine(angle
);
13 float y
= bzSine(angle
);
14 bzVectorSet(vectorOut
, x
, y
);
18 void bzVectorInverse(BZVector
*vectorOut
, const BZVector
*vector
) {
19 float x
= -(vector
->x
);
20 float y
= -(vector
->y
);
21 bzVectorSet(vectorOut
, x
, y
);
24 void bzVectorTangent(BZVector
*vectorOut
, const BZVector
*vector
) {
25 float x
= -(vector
->y
);
27 bzVectorSet(vectorOut
, x
, y
);
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
);
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
);
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
);
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
);
55 float bzVectorMagnitude(const BZVector
*vector
) {
56 float sqMagnitude
= bzVectorMagnitudeSquared(vector
);
57 float magnitude
= bzSqrt(sqMagnitude
);
61 float bzVectorMagnitudeSquared(const BZVector
*vector
) {
62 return bzVectorDot(vector
, vector
);
65 float bzVectorDot(const BZVector
*vector1
, const BZVector
*vector2
) {
66 float dot
= (vector1
->x
* vector2
->x
) + (vector1
->y
* vector2
->y
);
70 float bzVectorCross(const BZVector
*vector1
, const BZVector
*vector2
) {
71 float crossZ
= (vector1
->x
* vector2
->y
) - (vector1
->y
* vector2
->x
);
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
);