|
| 1 | +package echo.math; |
| 2 | + |
| 3 | +import echo.math.Types; |
| 4 | + |
| 5 | +@:dox(hide) |
| 6 | +@:noCompletion |
| 7 | +class Matrix2Default { |
| 8 | + public var m00:Float; |
| 9 | + public var m01:Float; |
| 10 | + |
| 11 | + public var m10:Float; |
| 12 | + public var m11:Float; |
| 13 | + /** |
| 14 | + * Column-Major Orientation. |
| 15 | + * /m00, m10/ |
| 16 | + * /m01, m11/ |
| 17 | + */ |
| 18 | + public inline function new(m00:Float, m10:Float, m01:Float, m11:Float) { |
| 19 | + this.m00 = m00 + 0.0; |
| 20 | + this.m10 = m10 + 0.0; |
| 21 | + this.m01 = m01 + 0.0; |
| 22 | + this.m11 = m11 + 0.0; |
| 23 | + } |
| 24 | + |
| 25 | + public function toString():String { |
| 26 | + return '{ m00:$m00, m10:$m10, m01:$m01, m11:$m11 }'; |
| 27 | + } |
| 28 | +} |
| 29 | +/** |
| 30 | + * Column-Major Orientation. |
| 31 | + * /m00, m10/ |
| 32 | + * /m01, m11/ |
| 33 | + */ |
| 34 | +@:using(echo.math.Matrix2) |
| 35 | +@:forward(m00, m10, m01, m11) |
| 36 | +abstract Matrix2(Matrix2Type) from Matrix2Type to Matrix2Type { |
| 37 | + public static inline final element_count:Int = 4; |
| 38 | + |
| 39 | + public static var zero(get, never):Matrix2; |
| 40 | + |
| 41 | + public static var identity(get, never):Matrix2; |
| 42 | + |
| 43 | + public var col_x(get, set):Vector2; |
| 44 | + |
| 45 | + public var col_y(get, set):Vector2; |
| 46 | + /** |
| 47 | + * Gets a rotation matrix from the given radians. |
| 48 | + */ |
| 49 | + public static inline function from_radians(radians:Float) { |
| 50 | + var c = Math.cos(radians); |
| 51 | + var s = Math.sin(radians); |
| 52 | + return new Matrix2(c, -s, s, c); |
| 53 | + } |
| 54 | + |
| 55 | + public static inline function from_vectors(x:Vector2, y:Vector2) return new Matrix2(x.x, y.x, x.y, y.y); |
| 56 | + |
| 57 | + @:from |
| 58 | + public static inline function from_arr(a:Array<Float>):Matrix2 @:privateAccess return new Matrix2(a[0], a[1], a[2], a[3]); |
| 59 | + |
| 60 | + @:to |
| 61 | + public inline function to_arr():Array<Float> { |
| 62 | + var self = this; |
| 63 | + return [self.m00, self.m10, self.m01, self.m11]; |
| 64 | + } |
| 65 | + |
| 66 | + public inline function new(m00:Float, m10:Float, m01:Float, m11:Float) { |
| 67 | + this = new Matrix2Type(m00, m10, m01, m11); |
| 68 | + } |
| 69 | + |
| 70 | + // region operator overloads |
| 71 | + |
| 72 | + @:op([]) |
| 73 | + public inline function arr_read(i:Int):Float { |
| 74 | + var self:Matrix2 = this; |
| 75 | + |
| 76 | + switch (i) { |
| 77 | + case 0: |
| 78 | + return self.m00; |
| 79 | + case 1: |
| 80 | + return self.m10; |
| 81 | + case 2: |
| 82 | + return self.m01; |
| 83 | + case 3: |
| 84 | + return self.m11; |
| 85 | + default: |
| 86 | + throw "Invalid element"; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @:op([]) |
| 91 | + public inline function arr_write(i:Int, value:Float):Float { |
| 92 | + var self:Matrix2 = this; |
| 93 | + |
| 94 | + switch (i) { |
| 95 | + case 0: |
| 96 | + return self.m00 = value; |
| 97 | + case 1: |
| 98 | + return self.m10 = value; |
| 99 | + case 2: |
| 100 | + return self.m01 = value; |
| 101 | + case 3: |
| 102 | + return self.m11 = value; |
| 103 | + default: |
| 104 | + throw "Invalid element"; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @:op(a * b) |
| 109 | + static inline function mul(a:Matrix2, b:Matrix2):Matrix2 |
| 110 | + return new Matrix2(a.m00 * b.m00 |
| 111 | + + a.m10 * b.m01, a.m00 * b.m10 |
| 112 | + + a.m10 * b.m11, a.m01 * b.m00 |
| 113 | + + a.m11 * b.m01, a.m01 * b.m10 |
| 114 | + + a.m11 * b.m11); |
| 115 | + |
| 116 | + @:op(a * b) |
| 117 | + static inline function mul_vec2(a:Matrix2, v:Vector2):Vector2 |
| 118 | + return new Vector2(a.m00 * v.x + a.m10 * v.y, a.m01 * v.x + a.m11 * v.y); |
| 119 | + |
| 120 | + // endregion |
| 121 | + |
| 122 | + static inline function get_zero():Matrix2 { |
| 123 | + return new Matrix2(0.0, 0.0, 0.0, 0.0); |
| 124 | + } |
| 125 | + |
| 126 | + static inline function get_identity():Matrix2 { |
| 127 | + return new Matrix2(1.0, 0.0, 0.0, 1.0); |
| 128 | + } |
| 129 | + |
| 130 | + inline function get_col_x():Vector2 { |
| 131 | + var self = this; |
| 132 | + return new Vector2(self.m00, self.m01); |
| 133 | + } |
| 134 | + |
| 135 | + inline function get_col_y():Vector2 { |
| 136 | + var self = this; |
| 137 | + return new Vector2(self.m11, self.m11); |
| 138 | + } |
| 139 | + |
| 140 | + inline function set_col_x(vector2:Vector2):Vector2 { |
| 141 | + var self = this; |
| 142 | + return vector2.set(self.m00, self.m01); |
| 143 | + } |
| 144 | + |
| 145 | + inline function set_col_y(vector2:Vector2):Vector2 { |
| 146 | + var self = this; |
| 147 | + return vector2.set(self.m10, self.m11); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +inline function copy_to(a:Matrix2, b:Matrix2):Matrix2 { |
| 152 | + b.copy_from(a); |
| 153 | + return a; |
| 154 | +} |
| 155 | + |
| 156 | +inline function copy_from(a:Matrix2, b:Matrix2):Matrix2 { |
| 157 | + a.m00 = b.m00; |
| 158 | + a.m10 = b.m10; |
| 159 | + a.m01 = b.m01; |
| 160 | + a.m11 = b.m11; |
| 161 | + return a; |
| 162 | +} |
| 163 | + |
| 164 | +inline function transposed(m:Matrix2):Matrix2 return new Matrix2(m.m00, m.m01, m.m10, m.m11); |
0 commit comments