VectorMath/Src/vector.c

90 lines
2.3 KiB
C

/*
* vector.c
*
* Created on: Jun 1, 2021
* Author: zemon
*/
#include "vector.h"
#include "math.h"
vector3 vector3Add(vector3 one, vector3 two) {
return (vector3) {one.x + two.x, one.y + two.y, one.z + two.z};
}
vector3 vector3Less(vector3 one, vector3 two) {
return (vector3) {one.x - two.x, one.y - two.y, one.z - two.z};
}
vector3 vector3DivNum(vector3 vec, float num) {
return (vector3) {vec.x / num, vec.y / num, vec.z / num};
}
vector3 vector3MultiplyNum(vector3 vec, float num) {
return (vector3) {vec.x * num, vec.y * num, vec.z * num};
}
float vector3Sum(vector3 vec) {
return vec.x + vec.y + vec.z;
}
float vector3DotProduct(vector3 first, vector3 second) {
return first.x * second.x + first.y * second.y + first.z * second.z;
}
float vector3LenSquare(vector3 vec) {
return vec.x * vec.x + vec.y * vec.y + vec.z * vec.z;
}
float vector3Len(vector3 vec) {
return sqrt(vector3LenSquare(vec));
}
float floatAbs(float value) {
return value >= 0 ? value : -1 * value;
}
vector3 vector3AbsVector(vector3 one) {
return (vector3) {floatAbs(one.x), floatAbs(one.y), floatAbs(one.z)};
}
float vector3AngleCos(vector3 first, vector3 second) {
return vector3DotProduct(first, second) / (vector3Len(first) * vector3Len(second));
}
///!!!! VERY EXPENSIVE calculation
float vector3Angle(vector3 first, vector3 second) {
return acosf(vector3AngleCos(first, second));
}
vector3 vector3Projection(vector3 projector, vector3 receiver) {
return vector3MultiplyNum(receiver, vector3DotProduct(projector, receiver) / vector3DotProduct(receiver, receiver));
}
vector3int32 vector3Int32Less(vector3int32 one, vector3int32 two) {
return (vector3int32) {one.x - two.x, one.y - two.y, one.z - two.z};
}
vector3int32 vector3Int32Add(vector3int32 one, vector3int32 two) {
return (vector3int32) {one.x + two.x, one.y + two.y, one.z + two.z};
}
vector3int32 vector3Int32Div(vector3int32 vector, int32_t divider) {
return (vector3int32) {vector.x / divider, vector.y / divider, vector.z / divider};
}
vector3int16 vector3Int32toInt16(vector3int32 vector) {
return (vector3int16) {vector.x, vector.y, vector.z};
}
int32_t intAbs(int32_t value) {
return value >= 0 ? value : -1 * value;
}
vector3int32 vector3int32Abs(vector3int32 one) {
return (vector3int32) {intAbs(one.x), intAbs(one.y), intAbs(one.z)};
}