- Go backend (server/)
- Frontend (web/, server/static/)
- Database and deployment files
- Scripts and docs
Co-Authored-By: 狸花猫/Claude-Qwen3.6-Plus 🐾
102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
/**
|
|
* @fileoverview 矩阵运算,本来是要引入 gl-matrix, 但是考虑到 g-mobile 对大小有限制,同时 g-webgl 使用的 matrix 不一致
|
|
* 所以,这里仅实现 2D 几个运算,上层自己引入 gl-matrix
|
|
* @author dxq613@gmail.com
|
|
*/
|
|
|
|
/**
|
|
* 3阶矩阵相乘
|
|
* @param {number[]} a 矩阵1
|
|
* @param {number[]} b 矩阵2
|
|
*/
|
|
export function multiplyMatrix(a: number[], b: number[]) {
|
|
const out = [];
|
|
const a00 = a[0];
|
|
const a01 = a[1];
|
|
const a02 = a[2];
|
|
|
|
const a10 = a[3];
|
|
const a11 = a[4];
|
|
const a12 = a[5];
|
|
const a20 = a[6];
|
|
const a21 = a[7];
|
|
const a22 = a[8];
|
|
|
|
const b00 = b[0];
|
|
const b01 = b[1];
|
|
const b02 = b[2];
|
|
const b10 = b[3];
|
|
const b11 = b[4];
|
|
const b12 = b[5];
|
|
const b20 = b[6];
|
|
const b21 = b[7];
|
|
const b22 = b[8];
|
|
|
|
out[0] = b00 * a00 + b01 * a10 + b02 * a20;
|
|
out[1] = b00 * a01 + b01 * a11 + b02 * a21;
|
|
out[2] = b00 * a02 + b01 * a12 + b02 * a22;
|
|
|
|
out[3] = b10 * a00 + b11 * a10 + b12 * a20;
|
|
out[4] = b10 * a01 + b11 * a11 + b12 * a21;
|
|
out[5] = b10 * a02 + b11 * a12 + b12 * a22;
|
|
|
|
out[6] = b20 * a00 + b21 * a10 + b22 * a20;
|
|
out[7] = b20 * a01 + b21 * a11 + b22 * a21;
|
|
out[8] = b20 * a02 + b21 * a12 + b22 * a22;
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* 3阶矩阵同2阶向量相乘
|
|
* @param {number[]} m 矩阵
|
|
* @param {number[]} v 二阶向量
|
|
*/
|
|
export function multiplyVec2(m: number[], v: number[]) {
|
|
const out = [];
|
|
const x = v[0];
|
|
const y = v[1];
|
|
out[0] = m[0] * x + m[3] * y + m[6];
|
|
out[1] = m[1] * x + m[4] * y + m[7];
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* 矩阵的逆
|
|
* @param {number[]} a 矩阵
|
|
*/
|
|
export function invert(a: number[]) {
|
|
const out = [];
|
|
const a00 = a[0];
|
|
const a01 = a[1];
|
|
const a02 = a[2];
|
|
const a10 = a[3];
|
|
const a11 = a[4];
|
|
const a12 = a[5];
|
|
const a20 = a[6];
|
|
const a21 = a[7];
|
|
const a22 = a[8];
|
|
|
|
const b01 = a22 * a11 - a12 * a21;
|
|
const b11 = -a22 * a10 + a12 * a20;
|
|
const b21 = a21 * a10 - a11 * a20;
|
|
|
|
// Calculate the determinant
|
|
let det = a00 * b01 + a01 * b11 + a02 * b21;
|
|
|
|
if (!det) {
|
|
return null;
|
|
}
|
|
det = 1.0 / det;
|
|
|
|
out[0] = b01 * det;
|
|
out[1] = (-a22 * a01 + a02 * a21) * det;
|
|
out[2] = (a12 * a01 - a02 * a11) * det;
|
|
out[3] = b11 * det;
|
|
out[4] = (a22 * a00 - a02 * a20) * det;
|
|
out[5] = (-a12 * a00 + a02 * a10) * det;
|
|
out[6] = b21 * det;
|
|
out[7] = (-a21 * a00 + a01 * a20) * det;
|
|
out[8] = (a11 * a00 - a01 * a10) * det;
|
|
return out;
|
|
}
|