omelet 0.1.2

A lightweight, game-orented math library for Rust, including vectors, matrices, and quaternions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use crate::matrices::Mat3;
use crate::utils::epsilon_eq_default;
use crate::vec::{Vec2, Vec3};
use core::f32;
use std::{
    cmp::PartialEq,
    fmt,
    ops::{Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Sub, SubAssign},
};

/// A 2x2 column-major matrix used for 2D linear transformations such as
/// rotation, scaling, and shearing.
///
/// The matrix is composed of two `Vec2` column vectors:
///
/// ```text
/// [col0.x, col1.x]
/// [col0.y, col1.y]
/// ```
///
/// # Example
/// ```rust
/// use omelet::vec::Vec2;
/// use omelet::matrices::Mat2;
///
/// let col0 = Vec2::new(1.0, 0.0);
/// let col1 = Vec2::new(0.0, 1.0);
/// let identity = Mat2::new(col0, col1);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Mat2 {
    /// The first column of the matrix
    pub col0: Vec2,
    /// The second column of the matrix
    pub col1: Vec2,
}

// ============= Types ==============
pub type Mat2Tuple2D = ((f32, f32), (f32, f32));
pub type Mat2Tuple = (f32, f32, f32, f32);

impl Mat2 {
    // ============= Construction and Conversion =============
    /// Constructs a new 2x2 matrix from two column vectors
    ///
    /// The matrix is structured in column-major order:
    /// ```text
    /// [col0.x, col1.x]
    /// [col0.y, col1.y]
    /// ```
    pub fn new(col0: Vec2, col1: Vec2) -> Mat2 {
        Mat2 { col0, col1 }
    }

    /// Constructs a matrix from row vectors
    ///
    /// # Example
    /// ```rust
    /// use omelet::vec::Vec2;
    /// use omelet::matrices::Mat2;
    ///
    /// let m = Mat2::from_rows(Vec2::new(1.0, 2.0), Vec2::new(3.0, 4.0));
    /// assert_eq!(m, Mat2::new(Vec2::new(1.0, 3.0), Vec2::new(2.0, 4.0)));
    /// ```
    pub fn from_rows(r0: Vec2, r1: Vec2) -> Mat2 {
        Mat2 {
            col0: Vec2::new(r0.x, r1.x),
            col1: Vec2::new(r0.y, r1.y),
        }
    }

    /// Constructs a matrix representing a counter-clockwise rotation
    ///
    /// # Parameters
    /// - `radians`: Rotation angle in radians
    ///
    /// # Example
    /// ```rust
    /// use omelet::matrices::Mat2;
    /// let rot90 = Mat2::from_angle(std::f32::consts::FRAC_PI_2);
    /// ```
    pub fn from_angle(radians: f32) -> Mat2 {
        let (s, c) = radians.sin_cos();
        Mat2::new(Vec2::new(c, s), Vec2::new(-s, c))
    }

    /// Constructs a scale matrix from a scale vector
    ///
    /// # Example
    /// ```rust
    /// use omelet::matrices::Mat2;
    /// use omelet::vec::Vec2;
    /// let scale = Mat2::from_scale(Vec2::new(2.0, 3.0));
    /// assert_eq!(scale, Mat2::new(Vec2::new(2.0, 0.0), Vec2::new(0.0, 3.0)));
    /// ```
    pub fn from_scale(scale: Vec2) -> Mat2 {
        Mat2::new(Vec2::new(scale.x, 0.0), Vec2::new(0.0, scale.y))
    }

    // ============= Constants ==============
    pub const ZERO: Self = Self {
        col0: Vec2::ZERO,
        col1: Vec2::ZERO,
    };

    pub const IDENTITY: Self = Self {
        col0: Vec2 { x: 1.0, y: 0.0 },
        col1: Vec2 { x: 0.0, y: 1.0 },
    };

    pub const NAN: Self = Self {
        col0: Vec2::NAN,
        col1: Vec2::NAN,
    };

    pub const INFINITY: Self = Self {
        col0: Vec2::INFINITY,
        col1: Vec2::INFINITY,
    };

    /// Returns the transpose of the matrix
    ///
    /// Swaps rows and columns:
    /// ```text
    /// [a, b]  =>  [a, c]
    /// [c, d]  =>  [b, d]
    /// ```
    pub fn transpose(&self) -> Mat2 {
        Mat2::new(
            Vec2::new(self.col0.x, self.col1.x),
            Vec2::new(self.col0.y, self.col1.y),
        )
    }

