more cosmetix
[b2ld.git] / b2dlite / mathutils.d
blob3f610b9c59c7867f9946f113a65d5b143c6dad51
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
4 * Permission to use, copy, modify, distribute and sell this software
5 * and its documentation for any purpose is hereby granted without fee,
6 * provided that the above copyright notice appear in all copies.
7 * Erin Catto makes no representations about the suitability
8 * of this software for any purpose.
9 * It is provided "as is" without express or implied warranty.
11 module b2dlite.mathutils;
13 import iv.vmath;
15 public alias Vec2 = VecN!(2, float);
16 public alias VFloat = Vec2.VFloat;
17 public alias VFloatNum = Vec2.VFloatNum;
20 // ////////////////////////////////////////////////////////////////////////// //
21 public struct Mat22 {
22 nothrow @safe @nogc:
23 public:
24 Vec2 col1, col2;
26 public:
27 this (VFloat angle) {
28 pragma(inline, true);
29 import core.stdc.math : cosf, sinf;
30 immutable VFloat c = cosf(angle), s = sinf(angle);
31 col1.x = c; col1.y = s;
32 col2.x = -s; col2.y = c;
35 pure:
36 this() (in auto ref Vec2 acol1, in auto ref Vec2 acol2) { pragma(inline, true); col1 = acol1; col2 = acol2; }
38 Mat22 transpose () const { pragma(inline, true); return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y)); }
40 Mat22 invert () const {
41 pragma(inline, true);
42 immutable VFloat a = col1.x, b = col2.x, c = col1.y, d = col2.y;
43 Mat22 B;
44 VFloat det = a*d-b*c;
45 assert(det != VFloatNum!(0.0));
46 det = VFloatNum!(1.0)/det;
47 B.col1.x = det*d;
48 B.col2.x = -det*b;
49 B.col1.y = -det*c;
50 B.col2.y = det*a;
51 return B;
54 Vec2 opBinary(string op:"*") (in auto ref Vec2 v) const { pragma(inline, true); return Vec2(col1.x*v.x+col2.x*v.y, col1.y*v.x+col2.y*v.y); }
56 Mat22 opBinary(string op:"+") (in auto ref Mat22 B) const { pragma(inline, true); return Mat22(col1+B.col1, col2+B.col2); }
57 Mat22 opBinary(string op:"*") (in auto ref Mat22 B) const { pragma(inline, true); return Mat22(this*B.col1, this*B.col2); }
59 Mat22 abs() () { pragma(inline, true); return Mat22(col1.abs, col2.abs); }
63 // ////////////////////////////////////////////////////////////////////////// //
64 package(b2dlite) Vec2 fcross() (VFloat s, in auto ref Vec2 a) { pragma(inline, true); return Vec2(-s*a.y, s*a.x); }