122 lines
2.5 KiB
C
122 lines
2.5 KiB
C
#ifndef _fencer_vmath_h
|
|
#define _fencer_vmath_h
|
|
|
|
#include <math.h>
|
|
|
|
#if VMATH_SDL == 1
|
|
|
|
#include <SDL2/SDL_rect.h>
|
|
typedef SDL_FPoint Vector;
|
|
typedef SDL_Point IVector;
|
|
|
|
#else
|
|
|
|
typedef struct Vector {
|
|
float x;
|
|
float y;
|
|
} Vector;
|
|
|
|
typedef struct IVector {
|
|
int x;
|
|
int y;
|
|
} IVector;
|
|
|
|
#endif
|
|
|
|
// Vector Constant Macros
|
|
#define ZeroVector (Vector){0.0f, 0.0f}
|
|
#define InfinityVector (Vector){INFINITY, INFINITY}
|
|
#define OneVector (Vector){1.0f,1.0f}
|
|
|
|
#define UpVector (Vector){0.0f,-1.0f}
|
|
#define RightVector (Vector){1.0f,0.0f}
|
|
#define LeftVector (Vector){-1.0f,0.0f}
|
|
#define DownVector (Vector){0.0f,1.0f}
|
|
|
|
// Integer Vector Constant Macros
|
|
#define ZeroIVector (IVector){0,0}
|
|
#define OneIVector (IVector){1,1}
|
|
|
|
#define UpIVector (IVector){-1,0}
|
|
#define DownIVector (IVector){1,0}
|
|
#define RightIVector (IVector){1,0}
|
|
#define LeftIVector (IVector){-1,0}
|
|
|
|
///
|
|
// Floating point vector maths functions.
|
|
///
|
|
static inline
|
|
Vector vaddf(Vector a, Vector b) {
|
|
return (Vector){a.x + b.x, a.y + b.y};
|
|
}
|
|
static inline
|
|
Vector vsubf(Vector a, Vector b) {
|
|
return (Vector){a.x - b.x, a.y - b.y};
|
|
}
|
|
static inline
|
|
Vector vmulff(Vector a, float b) {
|
|
return (Vector){a.x * b, a.y * b};
|
|
}
|
|
static inline
|
|
Vector vmulf(Vector a, Vector b) {
|
|
return (Vector) {a.x * b.x, a.y * b.y};
|
|
}
|
|
static inline
|
|
Vector vinvf(Vector a) {
|
|
return (Vector){-a.x, -a.y};
|
|
}
|
|
static inline
|
|
Vector vperpendicularf(Vector a) {
|
|
return (Vector){a.y, -a.x};
|
|
}
|
|
static inline
|
|
float vmagnitudef(Vector a) {
|
|
return sqrtf(a.x*a.x + a.y*a.y);
|
|
}
|
|
static inline
|
|
float vsqrmagnitudef(Vector a) {
|
|
return fabsf(a.x*a.x) + fabsf(a.y*a.y);
|
|
}
|
|
static inline
|
|
Vector vnormalizedf(Vector a) {
|
|
return vmulff(a, 1.0/vmagnitudef(a));
|
|
}
|
|
static inline
|
|
float vdotf(Vector a, Vector b) {
|
|
return (a.x*b.x) + (a.y*b.y);
|
|
}
|
|
static inline
|
|
float vdistancef(Vector a, Vector b) {
|
|
return vmagnitudef(vsubf(a, b));
|
|
}
|
|
static inline
|
|
float vsqrdistf(Vector a, Vector b) {
|
|
return vsqrmagnitudef(vsubf(a, b));
|
|
}
|
|
static inline
|
|
Vector vreciprocalf(Vector a) {
|
|
return (Vector){1.0/a.x, 1.0/a.y};
|
|
}
|
|
static inline
|
|
Vector vrotatef(Vector a, float t) {
|
|
return (Vector){
|
|
cosf(t) * a.x - sinf(t) * a.y,
|
|
sinf(t) * a.x + cosf(t) * a.y
|
|
};
|
|
}
|
|
|
|
static inline
|
|
IVector vaddi(IVector a, IVector b) {
|
|
return (IVector){a.x + b.x, a.y + b.y};
|
|
}
|
|
static inline
|
|
IVector vsubi(IVector a, IVector b) {
|
|
return (IVector){a.x - b.x, a.y - b.y};
|
|
}
|
|
static inline
|
|
IVector vmuli(IVector a, IVector b) {
|
|
return (IVector){a.x * b.x, a.y * b.y};
|
|
}
|
|
|
|
#endif // !_fencer_vmath_h
|