    /// Returns the determinant of the matrix
    ///
    /// Used for checking invertibility or computing area under transformations
    ///
    /// ```text
    /// det = a * d- b * c
    /// ```
    pub fn determinant(&self) -> f32 {
        self.col0.x * self.col1.y - self.col0.y * self.col1.x
    }

    /// Returns the inverse of the matrix, if it exists
    ///
    /// Returns `None` if the matrix is singular (non-invertible)
    ///
    /// # Panics
    /// Will not panic. Returns `None` instead of panicking on singular matrices
    pub fn inverse(&self) -> Option<Mat2> {
        let det = self.determinant();
        if epsilon_eq_default(det.abs(), 0.0) {
            return None;
        }

        let inv_det = 1.0 / det;

        let a = self.col0.x;
        let b = self.col0.y;
        let c = self.col1.x;
        let d = self.col1.y;

        Some(Mat2::new(
            Vec2::new(d, -b) * inv_det,
            Vec2::new(-c, a) * inv_det,
        ))
    }

    /// Returns `true` if the matrix is invertible (non-zero determinant)
    pub fn is_invertible(&self) -> bool {
        self.determinant().abs() > 1e-6
    }

    /// Returns a row major 2 dimensional array `[[f32; 2]; 2]`
    ///
    /// Equivalent to `[[col0.x, col1.x], [col0.y, col1.y]]`
    pub fn to_array_2d_row_major(&self) -> [[f32; 2]; 2] {
        [[self.col0.x, self.col1.x], [self.col0.y, self.col1.y]]
    }

    /// Returns a row major flat array `[f32; 4]`
    ///
    /// Equivalent to `[col0.x, col0.y, col1.x, col1.y]`
    pub fn to_array_row_major(&self) -> [f32; 4] {
        [self.col0.x, self.col0.y, self.col1.x, self.col1.y]
    }

    /// Returns a column major 2 dimensional array `[[f32; 2]; 2]`
    ///
    /// Equivalent to `[[col0.x, col0.y], [col1.x, col1.y]]`
    pub fn to_array_2d_col_major(&self) -> [[f32; 2]; 2] {
        [[self.col0.x, self.col0.y], [self.col1.x, self.col1.y]]
    }

    /// Returns a column major flat array `[f32; 4]`
    ///
    /// Equivalent to `[col0.x, col0.y, col1.x, col1.y]`
    pub fn to_array_col_major(&self) -> [f32; 4] {
        [self.col0.x, self.col0.y, self.col1.x, self.col1.y]
    }

    /// Returns a row major 2 dimensional tuple `((f32, f32), (f32, f32))`
    ///
    /// Equivalent to `((col0.x, col1.x), (col0.y, col1.y))`
    pub fn to_tuple_2d_row_major(&self) -> Mat2Tuple2D {
        ((self.col0.x, self.col1.x), (self.col0.y, self.col1.y))
    }

    /// Returns a row major tuple `(f32, f32, f32, f32)`
    ///
    /// Equivalent to `(col0.x, col0.y, col1.x, col1.y)`
    pub fn to_tuple_row_major(&self) -> Mat2Tuple {
        (self.col0.x, self.col0.y, self.col1.x, self.col1.y)
    }

    /// Returns a column major 2 dimensional tuple `((f32, f32), (f32, f32))`
    ///
    /// Equivalent to `((col0.x, col0.y), (col1.x, col1.y))`
    pub fn to_tuple_2d_col_major(&self) -> Mat2Tuple2D {
        ((self.col0.x, self.col0.y), (self.col1.x, self.col1.y))
    }

    /// Returns a column major tuple `(f32, f32, f32, f32)`
    ///
    /// Equivalent to `(col0.x, col0.y, col1.x, col1.y)`
    pub fn to_tuple_col_major(&self) -> Mat2Tuple {
        (self.col0.x, self.col0.y, self.col1.x, self.col1.y)
    }

    /// Returns a `Mat2` from a 2 dimensional array
    ///
    /// # Paramneters
    /// - `arr`: 2 dimensional array `[[f32; 2]; 2]`
    pub fn from_2d_array(arr: [[f32; 2]; 2]) -> Mat2 {
        Mat2::new(
            Vec2::new(arr[0][0], arr[0][1]),
            Vec2::new(arr[1][0], arr[1][1]),
        )
    }

    /// Returns a `Mat2` from a flat array
    ///
    /// # Parameters
    /// - `arr`: Flat array `[f32; 4]`
    ///
    /// Equivalent to `Mat2{Vec2{arr[0], arr[1]}, Vec2[arr[2], arr[3]]}}`
    pub fn from_array(arr: [f32; 4]) -> Mat2 {
        Mat2::new(Vec2::new(arr[0], arr[1]), Vec2::new(arr[2], arr[3]))
    }

