|
|
|
@ -5,15 +5,13 @@
@@ -5,15 +5,13 @@
|
|
|
|
|
* Creates a new vec* struct with the given number of dimenions. |
|
|
|
|
* All elements are set to 0. |
|
|
|
|
*/ |
|
|
|
|
struct vec* new_vec(int num_dimensions){ |
|
|
|
|
struct vec* new_vector = malloc(1 * sizeof(struct vec)); |
|
|
|
|
struct vec*
|
|
|
|
|
new_vec(int num_dimensions) |
|
|
|
|
{ |
|
|
|
|
struct vec* new_vector = calloc(1,sizeof(struct vec)); |
|
|
|
|
new_vector->dimension = num_dimensions; |
|
|
|
|
|
|
|
|
|
double* elements = malloc(num_dimensions * sizeof(double)); |
|
|
|
|
new_vector->elements = elements; |
|
|
|
|
for (int i = 0; i < num_dimensions; i++) { |
|
|
|
|
elements[i] = 0; |
|
|
|
|
} |
|
|
|
|
new_vector->elements = calloc(num_dimensions, sizeof(double)); |
|
|
|
|
|
|
|
|
|
return new_vector; |
|
|
|
|
} |
|
|
|
@ -21,7 +19,9 @@ struct vec* new_vec(int num_dimensions){
@@ -21,7 +19,9 @@ struct vec* new_vec(int num_dimensions){
|
|
|
|
|
/**
|
|
|
|
|
* Frees all memory related to the given vec. |
|
|
|
|
*/ |
|
|
|
|
void free_vec(struct vec* a) { |
|
|
|
|
void
|
|
|
|
|
free_vec(struct vec* a)
|
|
|
|
|
{ |
|
|
|
|
free(a->elements); |
|
|
|
|
free(a); |
|
|
|
|
} |
|
|
|
@ -29,7 +29,9 @@ void free_vec(struct vec* a) {
@@ -29,7 +29,9 @@ void free_vec(struct vec* a) {
|
|
|
|
|
/**
|
|
|
|
|
* Takes 2 doubles and creates an vec of 2 dimensions. |
|
|
|
|
*/ |
|
|
|
|
struct vec* new_vec2(double x, double y) { |
|
|
|
|
struct vec*
|
|
|
|
|
new_vec2(double x, double y)
|
|
|
|
|
{ |
|
|
|
|
struct vec* new_vector = new_vec(2); |
|
|
|
|
new_vector->elements[0] = x; |
|
|
|
|
new_vector->elements[1] = y; |
|
|
|
@ -40,7 +42,9 @@ struct vec* new_vec2(double x, double y) {
@@ -40,7 +42,9 @@ struct vec* new_vec2(double x, double y) {
|
|
|
|
|
/**
|
|
|
|
|
* Takes 3 doubles and creates an vec of 3 dimensions. |
|
|
|
|
*/ |
|
|
|
|
struct vec* new_vec3(double x, double y, double z) { |
|
|
|
|
struct vec*
|
|
|
|
|
new_vec3(double x, double y, double z)
|
|
|
|
|
{ |
|
|
|
|
struct vec* new_vector = new_vec(3); |
|
|
|
|
new_vector->elements[0] = x; |
|
|
|
|
new_vector->elements[1] = y; |
|
|
|
@ -52,7 +56,9 @@ struct vec* new_vec3(double x, double y, double z) {
@@ -52,7 +56,9 @@ struct vec* new_vec3(double x, double y, double z) {
|
|
|
|
|
/**
|
|
|
|
|
* Takes 4 doubles and creates an vec of 4 dimensions. |
|
|
|
|
*/ |
|
|
|
|
struct vec* new_vec4(double w, double x, double y, double z) { |
|
|
|
|
struct vec*
|
|
|
|
|
new_vec4(double w, double x, double y, double z)
|
|
|
|
|
{ |
|
|
|
|
struct vec* new_vector = new_vec(2); |
|
|
|
|
new_vector->elements[0] = w; |
|
|
|
|
new_vector->elements[1] = x; |
|
|
|
@ -65,9 +71,11 @@ struct vec* new_vec4(double w, double x, double y, double z) {
@@ -65,9 +71,11 @@ struct vec* new_vec4(double w, double x, double y, double z) {
|
|
|
|
|
/**
|
|
|
|
|
* Adds together two vecs and returns a reference to the summed vec. |
|
|
|
|
*/ |
|
|
|
|
struct vec* add_vec(struct vec* a, struct vec* b) { |
|
|
|
|
struct vec*
|
|
|
|
|
add_vec(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
int smallest_dimension = a->dimension < b->dimension ? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension ? a->dimension : b->dimension; |
|
|
|
|
|
|
|
|
|
// Perform addition up to where the dimensions of both vectors are equal.
|
|
|
|
|
struct vec* result = new_vec(a->dimension); |
|
|
|
@ -90,7 +98,9 @@ struct vec* add_vec(struct vec* a, struct vec* b) {
@@ -90,7 +98,9 @@ struct vec* add_vec(struct vec* a, struct vec* b) {
|
|
|
|
|
/**
|
|
|
|
|
* Add vec a to vec b and return the pointer to vec a. |
|
|
|
|
*/ |
|
|
|
|
struct vec* add_vec_ip(struct vec* a, struct vec* b) { |
|
|
|
|
struct vec*
|
|
|
|
|
add_vec_ip(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
int smallest_dimension = a->dimension < b->dimension ? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension? a->dimension : b->dimension; |
|
|
|
|
|
|
|
|
@ -114,7 +124,9 @@ struct vec* add_vec_ip(struct vec* a, struct vec* b) {
@@ -114,7 +124,9 @@ struct vec* add_vec_ip(struct vec* a, struct vec* b) {
|
|
|
|
|
/**
|
|
|
|
|
* Subtracts vec b from vec a and returns a reference to the difference vec. |
|
|
|
|
*/ |
|
|
|
|
struct vec* subtract_vec(struct vec* a, struct vec* b) { |
|
|
|
|
struct vec*
|
|
|
|
|
subtract_vec(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
int smallest_dimension = a->dimension < b->dimension ? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension? a->dimension : b->dimension; |
|
|
|
|
|
|
|
|
@ -139,7 +151,9 @@ struct vec* subtract_vec(struct vec* a, struct vec* b) {
@@ -139,7 +151,9 @@ struct vec* subtract_vec(struct vec* a, struct vec* b) {
|
|
|
|
|
/**
|
|
|
|
|
* Calculates the magnitude of vec a. |
|
|
|
|
*/ |
|
|
|
|
double magnitude_vec(struct vec* a) { |
|
|
|
|
double
|
|
|
|
|
magnitude_vec(struct vec* a)
|
|
|
|
|
{ |
|
|
|
|
double sum_of_squares = 0; |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
sum_of_squares += pow(a->elements[i], 2.0); |
|
|
|
@ -151,7 +165,9 @@ double magnitude_vec(struct vec* a) {
@@ -151,7 +165,9 @@ double magnitude_vec(struct vec* a) {
|
|
|
|
|
/**
|
|
|
|
|
* Normalise the given vec by dividing all components by the magnitude. |
|
|
|
|
*/ |
|
|
|
|
struct vec* normalise_vec(struct vec* a) { |
|
|
|
|
struct vec*
|
|
|
|
|
normalise_vec(struct vec* a)
|
|
|
|
|
{ |
|
|
|
|
struct vec* result = new_vec(a->dimension); |
|
|
|
|
double magnitude = magnitude_vec(a); |
|
|
|
|
for (int i = 0; i < a-> dimension; i++) { |
|
|
|
@ -164,7 +180,9 @@ struct vec* normalise_vec(struct vec* a) {
@@ -164,7 +180,9 @@ struct vec* normalise_vec(struct vec* a) {
|
|
|
|
|
/**
|
|
|
|
|
* Normalise the given vec and store in vec a. |
|
|
|
|
*/ |
|
|
|
|
struct vec* normalise_vec_ip(struct vec* a) { |
|
|
|
|
struct vec*
|
|
|
|
|
normalise_vec_ip(struct vec* a)
|
|
|
|
|
{ |
|
|
|
|
double magnitude = magnitude_vec(a); |
|
|
|
|
for (int i = 0; i < a-> dimension; i++) { |
|
|
|
|
a->elements[i] = a->elements[i] / magnitude; |
|
|
|
@ -177,7 +195,9 @@ struct vec* normalise_vec_ip(struct vec* a) {
@@ -177,7 +195,9 @@ struct vec* normalise_vec_ip(struct vec* a) {
|
|
|
|
|
* Calculate the dot product of vec a and vec b and return a reference to |
|
|
|
|
* the result. |
|
|
|
|
*/ |
|
|
|
|
struct vec* dot_product_vec(struct vec* a, struct vec* b) { |
|
|
|
|
struct vec*
|
|
|
|
|
dot_product_vec(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
struct vec* result = new_vec(a->dimension); |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
result->elements[i] = a->elements[i] * b->elements[i]; |
|
|
|
@ -189,7 +209,9 @@ struct vec* dot_product_vec(struct vec* a, struct vec* b) {
@@ -189,7 +209,9 @@ struct vec* dot_product_vec(struct vec* a, struct vec* b) {
|
|
|
|
|
/**
|
|
|
|
|
* Multiply vec a by a scalarFactor and return a new vec with the result. |
|
|
|
|
*/ |
|
|
|
|
struct vec* scalar_multiply_vec(struct vec* a, double scalarFactor) { |
|
|
|
|
struct vec*
|
|
|
|
|
scalar_multiply_vec(struct vec* a, double scalarFactor)
|
|
|
|
|
{ |
|
|
|
|
struct vec* result = new_vec(a->dimension); |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
result->elements[i] = a->elements[i] * scalarFactor; |
|
|
|
@ -202,7 +224,9 @@ struct vec* scalar_multiply_vec(struct vec* a, double scalarFactor) {
@@ -202,7 +224,9 @@ struct vec* scalar_multiply_vec(struct vec* a, double scalarFactor) {
|
|
|
|
|
* Calculate the distance between vec a and vec b by summing the square of |
|
|
|
|
* the differences of each component. |
|
|
|
|
*/ |
|
|
|
|
double distance_vec(struct vec* a, struct vec* b) { |
|
|
|
|
double
|
|
|
|
|
distance_vec(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
double sum_of_differences = 0; |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
sum_of_differences = pow(a->elements[i] - b->elements[i], 2); |
|
|
|
|