Skip to content

Commit

Permalink
fix: Buggy zooming in / out in graphing mode & style: Improve code st…
Browse files Browse the repository at this point in the history
…ructure
  • Loading branch information
NriotHrreion committed Dec 2, 2023
1 parent 488f20e commit c3a1df2
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 119 deletions.
4 changes: 2 additions & 2 deletions src/compiler/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import VariableToken from "@/compiler/token/VariableToken";

import { functions, constants } from "@/global";
import { Operator, NumberSys } from "@/types";
import type { MathFunction } from "@/types";
import type { FunctionInfo } from "@/types";

export type NumberSymbol = string;

Expand All @@ -32,7 +32,7 @@ export default class Compiler {

private layer: number = 0;
private inAbs: boolean = false;
private currentFunction: MathFunction | null = null;
private currentFunction: FunctionInfo | null = null;
private secondaryRaw: string[] = [];
private hasError: boolean = false;

Expand Down
6 changes: 3 additions & 3 deletions src/compiler/token/FunctionToken.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { MathFunction } from "@/types";
import type { FunctionInfo } from "@/types";
import Token, { TokenType } from "@/compiler/token/Token";

export default class FunctionToken extends Token<void> {
public readonly type: TokenType = TokenType.FUNCTION;

public func: MathFunction[0];
public func: FunctionInfo[0];
public param: Token[];

public constructor(func: MathFunction[0], param: Token[]) {
public constructor(func: FunctionInfo[0], param: Token[]) {
super();

this.func = func;
Expand Down
4 changes: 2 additions & 2 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Compute from "@/compiler/Compute";

import Emitter from "@/utils/Emitter";
import { MathFunction, Shortcut, RollbackToward } from "@/types";
import { FunctionInfo, Shortcut, RollbackToward } from "@/types";

export const version = "1.3.3";
export const errorText = "\\text{Error}";

export const functions: Map<string, MathFunction> = new Map([
export const functions: Map<string, FunctionInfo> = new Map([
["sin", [(x) => Math.sin(x), 1]],
["cos", [(x) => Math.cos(x), 1]],
["tan", [(x) => Compute.safeTan(x), 1]],
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export enum Operator {
RSH = "rsh",
}

export type MathFunction = [(...params: number[]) => number, number /* amount of params */];
export type FunctionInfo = [(...params: number[]) => number, number /* amount of params */];

export interface RenderedFunction {
id: number
Expand Down
17 changes: 17 additions & 0 deletions src/utils/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ export default class Collection<T = any> extends List<T> {
public shift(): T | undefined {
return this.value.shift();
}

public put(index: number, item: T): void {
var temp;

for(let i = index; i < this.length; i++) {
if(!temp) {
temp = this.set(i, item);
continue;
}

temp = this.set(i, temp);

if(i === this.length - 1) {
this.add(temp);
}
}
}
}
4 changes: 3 additions & 1 deletion src/utils/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export default class List<T = any> {
return this.value[index];
}

public set(index: number, item: T): void {
public set(index: number, item: T): T {
var oldItem = this.value[index];
this.value[index] = item;
return oldItem;
}

public remove(index: number): void {
Expand Down
32 changes: 32 additions & 0 deletions src/workers/Function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import RootToken from "@/compiler/token/RootToken";
import Evaluator from "@/compiler/Evaluator";
import { delta } from "@/workers/Render";

export default class Function {
public root: RootToken;

public constructor(root: RootToken) {
this.root = root;
}

public calculate(x: number): number {
return new Evaluator(this.root, new Map([["x", x.toString()]])).evaluate();
}

public play(workerCtx: Worker): void {
var rawPitches: number[] = [];

for(let x = -8; x <= 8; x += delta) {
var y = this.calculate(x);

rawPitches.push(y);
}

const min = Math.min(...rawPitches);
for(let i = 0; i < rawPitches.length && min < 0; i++) {
rawPitches[i] += -min;
}

workerCtx.postMessage({ type: "play", rawPitches });
}
}
24 changes: 24 additions & 0 deletions src/workers/Point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Render from "@/workers/Render";

export default class Point {
private renderer: Render;

public x: number;
public y: number;

public constructor(renderer: Render, x: number, y: number) {
this.renderer = renderer;
this.x = x;
this.y = y;
}

public toCoordinates(): Point {
var unitPx = this.renderer.scale;
return new Point(this.renderer, (this.x - this.renderer.center.x) / unitPx, -(this.y - this.renderer.center.y) / unitPx);
}

public toScreen(): Point {
var unitPx = this.renderer.scale;
return new Point(this.renderer, this.renderer.center.x + (this.x * unitPx), this.renderer.center.y - (this.y * unitPx));
}
}
Loading

1 comment on commit c3a1df2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.