    /// Returns a `Mat2` from a 2 dimensional tuple
    ///
    /// # Parameters
    /// - `t`: Tuple to use `((f32, f32), (f32, f32))`
    pub fn from_2d_tuple(t: Mat2Tuple2D) -> Mat2 {
        Mat2::new(Vec2::new(t.0 .0, t.0 .1), Vec2::new(t.1 .0, t.1 .1))
    }

    /// Returns a `Mat2` from a tuple
    ///
    /// # Parameters
    /// - `t`: Tuple `(f32, f32, f32, f32)`
    ///
    /// Equivalent to `Mat2{Vec2{t.0, t.1}, Vec2{t.2, t.3}}`
    pub fn from_tuple(t: Mat2Tuple) -> Mat2 {
        Mat2::new(Vec2::new(t.0, t.1), Vec2::new(t.2, t.3))
    }

    /// Returns the specified column vector.
    ///
    /// # Panics
    /// Panics if `col_idx` is not 0 or 1.
    pub fn col(&self, index: usize) -> Vec2 {
        match index {
            0 => self.col0,
            1 => self.col1,
            _ => panic!("Mat2 column index out of bounds: {}", index),
        }
    }

    pub fn row(&self, index: usize) -> Vec2 {
        match index {
            0 => Vec2::new(self.col0.x, self.col1.x),
            1 => Vec2::new(self.col0.y, self.col1.y),
            _ => panic!("Mat2 row index out of bounds: {}", index),
        }
    }

    /// Returns a 3x3 column-major matrix.
    ///
    /// # Returns
    /// A `Mat3`:
    /// ```text
    /// [a, c, 0]
    /// [b, d, 0]
    /// [0, 0, 1]
    /// ```
    pub fn to_mat3(&self) -> Mat3 {
        Mat3::new(
            Vec3::new(self.col0.x, self.col0.y, 0.0),
            Vec3::new(self.col1.x, self.col1.y, 0.0),
            Vec3::new(0.0, 0.0, 1.0),
        )
    }

    // ============= Math Utilities =============

    /// Returns a matrix with the absolute value of each component
    ///
    /// Useful for bounding box operations or eliminating directional signs
    pub fn abs(self) -> Mat2 {
        Mat2 {
            col0: self.col0.abs(),
            col1: self.col1.abs(),
        }
    }

    /// Returns the sign (-1.0, 0.0, 1.0) of each matrix component
    ///
    /// Useful when analyzing directionality of matrix effects
    pub fn signum(self) -> Mat2 {
        Mat2 {
            col0: self.col0.signum(),
            col1: self.col1.signum(),
        }
    }

    /// Linearly interpolates between `self` and matrix `b` by amount `t`
    ///
    /// Equivalent to `self * (1.0 - t) + b * t`
    ///
    /// # Parameters
    /// - `b`: The target matrix
    /// - `t`: Interpolation factor
    ///
    /// # Example
    /// ```rust
    /// use omelet::matrices::Mat2;
    /// use omelet::vec::Vec2;
    ///
    /// let a = Mat2::IDENTITY;
    /// let b = Mat2::from_scale(Vec2::new(2.0, 2.0));
    /// let halfway = a.lerp(b, 0.5);
    /// ```
    pub fn lerp(&self, b: Mat2, t: f32) -> Mat2 {
        Mat2::new(self.col0.lerp(b.col0, t), self.col1.lerp(b.col1, t))
    }

    /// Linearly interpolates between matrix `a` and matrix `b` by amount `t`
    ///
    /// # Parameters
    /// - `a`: Starting matrix
    /// - `b`: Target matrix
    /// - `t`: Interpolation factor
    ///
    /// Equivalent to `a * (1.0 - t) + b * t`
    pub fn lerp_between(a: Mat2, b: Mat2, t: f32) -> Mat2 {
        Mat2::new(a.col0.lerp(b.col0, t), a.col1.lerp(b.col1, t))
    }

    /// Checks if all components in `self` are approximately equal to
    /// all components in `other` with a default epsilon
    ///
    /// # Parameters
    /// - `other`: The other matrix in the comparison
    pub fn approx_eq(&self, other: Mat2) -> bool {
        self.col0.approx_eq(other.col0) && self.col1.approx_eq(other.col1)
    }

    /// Checks if all components in `self` are approximately equal to
    /// all components in `other` with a custom epsilon
    ///
    /// # Parameters
    /// - `other`: The other matrix in the comparison
    /// - `epsilon`: Epsilon to use in the check
    pub fn approx_eq_eps(&self, other: Mat2, epsilon: f32) -> bool {
        self.col0.approx_eq_eps(other.col0, epsilon) && self.col1.approx_eq_eps(other.col1, epsilon)
    }

