Skip to content

Commit

Permalink
update vec3
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese committed Aug 24, 2023
1 parent 721f7a5 commit 6ed85f6
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 195 deletions.
4 changes: 1 addition & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vec3, Space, vec3, put, view } from './src/vector.js';
import { Vec3, Space, vec3 } from './src/vector.js';
import * as Exp from './src/expression.js';
import * as Generator from './src/generator.js';
import * as Transform from './src/transform.js';
Expand All @@ -16,8 +16,6 @@ export {
Vec3,
Space,
vec3,
put,
view,
Exp,
Symmetry,
Generator,
Expand Down
2 changes: 1 addition & 1 deletion src/expression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-types */
import { Vec3, Space, vec3 } from './vector.js';
import { Space, vec3 } from './vector.js';

function equation(
expr: string,
Expand Down
42 changes: 41 additions & 1 deletion src/lineamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class Matrix {

deepCopy(): Matrix {
const result = new Matrix(this.row, this.column);

result.matrix = this.matrix.map((row) => [...row]);

return result;
}

Expand All @@ -27,52 +29,67 @@ class Matrix {

add(b: Matrix): Matrix {
if (this.row != b.row || this.column != b.column) throw new Error('Matrix size error');

const result = this.deepCopy();

result.matrix = result.matrix.map((row, i) =>
row.map((value, j) => value + b.matrix[i][j])
);

return result;
}

sub(b: Matrix): Matrix {
if (this.row != b.row || this.column != b.column) throw new Error('Matrix size error');

const result = this.deepCopy();

result.matrix = result.matrix.map((row, i) =>
row.map((value, j) => value - b.matrix[i][j])
);

return result;
}

scala(scalar: number): Matrix {
const result = this.deepCopy();

result.matrix = result.matrix.map((row) => row.map((value) => value * scalar));

return result;
}

mul(b: Matrix): Matrix {
if (this.column != b.row) throw new Error('Matrix size error');

const result = new Matrix(this.row, b.column);

for (let i = 0; i < this.row; ++i)
for (let k = 0; k < this.column; ++k)
for (let j = 0; j < b.column; ++j)
result.matrix[i][j] += this.matrix[i][k] * b.matrix[k][j];

return result;
}

pow(p: number): Matrix {
let a = this.deepCopy(),
result = unit(this.row);

while (p) {
if (p & 1) result = result.mul(a);
p >>= 1;
a = a.mul(a);
}

return result;
}

map(f: (v: number) => number): Matrix {
const result = this.deepCopy();

result.matrix = result.matrix.map((row) => row.map(f));

return result;
}

Expand All @@ -82,46 +99,61 @@ class Matrix {

equal(b: Matrix): boolean {
if (this.row != b.row || this.column != b.column) return false;

return this.matrix.every((row, i) => row.every((value, j) => value === b.matrix[i][j]));
}

transpose(): Matrix {
const result = new Matrix(this.column, this.row);

result.matrix = this.matrix[0].map((_, i) => this.matrix.map((row) => row[i]));

return result;
}

swapRow(a: number, b: number): Matrix {
if (a > this.row || b > this.row) throw new Error('The row is too big');

const result = this.deepCopy();

[result.matrix[a], result.matrix[b]] = [result.matrix[b], result.matrix[a]];

return result;
}

swapColumn(a: number, b: number): Matrix {
if (a > this.column || b > this.column) throw new Error('The column is too big');

const result = this.deepCopy();

result.matrix.forEach((row) => {
[row[a], row[b]] = [row[b], row[a]];
});

return result;
}

flipHorizontal(): Matrix {
const result = this.deepCopy();

result.matrix = result.matrix.reverse();

return result;
}

flipVertical(): Matrix {
const result = this.deepCopy();

result.matrix.forEach((row) => row.reverse());

return result;
}

flipMdiagonal(): Matrix {
if (this.row != this.column) throw new Error('The row must be equal to the column');

const result = this.deepCopy();

result.matrix.forEach((row, i) => {
row.forEach((_, j) => {
if (j > i)
Expand All @@ -131,12 +163,15 @@ class Matrix {
];
});
});

return result;
}

flipSdiagonal(): Matrix {
if (this.row != this.column) throw new Error('The row must be equal to the column');

const result = this.deepCopy();

result.matrix.forEach((row, i) => {
row.forEach((_, j) => {
if (j < result.row - i) {
Expand All @@ -149,6 +184,7 @@ class Matrix {
}
});
});

return result;
}

Expand All @@ -159,13 +195,17 @@ class Matrix {

function unit(n: number): Matrix {
const result = new Matrix(n, n);

for (let i = 0; i < n; ++i) result.matrix[i][i] = 1;

return result;
}

function fromArray(A: Array<Array<number>>): Matrix {
function fromArray(A: number[][]): Matrix {
const temp = new Matrix(A.length, A[0].length);

temp.matrix = A.map((row) => [...row]);

return temp;
}

Expand Down
Empty file removed src/physics.ts
Empty file.
8 changes: 4 additions & 4 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vec3, Space, put, view, vec3 } from './vector.js';
import { Vec3, Space, vec3 } from './vector.js';
import { fromArray } from './lineamp.js';

function embed(base: Space, target: Space) {
Expand All @@ -13,9 +13,9 @@ function embed(base: Space, target: Space) {
// Swap The Direction of the Structure
function swap(v: Space, d1: number, d2: number): Space {
return v.map((b) => {
const k = view(b);
const k = b.view();
[k[d1], k[d2]] = [k[d2], k[d1]];
return put(k);
return vec3(...k);
});
}

Expand Down Expand Up @@ -123,7 +123,6 @@ function round_pos(v: Space): Space {
}

export {
put,
round_pos,
scale,
diffusion,
Expand All @@ -134,6 +133,7 @@ export {
center,
moveTo,
pipe,
duplicate,
array_gen,
array_gen_fn,
reduce_pos
Expand Down
6 changes: 3 additions & 3 deletions src/turtle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vec3, vec3, put, Space } from './vector.js';
import { Vec3, vec3, Space } from './vector.js';
import { Matrix, fromArray } from './lineamp.js';
import { lineVoxel } from './generator.js';
class Turtle2D {
Expand Down Expand Up @@ -247,11 +247,11 @@ class Turtle3D {

forward(d: number): void {
const heading = this.getHeading();
const newPos = put([
const newPos = vec3(
this.pos.x + heading.x * d,
this.pos.y + heading.y * d,
this.pos.z + heading.z * d
]);
);
this.track = this.track.concat(lineVoxel(this.pos, newPos));
this.pos = newPos;
}
Expand Down
26 changes: 13 additions & 13 deletions src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Vec3 {
}

scale(scalar: number): Vec3 {
return new Vec3(this.x * scalar, this.y * scalar, this.z * scalar);
return this.map((v) => v * scalar);
}

equals(other: Vec3): boolean {
Expand All @@ -52,44 +52,44 @@ class Vec3 {

normalize(): Vec3 {
const mag = this.magnitude();

if (mag === 0) {
return new Vec3(0, 0, 0);
}
return new Vec3(this.x / mag, this.y / mag, this.z / mag);

return this.map((v) => v / mag);
}

magnitude(): number {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
return Math.sqrt(this.dot(this));
}

angle(other: Vec3): number {
const dotProduct = this.dot(other);
const magProduct = this.magnitude() * other.magnitude();

return Math.acos(dotProduct / magProduct);
}

projectOnto(other: Vec3): Vec3 {
const scalar = this.dot(other) / other.magnitude() ** 2;

return other.scale(scalar);
}

negate(): Vec3 {
return new Vec3(-this.x, -this.y, -this.z);
return this.map((v) => -v);
}

view(): [x: number, y: number, z: number] {
return [this.x, this.y, this.z];
}
}

function vec3(x: number, y: number, z: number): Vec3 {
return new Vec3(x, y, z);
}

function view(v: Vec3): number[] {
return [v.x, v.y, v.z];
}

function put(k: number[]): Vec3 {
return vec3(k[0], k[1], k[2]);
}

type Space = Vec3[];

export { Vec3, Space, view, put, vec3 };
export { Vec3, Space, vec3 };
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions":{
"target":"es2020",
"moduleResolution":"node",
"moduleResolution":"classic",
"module":"es2020",
"declaration":true,
"noLib":false,
Expand Down
Loading

0 comments on commit 6ed85f6

Please sign in to comment.