From 87915532ee904ecbb1547369f6be259896193368 Mon Sep 17 00:00:00 2001 From: luc Date: Fri, 3 Apr 2020 01:59:56 +0200 Subject: [PATCH] add integer vector --- sweet_math.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++--- sweet_math.h | 18 ++++++++ sweet_types.h | 17 ++++++++ 3 files changed, 146 insertions(+), 6 deletions(-) diff --git a/sweet_math.c b/sweet_math.c index ab8ce62..446b390 100755 --- a/sweet_math.c +++ b/sweet_math.c @@ -24,6 +24,16 @@ #include "sweet_types.h" +void +random_float_numbers(float * array, int n) +{ + int i; + for (i = 0; i < n; i++) + { + array[i] = (float)rand() / (float)RAND_MAX; + } +} + vec2 vec2_zero () { @@ -82,7 +92,8 @@ vec4_new_4f (float a, float b, float c, float d) return v; } -vec2 vec2_new_v3(vec3 w) +vec2 +vec2_new_v3(vec3 w) { vec2 v; v.x = w.x; @@ -90,7 +101,8 @@ vec2 vec2_new_v3(vec3 w) return v; } -vec3 vec3_new_v2_1f(vec2 w, float z) +vec3 +vec3_new_v2_1f(vec2 w, float z) { vec3 v; v.x = w.x; @@ -99,7 +111,8 @@ vec3 vec3_new_v2_1f(vec2 w, float z) return v; } -vec3 vec3_new_v4(vec4 w) +vec3 +vec3_new_v4(vec4 w) { vec3 v; v.x = w.x; @@ -108,7 +121,8 @@ vec3 vec3_new_v4(vec4 w) return v; } -vec4 vec4_new_v2_2f(vec2 u, float z, float w) +vec4 +vec4_new_v2_2f(vec2 u, float z, float w) { vec4 v; v.x = u.x; @@ -118,7 +132,8 @@ vec4 vec4_new_v2_2f(vec2 u, float z, float w) return v; } -vec4 vec4_new_v3_1f(vec3 u, float w) +vec4 +vec4_new_v3_1f(vec3 u, float w) { vec4 v; v.x = u.x; @@ -128,7 +143,8 @@ vec4 vec4_new_v3_1f(vec3 u, float w) return v; } -vec4 vec4_new_v4 (vec4 v) +vec4 +vec4_new_v4 (vec4 v) { return v; } @@ -328,6 +344,95 @@ vec4_normalize (vec4 v) return nv; } +vec2i +vec2_int (vec2 v) +{ + vec2i u; + u.x = (int)(v.x > 0.0 ? v.x : v.x - 1); + u.y = (int)(v.y > 0.0 ? v.y : v.y - 1); + return u; +} + +vec3i +vec3_int (vec3 v) +{ + vec3i u; + u.x = (int)(v.x > 0.0 ? v.x : v.x - 1); + u.y = (int)(v.y > 0.0 ? v.y : v.y - 1); + u.z = (int)(v.z > 0.0 ? v.z : v.z - 1); + return u; +} + +vec4i +vec4_int (vec4 v) +{ + vec4i u; + u.x = (int)(v.x > 0.0 ? v.x : v.x - 1); + u.y = (int)(v.y > 0.0 ? v.y : v.y - 1); + u.z = (int)(v.z > 0.0 ? v.z : v.z - 1); + u.w = (int)(v.w > 0.0 ? v.w : v.w - 1); + return u; +} + +vec2i +vec2i_new (int x, int y) +{ + vec2i v; + v.x = x; + v.y = y; + return v; +} + +vec3i +vec3i_new (int x, int y, int z) +{ + vec3i v; + v.x = x; + v.y = y; + v.z = z; + return v; +} + +vec4i +vec4i_new (int x, int y, int z, int w) +{ + vec4i v; + v.x = x; + v.y = y; + v.z = z; + v.w = w; + return v; +} + +vec2i +vec2i_add (vec2i v1, vec2i v2) +{ + v1.x += v2.x; + v1.y += v2.y; + + return v1; +} + +vec3i +vec3i_add (vec3i v1, vec3i v2) +{ + v1.x += v2.x; + v1.y += v2.y; + v1.z += v2.z; + + return v1; +} + +vec4i +vec4i_add (vec4i v1, vec4i v2) +{ + v1.x += v2.x; + v1.y += v2.y; + v1.z += v2.z; + v1.w += v2.w; + + return v1; +} vec2 vec2_scale (vec2 v, float s) diff --git a/sweet_math.h b/sweet_math.h index 59081ec..89a6a24 100755 --- a/sweet_math.h +++ b/sweet_math.h @@ -58,6 +58,9 @@ #define max(a, b) (a >= b ? a : b) #define min(a, b) (a <= b ? a : b) +/* */ +void random_float_numbers(float * array, int n); + /* Vector */ vec2 vec2_zero(); @@ -205,6 +208,21 @@ vec2 vec2_product (vec2 vector1, vec2 vector2); vec3 vec3_product (vec3 vector1, vec3 vector2); vec4 vec4_product (vec4 vector1, vec4 vector2); +/* Integer part */ +vec2i vec2_int (vec2 v); +vec3i vec3_int (vec3 v); +vec4i vec4_int (vec4 v); + +/* Integer vector builder */ +vec2i vec2i_new (int x, int y); +vec3i vec3i_new (int x, int y, int z); +vec4i vec4i_new (int x, int y, int z, int w); + +/* Integer vector addition */ +vec2i vec2i_add (vec2i vector1, vec2i vector2); +vec3i vec3i_add (vec3i vector1, vec3i vector2); +vec4i vec4i_add (vec4i vector1, vec4i vector2); + /* Quaternion */ quat quat_new (float w, float x, float y, float z); quat quat_rotation (float angle, float x, float y, float z); diff --git a/sweet_types.h b/sweet_types.h index 3ac0423..d9eba91 100755 --- a/sweet_types.h +++ b/sweet_types.h @@ -50,6 +50,23 @@ typedef struct vec2 { float y; } vec2; +typedef struct vec4i { + int x; + int y; + int z; + int w; +} vec4i; + +typedef struct vec3i { + int x; + int y; + int z; +} vec3i; + +typedef struct vec2i { + int x; + int y; +} vec2i; typedef struct complex { float r;