    /// Returns `true` if any of the components are NaN
    pub fn is_nan(&self) -> bool {
        self.col0.is_nan() || self.col1.is_nan()
    }

    /// Returns `true` if all of the components are finite
    pub fn is_finite(&self) -> bool {
        self.col0.is_finite() && self.col1.is_finite()
    }

    /// Computes the adjugate (classical adjoint) of the matrix.
    ///
    /// For matrix A =
    /// ```text
    /// [a, b]
    /// [c, d]
    /// ```
    /// the adjugate is:
    /// ```text
    /// [ d, -b]
    /// [-c,  a]
    /// ```
    pub fn adjugate(self) -> Mat2 {
        Mat2::new(
            Vec2::new(self.col1.y, -self.col0.y),
            Vec2::new(-self.col1.x, self.col0.x),
        )
    }

    // ============= Transform Utilities =============
    /// Extracts the scale component from the matrix by computing
    /// the length (magnitude) of each column vector
    ///
    /// This assumes that the matrix encodes a rotation+scale transform
    pub fn extract_scale(&self) -> Vec2 {
        Vec2::new(self.col0.length(), self.col1.length())
    }

    /// Returns the diagonal scale components without considering rotation
    ///
    /// This assumes the matrix is purely scaling or has scale aligned with axes
    /// It's a "raw" lookup: returns (col0.x, col1.y)
    pub fn extract_scale_raw(&self) -> Vec2 {
        Vec2::new(self.col0.x, self.col1.y)
    }

    /// Extracts the rotation angle in radians from the matrix
    ///
    /// Assumes the matrix is a pure rotation or a rotation+scale transform
    /// Uses the direction of the first column vector (X-axis) to determine angle
    pub fn extract_rotation(&self) -> f32 {
        self.col0.y.atan2(self.col0.x)
    }

    /// Returns an orthonormalized version of the matrix
    ///
    /// This ensures the columns are orthogonal unit vectors.
    /// This is useful if the matrix has accumulated numerical
    /// error or is approximately orthonormal
    ///
    /// - First column is normalized as the X-axis.
    /// - Second column is computed as the perpendicular vector (rotated 90 degrees CCW)
    pub fn orthonormalize(&self) -> Mat2 {
        let x = self.col0.normalize();
        let y = Vec2::new(-x.y, x.x);
        Mat2::new(x, y)
    }

    /// Checks if the matrix is orthogonal within a tolerance.
    ///
    /// Orthogonal matrices satisfy `M^T * M = I`.
    ///
    /// # Parameters
    /// - `epsilon`: Maximum allowed deviation from orthogonality.
    ///
    /// # Returns
    /// `true` if orthogonal within tolerance.
    pub fn is_orthogonal(&self, epsilon: f32) -> bool {
        (self.transpose() * *self).approx_eq_eps(Mat2::IDENTITY, epsilon)
    }

    /// Returns the trace of the matrix.
    ///
    /// The trace is the sum of the diagonal elements (`a + d`).
    ///
    /// # Returns
    /// Scalar trace value.
    pub fn trace(self) -> f32 {
        self.col0.x + self.col1.y
    }

    // ============= Decomposition =============
    pub fn decompose(&self) -> (Vec2, f32) {
        (self.extract_scale(), self.extract_rotation())
    }

    // ============= Triangle Checks =============
    /// Checks if the matrix is lower-triangular within a tolerance.
    ///
    /// This means elements above the main diagonal are approx zero.
    ///
    /// # Parameters
    /// - `epsilon`: Maximum allowed magnitude of upper-triangular elements.
    ///
    /// # Returns
    /// `true` if matrix is lower-triangular within tolerance.
    pub fn is_lower_triangular(&self, epsilon: f32) -> bool {
        self.col0.y.abs() <= epsilon
    }

    /// Checks if the matrix is upper-triangular within a tolerance.
    ///
    /// This means elements below the main diagonal are approx zero.
    ///
    /// # Parameters
    /// - `epsilon`: Maximum allowed magnitude of lower-triangular elements.
    ///
    /// # Returns
    /// `true` if matrix is upper-triangular within tolerance.
    pub fn is_upper_triangular(&self, epsilon: f32) -> bool {
        self.col1.x.abs() <= epsilon
    }
}

// ============= Operator Overloads =============

/// Adds two matrices together component-wise.
impl Add for Mat2 {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Self::new(self.col0 + rhs.col0, self.col1 + rhs.col1)
    }
}

