Add glMatrix

This commit is contained in:
schmelczerandras 2020-07-22 20:00:57 +02:00
parent 582979d3ed
commit e88b4589d2
26 changed files with 211 additions and 358 deletions

View file

@ -1,78 +0,0 @@
// https://github.com/Azleur/mat3/blob/master/src/index.ts
import { Vec2 } from './vec2';
export class Mat3 {
constructor(private readonly values: Array<Array<number>>) {}
public static get Zero() {
return new Mat3([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
}
public static get Id() {
return new Mat3([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]);
}
public static get Ones() {
return new Mat3([
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
]);
}
public static translateMatrix(by: Vec2): Mat3 {
return new Mat3([
[1, 0, 0],
[0, 1, 0],
[by.x, by.y, 1],
]);
}
public static scaleMatrix(by: Vec2): Mat3 {
return new Mat3([
[by.x, 0, 0],
[0, by.y, 0],
[0, 0, 1],
]);
}
public get transposed(): Mat3 {
const values: number[][] = [[], [], []];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
values[i][j] = this.values[j][i];
}
}
return new Mat3(values);
}
public times(other: Mat3): Mat3 {
const values: number[][] = Mat3.Zero.values;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
for (let k = 0; k < 3; k++) {
values[i][j] += this.values[i][k] * other.values[k][j];
}
}
}
return new Mat3(values);
}
public get transposedFlat(): Array<number> {
const transposed = this.transposed;
return [
...transposed.values[0],
...transposed.values[1],
...transposed.values[2],
];
}
}

View file

@ -1,16 +1,16 @@
import { Typed } from '../transport/serializable';
import { Vec2 } from './vec2';
import { vec2 } from 'gl-matrix';
export class Rectangle extends Typed {
public constructor(
public topLeft: Vec2 = new Vec2(),
public size: Vec2 = new Vec2()
public topLeft: vec2 = vec2.create(),
public size: vec2 = vec2.create()
) {
super();
}
public isInside(position: Vec2): boolean {
const translated = position.subtract(this.topLeft);
public isInside(position: vec2): boolean {
const translated = vec2.subtract(vec2.create(), position, this.topLeft);
return (
0 <= translated.x &&
translated.x < this.size.x &&

View file

@ -1,50 +0,0 @@
import { Typed } from '../transport/serializable';
export class Vec2 extends Typed {
public constructor(public x: number = 0.0, public y: number = null) {
super();
if (this.y === null) {
this.y = this.x;
}
}
public scale(scalar: number): Vec2 {
return new Vec2(this.x * scalar, this.y * scalar);
}
public add(other: Vec2): Vec2 {
return new Vec2(this.x + other.x, this.y + other.y);
}
public subtract(other: Vec2): Vec2 {
return new Vec2(this.x - other.x, this.y - other.y);
}
public times(other: Vec2): Vec2 {
return new Vec2(this.x * other.x, this.y * other.y);
}
public divide(other: Vec2): Vec2 {
return new Vec2(this.x / other.x, this.y / other.y);
}
public get length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public get normalized(): Vec2 {
return this.scale(1 / this.length);
}
public get clamped_0_1(): Vec2 {
return new Vec2(
Math.min(1, Math.max(0, this.x)),
Math.min(1, Math.max(0, this.y))
);
}
public get list(): [number, number] {
return [this.x, this.y];
}
}