Browse Source

Vector math !

master
parent
commit
f8cd0ae6f2
  1. 3
      .gitignore
  2. 11
      Makefile
  3. 102
      src/math/vector.c
  4. 83
      src/math/vector.h
  5. 50
      testing/test.c

3
.gitignore vendored

@ -1,3 +1,4 @@
build/
bin/
auxilib
auxilib
.vscode/

11
Makefile

@ -1,5 +1,5 @@
CC=gcc
DEFAULTCFLAGS=-Wall -Werror -O2
DEFAULTCCFLAGS=-Wall -Werror -O2
SRCDIR=src
LIBNAME=auxlib.a
BUILDDIR=build
@ -8,7 +8,7 @@ TESTDIR=testing
TESTBIN=test
lib:
$(CC) $(DEFAULTCFLAGS) -c $(SRCDIR)/*/*.c
$(CC) $(DEFAULTCCFLAGS) -c $(SRCDIR)/*/*.c
mkdir -p $(BUILDDIR)
mv *.o $(BUILDDIR)
@ -16,18 +16,17 @@ lib:
mkdir -p $(BINDIR)
ar rcs $(BINDIR)/$(LIBNAME) $(BUILDDIR)/*.o
test:
$(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(SRCDIR)/*/*.c -o $(TESTDIR)/$(TESTBIN) && \
$(CC) $(DEFAULTCCFLAGS) $(TESTDIR)/$(TESTBIN).c $(SRCDIR)/*/*.c -o $(TESTDIR)/$(TESTBIN) -lm && \
cd $(TESTDIR) && \
./$(TESTBIN) && \
rm $(TESTBIN)
test_static: lib
$(CC) $(DEFAULTCFLAGS) $(TESTDIR)/$(TESTBIN).c $(BINDIR)/$(LIBNAME) -static -o $(TESTDIR)/$(TESTBIN) && \
$(CC) $(DEFAULTCCFLAGS) $(TESTDIR)/$(TESTBIN).c $(BINDIR)/$(LIBNAME) -static -lm -o $(TESTDIR)/$(TESTBIN) && \
cd $(TESTDIR) && \
./$(TESTBIN) && \
rm $(TESTBIN)
clear:
clean:
rm -rf $(BUILDDIR) $(BINDIR) $(TESTDIR)/$(TESTBIN)

102
src/math/vector.c

@ -0,0 +1,102 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "vector.h"
#include <math.h>
// Calculate vector's length
long double vec2_len(vec2 vec) {
return sqrtl((long double) (vec.x*vec.x) + (long double) (vec.y*vec.y));
}
// Calculate vector's length
long double vec2f_len(vec2f vec) {
return sqrtl(vec.x*vec.x + vec.y*vec.y);
}
// Add 2 vectors together
vec2 vec2_add(vec2 a, vec2 b) {
return (vec2){a.x+b.x, a.y+b.y};
}
// Add 2 vectors together
vec2f vec2f_add(vec2f a, vec2f b) {
return (vec2f){a.x+b.x, a.y+b.y};
}
// Multiply 2 vectors the scalar way
long long int vec2_multiply_scalar(vec2 a, vec2 b) {
return a.x*b.x + a.y*b.y;
}
// Multiply 2 vectors the scalar way
long double vec2f_multiply_scalar(vec2f a, vec2f b) {
return a.x*b.x + a.y*b.y;
}
// Get cosinus of angle between 2 given vectors
long double vec2_angle(vec2 a, vec2 b) {
return vec2_multiply_scalar(a, b) / (vec2_len(a) * vec2_len(b));
}
// Get cosinus of angle between 2 given vectors
long double vec2f_angle(vec2f a, vec2f b) {
return vec2f_multiply_scalar(a, b) / (vec2f_len(a) * vec2f_len(b));
}
// Calculate vector's length
long double vec3_len(vec3 vec) {
return sqrt(
(long double) (vec.x*vec.x) +
(long double) (vec.y*vec.y) +
(long double) (vec.z*vec.z)
);
}
// Calculate vector's length
long double vec3f_len(vec3f vec) {
return sqrtl(
vec.x*vec.x +
vec.y*vec.y +
vec.z*vec.z
);
}
// Add 2 vectors together
vec3 vec3_add(vec3 a, vec3 b) {
return (vec3){a.x+b.x, a.y+b.y, a.z+b.z};
}
// Add 2 vectors together
vec3f vec3f_add(vec3f a, vec3f b) {
return (vec3f){a.x+b.x, a.y+b.y, a.z+b.z};
}
// Multiply 2 vectors the scalar way
long long int vec3_multiply_scalar(vec3 a, vec3 b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
// Multiply 2 vectors the scalar way
long double vec3f_multiply_scalar(vec3f a, vec3f b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
// Get cosinus of angle between 2 given vectors
long double vec3_angle(vec3 a, vec3 b) {
return vec3_multiply_scalar(a, b) / (vec3_len(a) * vec3_len(b));
}
// Get cosinus of angle between 2 given vectors
long double vec3f_angle(vec3f a, vec3f b) {
return vec3f_multiply_scalar(a, b) / (vec3f_len(a) * vec3f_len(b));
}

83
src/math/vector.h

@ -0,0 +1,83 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <math.h>
typedef struct vec2 {
long long int x;
long long int y;
} vec2;
typedef struct vec2f {
long double x;
long double y;
} vec2f;
// Calculate vector's length
long double vec2_len(vec2 vec);
// Calculate vector's length
long double vec2f_len(vec2f vec);
// Multiply 2 vectors the scalar way
long long int vec2_multiply_scalar(vec2 a, vec2 b);
// Multiply 2 vectors the scalar way
long double vec2f_multiply_scalar(vec2f a, vec2f b);
// Add 2 vectors together
vec2 vec2_add(vec2 a, vec2 b);
// Add 2 vectors together
vec2f vec2f_add(vec2f a, vec2f b);
// Get cosinus of angle between 2 given vectors
long double vec2_angle(vec2 a, vec2 b);
// Get cosinus of angle between 2 given vectors
long double vec2f_angle(vec2f a, vec2f b);
typedef struct vec3 {
long long int x;
long long int y;
long long int z;
} vec3;
typedef struct vec3f {
long double x;
long double y;
long double z;
} vec3f;
// Calculate vector's length
long double vec3_len(vec3 vec);
// Calculate vector's length
long double vec3f_len(vec3f vec);
// Add 2 vectors together
vec3 vec3_add(vec3 a, vec3 b);
// Add 2 vectors together
vec3f vec3f_add(vec3f a, vec3f b);
// Multiply 2 vectors the scalar way
long long int vec3_multiply_scalar(vec3 a, vec3 b);
// Multiply 2 vectors the scalar way
long double vec3f_multiply_scalar(vec3f a, vec3f b);
// Get cosinus of angle between 2 given vectors
long double vec3_angle(vec3 a, vec3 b);
// Get cosinus of angle between 2 given vectors
long double vec3f_angle(vec3f a, vec3f b);

50
testing/test.c

@ -22,6 +22,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#include "../src/endian/endian.h"
#include "../src/fs/fs.h"
#include "../src/bits/bits.h"
#include "../src/math/vector.h"
int test_rng() {
lcg(76);
@ -166,6 +167,47 @@ int test_bits() {
return EXIT_SUCCESS;
}
int test_vector() {
const long double threshold = 0.01;
vec2 vec2d = {2, 3};
vec2 vec2d_other = {3, 2};
long long int multiplied;
if (fabsl(vec2_len(vec2d) - 3.606f) > threshold) {
printf("[ERROR] Vector length calculation is wrong: expeted to be %f (+-%Lf); got %Lf\n",
3.606f, threshold, vec2_len(vec2d)
);
return EXIT_FAILURE;
}
multiplied = vec2_multiply_scalar(vec2d, vec2d_other);
if (multiplied != (vec2d.x*vec2d_other.x + vec2d.y*vec2d_other.y)) {
printf("[ERROR] Vector scalar multiplication is wrong\n");
return EXIT_FAILURE;
}
vec2d = (vec2){3, 4};
vec2d_other = (vec2){4, 3};
if (fabsl(vec2_angle(vec2d, vec2d_other) - 0.96f) > threshold) {
printf("[ERROR] Failed to calculate angle between 2 vectors: expected to be %f (+-%Lf); got %Lf\n",
0.96f, threshold, vec2_angle(vec2d, vec2d_other)
);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int test_math() {
if (test_vector() == EXIT_FAILURE) {
printf("[ERROR] Vector test failed\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main() {
// rng
printf("[INFO] Testing rng...\n");
@ -207,5 +249,13 @@ int main() {
printf("[INFO] Bits test passed\n\n");
}
// math
printf("[INFO] Testing math...\n");
if (test_math() == EXIT_FAILURE) {
printf("[INFO] Math test failed\n\n");
} else {
printf("[INFO] Math test passed\n\n");
}
return EXIT_SUCCESS;
}
Loading…
Cancel
Save