/// Subtracts `rhs` from `self` component-wise.
impl Sub for Mat2 {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Self::new(self.col0 - rhs.col0, self.col1 - rhs.col1)
    }
}

/// Multiplies two matrices using standard matrix multiplication.
impl Mul for Mat2 {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        Self::new(self * rhs.col0, self * rhs.col1)
    }
}

/// Multiplies the matrix by a `Vec2` (matrix-vector multiplication).
impl Mul<Vec2> for Mat2 {
    type Output = Vec2;
    #[inline]
    fn mul(self, rhs: Vec2) -> Self::Output {
        self.col0 * rhs.x + self.col1 * rhs.y
    }
}

/// Multiplies each component of the matrix by a scalar.
impl Mul<f32> for Mat2 {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: f32) -> Self::Output {
        Self::new(self.col0 * rhs, self.col1 * rhs)
    }
}

/// Multiplies a scalar by each component of the matrix.
impl Mul<Mat2> for f32 {
    type Output = Mat2;
    #[inline]
    fn mul(self, rhs: Mat2) -> Self::Output {
        rhs * self
    }
}

/// Divides each component of the matrix by a scalar.
impl Div<f32> for Mat2 {
    type Output = Self;
    #[inline]
    fn div(self, rhs: f32) -> Self::Output {
        Self::new(self.col0 / rhs, self.col1 / rhs)
    }
}

/// Negates each component of the matrix.
impl Neg for Mat2 {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self::Output {
        Self::new(-self.col0, -self.col1)
    }
}

// ============= Assignment Operator Overloads =============

impl AddAssign for Mat2 {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.col0 += rhs.col0;
        self.col1 += rhs.col1;
    }
}

impl SubAssign for Mat2 {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.col0 -= rhs.col0;
        self.col1 -= rhs.col1;
    }
}

impl MulAssign for Mat2 {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl MulAssign<f32> for Mat2 {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        self.col0 *= rhs;
        self.col1 *= rhs;
    }
}

impl DivAssign<f32> for Mat2 {
    #[inline]
    fn div_assign(&mut self, rhs: f32) {
        self.col0 /= rhs;
        self.col1 /= rhs;
    }
}

// ============= Trait Implementations =============

impl Default for Mat2 {
    /// Returns the identity matrix.
    #[inline]
    fn default() -> Self {
        Self::IDENTITY // Assumes Mat2::IDENTITY constant exists
    }
}

/// Checks whether two matrices are exactly equal.
impl PartialEq for Mat2 {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.col0 == other.col0 && self.col1 == other.col1
    }
}

/// Enables `m[column]` access. Panics if `col_index` is out of bounds.
impl Index<usize> for Mat2 {
    type Output = Vec2;
    #[inline]
    fn index(&self, col_index: usize) -> &Self::Output {
        match col_index {
            0 => &self.col0,
            1 => &self.col1,
            _ => panic!("Mat2 column index out of bounds: {}", col_index),
        }
    }
}

/// Enables mutable `m[column]` access. Panics if `col_index` is out of bounds.
impl IndexMut<usize> for Mat2 {
    #[inline]
    fn index_mut(&mut self, col_index: usize) -> &mut Self::Output {
        match col_index {
            0 => &mut self.col0,
            1 => &mut self.col1,
            _ => panic!("Mat2 column index out of bounds: {}", col_index),
        }
    }
}

/// Implements the `Display` trait for pretty-printing.
impl fmt::Display for Mat2 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{:.3}, {:.3}]\n[{:.3}, {:.3}]",
            self.col0.x, self.col1.x, self.col0.y, self.col1.y
        )
    }
}

// ============= Approx Crate Implementations =============

/// Implements absolute difference equality comparison for `Mat2`.
impl approx::AbsDiffEq for Mat2 {
    type Epsilon = f32;

    #[inline]
    fn default_epsilon() -> f32 {
        f32::EPSILON
    }

    #[inline]
    fn abs_diff_eq(&self, other: &Self, epsilon: f32) -> bool {
        self.col0.abs_diff_eq(&other.col0, epsilon) && self.col1.abs_diff_eq(&other.col1, epsilon)
    }
}

/// Implements relative equality comparison for `Mat2`.
impl approx::RelativeEq for Mat2 {
    #[inline]
    fn default_max_relative() -> f32 {
        f32::EPSILON
    }

    #[inline]
    fn relative_eq(&self, other: &Self, epsilon: f32, max_relative: f32) -> bool {
        self.col0.relative_eq(&other.col0, epsilon, max_relative)
            && self.col1.relative_eq(&other.col1, epsilon, max_relative)
    }
}