#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define TAU 6.28318530717958647
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint32_t b32;
typedef float f32;
typedef double f64;
typedef uintptr_t ptr;
#define VectorOverloadDefine(Type,Syntax) \
inline v2##Type \
operator Syntax(v2##Type a, v2##Type b) \
{ \
v2##Type Result; \
Result.x = a.x Syntax b.x; \
Result.y = a.y Syntax b.y; \
return(Result); \
} \
inline void \
operator Syntax##=(v2##Type &a, v2##Type b) \
{ \
a = a Syntax b; \
} \
inline v3##Type \
operator Syntax(v3##Type a, v3##Type b) \
{ \
v3##Type Result; \
Result.x = a.x Syntax b.x; \
Result.y = a.y Syntax b.y; \
Result.z = a.z Syntax b.z; \
return(Result); \
} \
inline void \
operator Syntax##=(v3##Type &a, v3##Type b) \
{ \
a = a Syntax b; \
} \
inline v4##Type \
operator Syntax(v4##Type a, v4##Type b) \
{ \
v4##Type Result; \
Result.x = a.x Syntax b.x; \
Result.y = a.y Syntax b.y; \
Result.z = a.z Syntax b.z; \
Result.w = a.w Syntax b.w; \
return(Result); \
} \
inline void \
operator Syntax##=(v4##Type &a, v4##Type b) \
{ \
a = a Syntax b; \
}
#define VectorDefine(Type) \
typedef union \
{ \
struct \
{ \
Type x; \
Type y; \
}; \
struct \
{ \
Type Width; \
Type Height; \
}; \
Type e[2]; \
}v2##Type; \
\
typedef union \
{ \
struct \
{ \
Type x; \
Type y; \
Type z; \
}; \
struct \
{ \
Type Width; \
Type Height; \
Type Depth; \
}; \
struct \
{ \
Type r; \
Type g; \
Type b; \
}; \
Type e[3]; \
v2##Type xy; \
}v3##Type; \
\
typedef union \
{ \
struct \
{ \
Type x; \
Type y; \
Type z; \
Type w; \
}; \
struct \
{ \
Type r; \
Type g; \
Type b; \
Type a; \
}; \
Type e[4]; \
v2##Type xy; \
v3##Type xyz; \
v3##Type rgb; \
}v4##Type; \
\
\
VectorOverloadDefine(Type,+) \
VectorOverloadDefine(Type,-) \
VectorOverloadDefine(Type,*) \
VectorOverloadDefine(Type,/) \
inline v2##Type \
operator-(v2##Type a) \
{ \
v2##Type Result; \
\
Result.x = -a.x; \
Result.y = -a.y; \
\
return(Result); \
} \
inline v3##Type \
operator-(v3##Type a) \
{ \
v3##Type Result; \
\
Result.x = -a.x; \
Result.y = -a.y; \
Result.z = -a.z; \
\
return(Result); \
} \
inline v4##Type \
operator-(v4##Type a) \
{ \
v4##Type Result; \
\
Result.x = -a.x; \
Result.y = -a.y; \
Result.z = -a.z; \
Result.w = -a.w; \
\
return(Result); \
}
#define VectorFloatExtraDefine(Type) \
inline Type \
v2##Type##Length(v2##Type a) \
{ \
Type Result; \
a.x = a.x * a.x; \
a.y = a.y * a.y; \
Result = sqrt(a.x + a.y); \
return Result; \
} \
inline Type \
v3##Type##Length(v3##Type a) \
{ \
Type Result; \
a.x = a.x * a.x; \
a.y = a.y * a.y; \
a.z = a.z * a.z; \
Result = sqrt(a.x + a.y + a.z); \
return Result; \
} \
inline Type \
v4##Type##Length(v4##Type a) \
{ \
Type Result; \
a.x = a.x * a.x; \
a.y = a.y * a.y; \
a.z = a.z * a.z; \
a.w = a.w * a.w; \
Result = sqrt(a.x + a.y + a.z + a.w); \
return Result; \
} \
inline Type \
v2##Type##Angle(v2##Type a) \
{ \
Type Result; \
Type x = (Type)a.x; \
Type y = (Type)a.y; \
Result = atan2(y,x); \
Result = Result >= 0 ? Result : Result + TAU; \
return Result; \
} \
\
inline v2##Type \
v2##Type##New(Type Length, Type Angle) \
{ \
v2##Type Result; \
Result.x = cos(Angle)*Length; \
Result.y = sin(Angle)*Length; \
return Result; \
} \
\
inline v2##Type \
v2##Type##Rotate(v2##Type a, Type Rotation) \
{ \
v2##Type Result; \
Type Length = v2##Type##Length(a); \
Type Angle = v2##Type##Angle(a); \
\
Result = v2##Type##New(Length,Angle + Rotation);\
return Result; \
} \
\
inline v2##Type \
v2##Type##Normal(v2##Type a) \
{ \
v2##Type Result; \
Type Length = v2##Type##Length(a); \
\
Result.x = a.x / Length; \
Result.y = a.y / Length; \
return Result; \
} \
inline v3##Type \
v3##Type##Normal(v3##Type a) \
{ \
v3##Type Result; \
Type Length = v3##Type##Length(a); \
\
Result.x = a.x / Length; \
Result.y = a.y / Length; \
Result.z = a.z / Length; \
return Result; \
} \
inline v4##Type \
v4##Type##Normal(v4##Type a) \
{ \
v4##Type Result; \
Type Length = v4##Type##Length(a); \
\
Result.x = a.x / Length; \
Result.y = a.y / Length; \
Result.z = a.z / Length; \
Result.w = a.w / Length; \
return Result; \
}
VectorDefine(u8)
VectorDefine(u16)
VectorDefine(u32)
VectorDefine(u64)
VectorDefine(s8)
VectorDefine(s16)
VectorDefine(s32)
VectorDefine(s64)
VectorDefine(f32)
VectorDefine(f64)
VectorFloatExtraDefine(f32)
VectorFloatExtraDefine(f64)
int main(void)
{
v3u8 av = {5,6,6};
v3s32 bb = {200,10,15};
bb *= (v3s32){av.x,av.y,av.z}; // Casting have to be done a little inconvinient.
av = av + (v3u8){1,1,1};
printf("%i , %i , %i\n",bb.x,bb.y,bb.z);
printf("%i , %i , %i\n",av.x,av.y,av.z);
return 0;
}