diff options
Diffstat (limited to 'math.h')
-rw-r--r-- | math.h | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -30,6 +30,55 @@ #define clamp(a,min,max) (a > max ? max : (a < min ? min : a)) +/* Apply affine coordinate transformation + */ +inline double2 apply_affine (const double2 in, const double2 matrix[3]) { + return matrix[0] * in[0] + matrix[1] * in[1] + matrix[2]; +} + +/* Create affine rotation matrix, angle in degree + */ +inline void rotate (const double angle, double2 matrix[3]) { + double s, c; + sincos (angle * 2.0 * M_PI / 360.0, &s, &c); + matrix[0] = (double2) { c, s }; + matrix[1] = (double2) { -s, c }; + matrix[2] = (double2) { 0.0, 0.0 }; +} + +/* Create affine translation matrix + */ +inline void translate (const double2 xy, double2 matrix[3]) { + matrix[0] = (double2) { 1.0, 0.0 }; + matrix[1] = (double2) { 0.0, 1.0 }; + matrix[2] = xy; +} + +/* Multiply two affine matrices a, b and store the result in c. + * + * The last row of each matrix is assumed to be 0, 0, 1. + */ +inline void matrixmul (const double2 a[3], const double2 b[3], double2 c[3]) { + c[0] = a[0] * b[0][0] + a[1] * b[0][1]; + c[1] = a[0] * b[1][0] + a[1] * b[1][1]; + c[2] = a[0] * b[2][0] + a[1] * b[2][1] + a[2]; +} + +/* Create rotation around center. Note that matrix multiplication is + * right-associative, thus A*B*C == A*(B*C) */ +inline void rotate_center (const double2 center, const double angle, double2 out[3]) { + double2 rot[3], trans_a[3], trans_b[3], tmp[3]; + translate (-1.0 * center, trans_a); + rotate (angle, rot); + translate (center, trans_b); + matrixmul (rot, trans_a, tmp); + matrixmul (trans_b, tmp, out); +} + +inline double sum(const double2 in) { + return in[0] + in[1]; +} + /* Vector wrapping function, could be replaced by true vector functions later */ |