This is: CODING RUN!!!
Test out an idea in a small amount of time to distract yourself from your business.
While learning some Math I thought about the difficulty of an easy glm-alike Vector Datatrype in C.
20 minutes later I got this:
#include <stdlib.h> #include <stdio.h> #include <math.h> typedef struct{ union{ double x; double r; }; union{ double y; double g; }; union{ double z; double b; }; }Vec3; Vec3 add(Vec3 a, Vec3 b); double scalarProduct(Vec3 a, Vec3 b); double absVec(Vec3 v); int main(int argc, char* argv[]) { Vec3 a; a.x = 2; a.y = -4; a.z = 0; Vec3 b; b.x = 3; b.y = 2; b.z = 5; Vec3 c = add(a,b); double sp = scalarProduct(a,b); double absA = absVec(a); printf("a + b = (%.2f %.2f %.2f)T\n", c.x, c.y, c.z); printf("<a,b> = %.2f\n", sp); printf("|a| = %.2f\n", absA); return EXIT_SUCCESS; } Vec3 add(Vec3 a, Vec3 b) { Vec3 result; result.x = a.x + b.x; result.y = a.y + b.y; result.z = a.z + b.z; return result; } double scalarProduct(Vec3 a, Vec3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; } double absVec(Vec3 v) { return sqrt(v.x*v.x + v.y*v.y + v.z*v.z); }
A pretty neat quick n dirty solution, isn’t it?
Compile it with gcc -lm vec3.c
EDIT [22.06.2018]
I just added even more functions and a “Test suite”. Rather than a single main-implementation-file, everything is now in a static lib which can be linked against your ready-to-deploy application.
Repository: https://bitbucket.org/evilc00kie/vec3lib/src
Hits: 325