|
@@ -2,6 +2,7 @@
|
|
|
#define MATH_H
|
|
#define MATH_H
|
|
|
|
|
|
|
|
#include "stdint.h"
|
|
#include "stdint.h"
|
|
|
|
|
+#include <math.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
int x;
|
|
int x;
|
|
@@ -19,6 +20,12 @@ typedef struct {
|
|
|
double z;
|
|
double z;
|
|
|
} RI_vector_3f;
|
|
} RI_vector_3f;
|
|
|
|
|
|
|
|
|
|
+typedef struct {
|
|
|
|
|
+ int x;
|
|
|
|
|
+ int y;
|
|
|
|
|
+ int z;
|
|
|
|
|
+} RI_vector_3;
|
|
|
|
|
+
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
double w;
|
|
double w;
|
|
|
double x;
|
|
double x;
|
|
@@ -33,6 +40,13 @@ void vector_2f_times(RI_vector_2f *vector, double value){
|
|
|
vector->y *= value;
|
|
vector->y *= value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// value-wise multiplacation.
|
|
|
|
|
+// multiply the whole vector by 1 value
|
|
|
|
|
+void vector_2_times(RI_vector_2 *vector, double value){
|
|
|
|
|
+ vector->x *= value;
|
|
|
|
|
+ vector->y *= value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// value-wise multiplacation.
|
|
// value-wise multiplacation.
|
|
|
// multiply the whole vector by 1 value
|
|
// multiply the whole vector by 1 value
|
|
|
void vector_3f_times(RI_vector_3f *vector, double value){
|
|
void vector_3f_times(RI_vector_3f *vector, double value){
|
|
@@ -41,6 +55,14 @@ void vector_3f_times(RI_vector_3f *vector, double value){
|
|
|
vector->z *= value;
|
|
vector->z *= value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// value-wise multiplacation.
|
|
|
|
|
+// multiply the whole vector by 1 value
|
|
|
|
|
+void vector_3_times(RI_vector_3 *vector, double value){
|
|
|
|
|
+ vector->x *= value;
|
|
|
|
|
+ vector->y *= value;
|
|
|
|
|
+ vector->z *= value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// hadamard multiplacation.
|
|
// hadamard multiplacation.
|
|
|
// multiply each value of one vector with the matching one on the other vector
|
|
// multiply each value of one vector with the matching one on the other vector
|
|
|
void vector_3f_hadamard(RI_vector_3f *multiplicand, RI_vector_3f multiplicator){
|
|
void vector_3f_hadamard(RI_vector_3f *multiplicand, RI_vector_3f multiplicator){
|
|
@@ -56,6 +78,13 @@ void vector_2f_element_wise_add(RI_vector_2f *addend_a, RI_vector_2f addend_b){
|
|
|
addend_a->y += addend_b.y;
|
|
addend_a->y += addend_b.y;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// "hadamard" addition.
|
|
|
|
|
+// add each value of one vector with the matching one on the other vector
|
|
|
|
|
+void vector_2_element_wise_add(RI_vector_2 *addend_a, RI_vector_2 addend_b){
|
|
|
|
|
+ addend_a->x += addend_b.x;
|
|
|
|
|
+ addend_a->y += addend_b.y;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// "hadamard" addition.
|
|
// "hadamard" addition.
|
|
|
// add each value of one vector with the matching one on the other vector
|
|
// add each value of one vector with the matching one on the other vector
|
|
|
void vector_3f_element_wise_add(RI_vector_3f *addend_a, RI_vector_3f addend_b){
|
|
void vector_3f_element_wise_add(RI_vector_3f *addend_a, RI_vector_3f addend_b){
|
|
@@ -64,6 +93,14 @@ void vector_3f_element_wise_add(RI_vector_3f *addend_a, RI_vector_3f addend_b){
|
|
|
addend_a->z += addend_b.z;
|
|
addend_a->z += addend_b.z;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// "hadamard" addition.
|
|
|
|
|
+// add each value of one vector with the matching one on the other vector
|
|
|
|
|
+void vector_3_element_wise_add(RI_vector_3 *addend_a, RI_vector_3 addend_b){
|
|
|
|
|
+ addend_a->x += addend_b.x;
|
|
|
|
|
+ addend_a->y += addend_b.y;
|
|
|
|
|
+ addend_a->z += addend_b.z;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// "hadamard" subtraction.
|
|
// "hadamard" subtraction.
|
|
|
// subtraction each value of one vector with the matching one on the other vector
|
|
// subtraction each value of one vector with the matching one on the other vector
|
|
|
void vector_3f_element_wise_subtract(RI_vector_3f *minuend, RI_vector_3f subtrahend){
|
|
void vector_3f_element_wise_subtract(RI_vector_3f *minuend, RI_vector_3f subtrahend){
|
|
@@ -114,7 +151,22 @@ void vector_2f_lerp(RI_vector_2f vector_a, RI_vector_2f vector_b, RI_vector_2f *
|
|
|
vector_2f_element_wise_add(result, vector_b);
|
|
vector_2f_element_wise_add(result, vector_b);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// beziate between 2 vectors
|
|
|
|
|
|
|
+// linear interpolate between 2 vectors
|
|
|
|
|
+void vector_2_lerp(RI_vector_2 vector_a, RI_vector_2 vector_b, RI_vector_2 *result, double w1){
|
|
|
|
|
+ double w0 = 1.0 - w1;
|
|
|
|
|
+
|
|
|
|
|
+ vector_2_times(result, 0);
|
|
|
|
|
+
|
|
|
|
|
+ vector_2_times(&vector_a, w0);
|
|
|
|
|
+ vector_2_times(&vector_b, w1);
|
|
|
|
|
+
|
|
|
|
|
+ vector_2_element_wise_add(result, vector_a);
|
|
|
|
|
+ vector_2_element_wise_add(result, vector_b);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// beziate between 2 vectors.
|
|
|
|
|
+// A & C: end points of the line
|
|
|
|
|
+// B: point that determines the curve; off the line
|
|
|
void vector_2f_bezier_interpolate(RI_vector_2f vector_a, RI_vector_2f vector_b, RI_vector_2f vector_c, RI_vector_2f *result, double w1){
|
|
void vector_2f_bezier_interpolate(RI_vector_2f vector_a, RI_vector_2f vector_b, RI_vector_2f vector_c, RI_vector_2f *result, double w1){
|
|
|
double w0 = 1.0 - w1;
|
|
double w0 = 1.0 - w1;
|
|
|
|
|
|
|
@@ -124,6 +176,18 @@ void vector_2f_bezier_interpolate(RI_vector_2f vector_a, RI_vector_2f vector_b,
|
|
|
vector_2f_lerp(vector_b, vector_c, result, w1);
|
|
vector_2f_lerp(vector_b, vector_c, result, w1);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// beziate between 2 vectors.
|
|
|
|
|
+// A & C: end points of the line
|
|
|
|
|
+// B: point that determines the curve; off the line
|
|
|
|
|
+void vector_2_bezier_interpolate(RI_vector_2 vector_a, RI_vector_2 vector_b, RI_vector_2 vector_c, RI_vector_2 *result, double w1){
|
|
|
|
|
+ double w0 = 1.0 - w1;
|
|
|
|
|
+
|
|
|
|
|
+ vector_2_lerp(vector_a, vector_b, &vector_b, w1); // this works because the first vector b is a copy and the second is a reference
|
|
|
|
|
+ vector_2_lerp(vector_b, vector_c, &vector_c, w1);
|
|
|
|
|
+
|
|
|
|
|
+ vector_2_lerp(vector_b, vector_c, result, w1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// linear interpolate between 2 vectors
|
|
// linear interpolate between 2 vectors
|
|
|
void vector_3f_lerp(RI_vector_3f vector_a, RI_vector_3f vector_b, RI_vector_3f *result, double w1){
|
|
void vector_3f_lerp(RI_vector_3f vector_a, RI_vector_3f vector_b, RI_vector_3f *result, double w1){
|
|
|
double w0 = 1.0 - w1;
|
|
double w0 = 1.0 - w1;
|
|
@@ -137,5 +201,30 @@ void vector_3f_lerp(RI_vector_3f vector_a, RI_vector_3f vector_b, RI_vector_3f *
|
|
|
vector_3f_element_wise_add(result, vector_b);
|
|
vector_3f_element_wise_add(result, vector_b);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// linear interpolate between 2 vectors
|
|
|
|
|
+void vector_3_lerp(RI_vector_3 vector_a, RI_vector_3 vector_b, RI_vector_3 *result, double w1){
|
|
|
|
|
+ double w0 = 1.0 - w1;
|
|
|
|
|
+
|
|
|
|
|
+ vector_3_times(result, 0);
|
|
|
|
|
+
|
|
|
|
|
+ vector_3_times(&vector_a, w0);
|
|
|
|
|
+ vector_3_times(&vector_b, w1);
|
|
|
|
|
+
|
|
|
|
|
+ vector_3_element_wise_add(result, vector_a);
|
|
|
|
|
+ vector_3_element_wise_add(result, vector_b);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// returns the distance between 2 points
|
|
|
|
|
+int distance_2(RI_vector_2 a, RI_vector_2 b){
|
|
|
|
|
+ return (int)sqrtf((float)((float)(a.x - b.x) * (float)(a.x - b.x)) + (float)((float)(a.y - b.y) * (float)(a.y - b.y)));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+RI_vector_2f v2_to_2f(RI_vector_2 v){
|
|
|
|
|
+ return (RI_vector_2f){v.x, v.y};
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+RI_vector_2 v2f_to_2(RI_vector_2f v){
|
|
|
|
|
+ return (RI_vector_2){v.x, v.y};
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|