16
import { Array_from_number, float32, float64 } from "@koalaui/compat"
18
export function mat33(array?: Float32Array): Matrix33 {
19
return (array == undefined) ? new Matrix33 () : new Matrix33(array)
22
const tolerance: float32 = (1.0 / (1 << 12))
24
export class Matrix33 {
25
public readonly array: Float32Array
26
constructor (array: Float32Array = new Float32Array(Array_from_number([
31
this.array = array.slice()
34
static zero(): Matrix33 {
35
return new Matrix33(new Float32Array(Array_from_number([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])))
38
static makeTranslate(dx: float32, dy: float32): Matrix33 {
39
return new Matrix33(new Float32Array(Array_from_number([1.0, 0.0, dx as float64, 0.0, 1.0, dy as float64, 0.0, 0.0, 1.0])))
42
static makeScale(dx: float32, dy: float32 = dx): Matrix33 {
43
return new Matrix33(new Float32Array(Array_from_number([dx as float64, 0.0, 0.0, 0.0, dy as float64, 0.0, 0.0, 0.0, 1.0])))
46
static makeRotate(degrees: float32, pivotX?: float32, pivotY?: float32): Matrix33 {
47
let rads = degrees * Math.PI / 180
48
let cos = Math.cos(rads)
49
let sin = Math.sin(rads)
50
if (Math.abs(sin) <= tolerance) sin = 0.0
51
if (Math.abs(cos) <= tolerance) cos = 0.0
52
if (pivotX !== undefined && pivotY != undefined) {
53
let dx = pivotX - pivotX * cos + pivotY * sin
54
let dy = pivotY - pivotY * cos - pivotX * sin
55
return new Matrix33(new Float32Array(Array_from_number([cos, -sin, dx, sin, cos, dy, 0.0, 0.0, 1.0])))
57
return new Matrix33(new Float32Array(Array_from_number([cos, -sin, 0.0, sin, cos, 0.0, 0.0, 0.0, 1.0])))
61
static makeSkew(sx: float32, sy: float32): Matrix33 {
62
return new Matrix33(new Float32Array(Array_from_number([1.0, sx, 0.0, sy, 1.0, 0.0, 0.0, 0.0, 1.0])))
65
makeConcat(rhs: Matrix33): Matrix33 {
66
return new Matrix33(new Float32Array(Array_from_number([
67
this.array[0] * rhs.array[0] + this.array[1] * rhs.array[3] + this.array[2] * rhs.array[6],
68
this.array[0] * rhs.array[1] + this.array[1] * rhs.array[4] + this.array[2] * rhs.array[7],
69
this.array[0] * rhs.array[2] + this.array[1] * rhs.array[5] + this.array[2] * rhs.array[8],
70
this.array[3] * rhs.array[0] + this.array[4] * rhs.array[3] + this.array[5] * rhs.array[6],
71
this.array[3] * rhs.array[1] + this.array[4] * rhs.array[4] + this.array[5] * rhs.array[7],
72
this.array[3] * rhs.array[2] + this.array[4] * rhs.array[5] + this.array[5] * rhs.array[8],
73
this.array[6] * rhs.array[0] + this.array[7] * rhs.array[3] + this.array[8] * rhs.array[6],
74
this.array[6] * rhs.array[1] + this.array[7] * rhs.array[4] + this.array[8] * rhs.array[7],
75
this.array[6] * rhs.array[2] + this.array[7] * rhs.array[5] + this.array[8] * rhs.array[8],
79
makeTranspose(): Matrix33{
80
return new Matrix33(new Float32Array(Array_from_number([
81
this.array[0], this.array[3], this.array[6],
82
this.array[1], this.array[4], this.array[7],
83
this.array[2], this.array[5], this.array[8]