All files / src matrix.js

74.28% Statements 26/35
53.84% Branches 7/13
66.66% Functions 4/6
70% Lines 21/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        6x 6x 6x 6x 6x 6x         54x       6x     6x 6x 6x 6x         6x       6x 6x 18x   6x 18x 18x 54x       6x                          
import { substractVector } from './vector'
export function inverse2x2Matrix([a, b, c, d]) {
  //inverse matrix |a,c|
  //               |b,d|
  const det = a * b - c * b
  if (det == 0) {
    return null
  }
  return [d / det, -b / det, -c / det, a / det]
}
 
export function inverse3x3matrix(m) {
  let [[a, b, c], [d, e, f], [g, h, i]] = m
  let x = e * i - h * f
  let y = f * g - d * i
  let z = d * h - g * e
  let det = a * x + b * y + c * z
  return det != 0
    ? [
        [x, c * h - b * i, b * f - c * e],
        [y, a * i - c * g, d * c - a * f],
        [z, g * b - a * h, a * e - d * b]
      ].map((r) => r.map((v) => (v /= det)))
    : null
}
export function multiplyMatrices(a, b) {
  Iif (!Array.isArray(a) || !Array.isArray(b) || !a.length || !b.length) {
    throw new Error('arguments should be in 2-dimensional array format')
  }
  let x = a.length
  let z = a[0].length
  let y = b[0].length
  Iif (b.length !== z) {
    throw new Error(
      'number of columns in the first matrix should be the same as the number of rows in the second'
    )
  }
  let productRow = Array.apply(null, new Array(y)).map(
    Number.prototype.valueOf,
    0
  )
  let product = new Array(x)
  for (let p = 0; p < x; p++) {
    product[p] = productRow.slice()
  }
  for (let i = 0; i < x; i++) {
    for (let j = 0; j < y; j++) {
      for (let k = 0; k < z; k++) {
        product[i][j] += a[i][k] * b[k][j]
      }
    }
  }
  return product
}
export function rotateTransformation(point, angle, center = { x: 0, y: 0 }) {
  let rotationMatrix = [
    [Math.cos(angle), Math.sin(angle)],
    [-Math.sin(angle), Math.cos(angle)]
  ]
  let k = multiplyMatrices(rotationMatrix, [
    [point.x - center.x],
    [point.y - center.y]
  ])
  return { x: k[0][0] + center.x, y: k[1][0] + center.y }
}