This library only contains 2 files, vector.c and vector.h, so compiling is stright forward. If you want, you can use the provided makefile by running the following commands in the project root.
mkdir build
make
A libvector.a file will be generated in the build directory.
The Vector struct holds 3 doubles and 1 int: x, y, z, and is_2d. All vectors (including 2d ones) have the z attribute, but the z attribute will be ignored in all vector operations in 2d vectors. The Point struct is equivalent to the Vector struct, and can be used in replace of the vector struct for all vector functions (although some vector operations such as getting the magnitude doesn't really make sense to be applied on points). The code below demonstrates all the functionality of this library.
Vector v1, v2, v3;
Point p;
// initialize the vectors to be 3d
vector_init_3d(&v1, 1, 2, 3);
vector_init_3d(&v2, 4, 5, 6);
// OR, initialize the vectors to be 2d
vector_init_2d(&v1, 1, 2);
vector_init_2d(&v2, 3, 4);
// OR, make a vector equal another (no initialization of v3 has to be done)
v3 = v2;
/*
NOTE: for point initialization, just use vector_init
this applies to all other functions as well
*/
// initialize the point to be 2d
vector_init_2d(&p, 1, 2);
// initialize the point to be 3d
vector_init_3d(&p, 1, 2, 3);
// gets the squared magnitude of the vector
double length_sq = vector_get_mag_sq(&v1);
// gets the actual magnitude of the vector
double length = vector_get_mag(&v1);
// sets the magnitude of the vector to 10, doesn't do anything if the magnitude is already 0
vector_set_mag(&v1, 10);
// this function is functionally equivalent to vector_set_mag(v, 1)
vector_normalize(&v1);
// gets the distance between two vectors
double distance = vector_get_dist_btw(&v1, &v2);
// gets the squared distance between two vectors
double distance_sq = vector_get_dist_sq_btw(&v1, &v2);
// finds the dot product between two vectors
double dot_product = vector_dot(&v1, &v2);
// rotates the vector 0.2 radians around the x axis, 0.9 around the y, and -0.7 around the z (+ is anticlockwise, - is clockwise)
vector_rotate(&v1, 0.2, 0.9, -0.7);
// add the two vectors, this function changes the first vector
vector_add(&v1, &v2);
// adds 3 to all x, y, z attributes
vector_add_const(&v1, 3);
// subtract the two vectors, this function changes the first vector
vector_subtract(&v1, &v2);
// subtracts 5 to all x, y, z attributes
vector_subtract_const(&v1, 5);
// gets the angle in radians between the two vectors
double angle = vector_get_angle_btw(&v1, &v2);
// returns the length of the vector projected from v1 onto v2
double proj_length = vector_get_scalar_proj(&v1, &v2);
// sets v3 to the vector that is v1 projected onto v2 (note, the destination vector can also be v1 and v2)
vector_proj(&v1, &v2, &v3);
// multiplies all x, y, z attributes by 2
vector_scale(&v1, 2);
- https://www.youtube.com/watch?v=DOMg0lXWatM&t=105s
- https://en.wikipedia.org/wiki/Rotation_matrix
- https://youtu.be/fNk_zzaMoSs
- https://www.youtube.com/watch?v=mWJkvxQXIa8&list=PLRqwX-V7Uu6ZwSmtE13iJBcoI-r4y7iEc
- https://www.youtube.com/watch?v=bKEaK7WNLzM&list=PLRqwX-V7Uu6ZV4yEcW3uDwOgGXKUUsPOM&index=10
- https://natureofcode.com/book/chapter-1-vectors/