X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg%2Fvg_m.h;h=577138fb12e575698d1c941e15958f05a898beeb;hb=3363633178b1eea582304742ad1202487af0feb1;hp=de9cb6dd3713dc837f12b726e678634ab614d13d;hpb=bd3188f0fe52c9231f79af85d6cfaef2576b9f83;p=fishladder.git diff --git a/vg/vg_m.h b/vg/vg_m.h index de9cb6d..577138f 100644 --- a/vg/vg_m.h +++ b/vg/vg_m.h @@ -3,7 +3,8 @@ // Util // ================================================================================================================== -#define VG_PIf 3.14159265358979323846264338327950288f +#define VG_PIf 3.14159265358979323846264338327950288f +#define VG_TAUf 6.28318530717958647692528676655900576f // Simple min/max replacements static inline float vg_minf( float a, float b ) @@ -16,6 +17,15 @@ static inline float vg_maxf( float a, float b ) return a > b? a: b; } +static inline float vg_clampf( float a, float min, float max ) +{ + return vg_minf( max, vg_maxf( a, min ) ); +} + +#define VG_MIN( A, B ) ((A)<(B)?(A):(B)) +#define VG_MAX( A, B ) ((A)>(B)?(A):(B)) + +// Hopefully deprecate this!! static inline int vg_min( int a, int b ) { return a < b? a: b; @@ -40,6 +50,26 @@ static inline void v2_copy( v2f a, v2f b ) b[0] = a[0]; b[1] = a[1]; } +static inline void v2i_copy( v2i a, v2i b ) +{ + b[0] = a[0]; b[1] = a[1]; +} + +static inline int v2i_eq( v2i a, v2i b ) +{ + return ((a[0] == b[0]) && (a[1] == b[1])); +} + +static inline void v2i_add( v2i a, v2i b, v2i d ) +{ + d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; +} + +static inline void v2i_sub( v2i a, v2i b, v2i d ) +{ + d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; +} + static inline void v2_minv( v2f a, v2f b, v2f dest ) { dest[0] = vg_minf(a[0], b[0]); @@ -72,9 +102,16 @@ static inline void v2_muls( v2f a, float s, v2f d ) d[0] = a[0]*s; d[1] = a[1]*s; } +static inline void v2_divs( v2f a, float s, v2f d ) +{ + d[0] = a[0]/s; d[1] = a[1]/s; +} + + static inline void v2_mul( v2f a, v2f b, v2f d ) { - d[0] = a[0]*b[0]; d[1] = a[1]*b[1]; + d[0] = a[0]*b[0]; + d[1] = a[1]*b[1]; } static inline void v2_div( v2f a, v2f b, v2f d ) @@ -82,6 +119,46 @@ static inline void v2_div( v2f a, v2f b, v2f d ) d[0] = a[0]/b[0]; d[1] = a[1]/b[1]; } +static inline void v2_muladd( v2f a, v2f b, v2f s, v2f d ) +{ + d[0] = a[0]+b[0]*s[0]; + d[1] = a[1]+b[1]*s[1]; +} + +static inline void v2_muladds( v2f a, v2f b, float s, v2f d ) +{ + d[0] = a[0]+b[0]*s; + d[1] = a[1]+b[1]*s; +} + +static inline float v2_length2( v2f a ) +{ + return a[0]*a[0] + a[1]*a[1]; +} + +static inline float v2_length( v2f a ) +{ + return sqrtf( v2_length2( a ) ); +} + +static inline float v2_dist2( v2f a, v2f b ) +{ + v2f delta; + v2_sub( a, b, delta ); + return v2_length2( delta ); +} + +static inline float v2_dist( v2f a, v2f b ) +{ + return sqrtf( v2_dist2( a, b ) ); +} + +static inline void v2_lerp( v2f a, v2f b, float t, v2f d ) +{ + d[0] = a[0] + t*(b[0]-a[0]); + d[1] = a[1] + t*(b[1]-a[1]); +} + // Vector 3 // ================================================================================================================== @@ -169,7 +246,7 @@ static inline void v3_normalize( v3f a ) v3_muls( a, 1.f / v3_length( a ), a ); } -static inline float csr_lerpf( float a, float b, float t ) +static inline float vg_lerpf( float a, float b, float t ) { return a + t*(b-a); } @@ -225,6 +302,40 @@ static inline void v4_zero( v4f a ) a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f; } +// Matrix 2x2 +// =========================================================================================================== + +#define M2X2_INDENTIY {{1.0f, 0.0f, }, \ + { 0.0f, 1.0f, }} + +#define M2X2_ZERO {{0.0f, 0.0f, }, \ + { 0.0f, 0.0f, }} + +static inline void m2x2_copy( m2x2f a, m2x2f b ) +{ + v2_copy( a[0], b[0] ); + v2_copy( a[1], b[1] ); +} + +static inline void m2x2_identity( m2x2f a ) +{ + m2x2f id = M2X2_INDENTIY; + m2x2_copy( id, a ); +} + +static inline void m2x2_create_rotation( m2x2f a, float theta ) +{ + float s, c; + + s = sinf( theta ); + c = cosf( theta ); + + a[0][0] = c; + a[0][1] = -s; + a[1][0] = s; + a[1][1] = c; +} + // Matrix 3x3 //======================================================================================================