Use param props in examples

This commit is contained in:
Andrew Morris
2023-07-06 17:13:52 +10:00
parent 57b3a6b543
commit 711ad1e762
10 changed files with 97 additions and 215 deletions

View File

@@ -94,13 +94,10 @@ type StrengthValue = 0 | 1 | 2 | 3 | 4 | 5 | 6;
*/
class Strength {
strengthValue;
name;
constructor(strengthValue: StrengthValue, name: string) {
this.strengthValue = strengthValue;
this.name = name;
}
constructor(
public strengthValue: StrengthValue,
public name: string,
) {}
static stronger(s1: Strength, s2: Strength) {
return s1.strengthValue < s2.strengthValue;
@@ -156,11 +153,9 @@ class Strength {
* to represent a constraint.
*/
abstract class Constraint {
strength;
constructor(strength: Strength) {
this.strength = strength;
}
constructor(
public strength: Strength,
) {}
abstract addToGraph(): void;
abstract removeFromGraph(): void;
@@ -356,15 +351,11 @@ const Direction = {
* variables.
*/
abstract class BinaryConstraint extends Constraint {
v1;
v2;
direction;
constructor(var1: Variable, var2: Variable, strength: Strength) {
constructor(public v1: Variable, public v2: Variable, strength: Strength) {
super(strength);
this.v1 = var1;
this.v2 = var2;
this.direction = Direction.NONE;
}
@@ -477,20 +468,16 @@ abstract class BinaryConstraint extends Constraint {
*/
class ScaleConstraint extends BinaryConstraint {
direction;
scale;
offset;
constructor(
src: Variable,
scale: Variable,
offset: Variable,
public scale: Variable,
public offset: Variable,
dest: Variable,
strength: Strength,
) {
super(src, dest, strength);
this.direction = Direction.NONE;
this.scale = scale;
this.offset = offset;
}
/**
@@ -572,23 +559,16 @@ class EqualityConstraint extends BinaryConstraint {
* constraint solver.
*/
class Variable {
value;
constraints;
determinedBy: Constraint | null;
mark;
walkStrength;
stay;
name;
constraints = new OrderedCollection<Constraint>();
determinedBy: Constraint | null = null;
mark = 0;
walkStrength = Strength.WEAKEST;
stay = true;
constructor(name: string, initialValue = 0) {
this.value = initialValue;
this.constraints = new OrderedCollection<Constraint>();
this.determinedBy = null;
this.mark = 0;
this.walkStrength = Strength.WEAKEST;
this.stay = true;
this.name = name;
}
constructor(
public name: string,
public value = 0,
) {}
/**
* Add the given constraint to the set of all constraints that refer

View File

@@ -11,13 +11,10 @@ export default function () {
}
class Range {
start: number;
end: number;
constructor(start: number, end: number) {
this.start = start;
this.end = end;
}
constructor(
public start: number,
public end: number,
) {}
[Symbol.iterator]() {
return new RangeIterator(this.start, this.end);
@@ -25,13 +22,7 @@ class Range {
}
class RangeIterator {
value: number;
end: number;
constructor(value: number, end: number) {
this.value = value;
this.end = end;
}
constructor(public value: number, public end: number) {}
next() {
const done = this.value >= this.end;

View File

@@ -20,14 +20,10 @@ export default function () {
}
class SmallQueue<T extends {}> {
items: T[];
constructor(items: T[]) {
constructor(public items: T[]) {
if (items.length > 3) {
throw new Error(`${items.length} is too many items for SmallQueue`);
}
this.items = items;
}
pop(): T {

View File

@@ -5,11 +5,8 @@ export default function () {
}
class CustomError /* extends Error */ {
code: number;
message: string;
constructor(code: number, message: string) {
this.code = code;
this.message = message;
}
constructor(
public code: number,
public message: string,
) {}
}

View File

@@ -1,11 +1,9 @@
import { primes } from "../projEuler/helpers/primes.ts";
export default class Range<T = never> implements Iterable<T> {
iterable: Iterable<T>;
constructor(iterable: Iterable<T>) {
this.iterable = iterable;
}
constructor(
public iterable: Iterable<T>,
) {}
[Symbol.iterator]() {
return this.iterable[Symbol.iterator]();

View File

@@ -25,11 +25,9 @@ export default function main() {
class MergeSortStepper<T> {
tree: MergeSortNode<T>;
cmp: (a: T, b: T) => number;
constructor(vals: T[], cmp: (a: T, b: T) => number) {
constructor(vals: T[], public cmp: (a: T, b: T) => number) {
this.tree = makeTree(vals);
this.cmp = cmp;
}
step(): boolean {
@@ -38,11 +36,9 @@ class MergeSortStepper<T> {
}
class MergeSortNode<T> {
data: MergeSortNodeData<T>;
constructor(data: MergeSortNodeData<T>) {
this.data = data;
}
constructor(
public data: MergeSortNodeData<T>,
) {}
step(cmp: (a: T, b: T) => number): boolean {
if (this.data.type === "sorted") {

View File

@@ -135,15 +135,11 @@ class Color {
}
class Light {
position;
color;
intensity;
constructor(pos: Vector, color: Color, intensity = 10) {
this.position = pos;
this.color = color;
this.intensity = intensity;
}
constructor(
public position: Vector,
public color: Color,
public intensity = 10,
) {}
toString() {
return "Light [" + this.position.x + "," + this.position.y + "," +
@@ -152,15 +148,11 @@ class Light {
}
class Vector {
x;
y;
z;
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
constructor(
public x = 0,
public y = 0,
public z = 0,
) {}
copy(vector: Vector) {
this.x = vector.x;
@@ -264,26 +256,17 @@ function wrapUpMaterial(t: number) {
}
class SolidMaterial implements Material {
reflection: number;
refraction: number;
transparency: number;
gloss: number;
hasTexture: boolean;
color;
constructor(
color: Color,
reflection: number,
public color: Color,
public reflection: number,
_refraction: number,
transparency: number,
gloss: number,
public transparency: number,
public gloss: number,
) {
this.color = color;
this.reflection = reflection;
this.refraction = 0.5; // TODO: Why not use parameter?
this.transparency = transparency;
this.gloss = gloss;
this.hasTexture = false;
}
@@ -298,33 +281,17 @@ class SolidMaterial implements Material {
}
class ChessboardMaterial implements Material {
reflection: number;
refraction: number;
transparency: number;
gloss: number;
hasTexture: boolean;
colorEven;
colorOdd;
density;
refraction = 0.5;
hasTexture = true;
constructor(
colorEven: Color,
colorOdd: Color,
reflection: number,
transparency: number,
gloss: number,
density: number,
) {
this.colorEven = colorEven;
this.colorOdd = colorOdd;
this.reflection = reflection;
this.refraction = 0.5;
this.transparency = transparency;
this.gloss = gloss;
this.density = density;
this.hasTexture = true;
}
public colorEven: Color,
public colorOdd: Color,
public reflection: number,
public transparency: number,
public gloss: number,
public density: number,
) {}
getColor(u: number, v: number) {
let t = wrapUpMaterial(u * this.density) * wrapUpMaterial(v * this.density);
@@ -350,15 +317,11 @@ type Shape = {
};
class Sphere implements Shape {
radius;
position;
material;
constructor(pos: Vector, radius: number, material: Material) {
this.radius = radius;
this.position = pos;
this.material = material;
}
constructor(
public position: Vector,
public radius: number,
public material: Material,
) {}
intersect(ray: Ray) {
let info = new IntersectionInfo();
@@ -402,15 +365,11 @@ class Sphere implements Shape {
}
class Plane implements Shape {
position;
d;
material;
constructor(pos: Vector, d: number, material: Material) {
this.position = pos;
this.d = d;
this.material = material;
}
constructor(
public position: Vector,
public d: number,
public material: Material,
) {}
intersect(ray: Ray) {
let info = new IntersectionInfo();
@@ -474,16 +433,14 @@ class IntersectionInfo {
}
class Camera {
position;
lookAt;
equator;
up;
screen;
constructor(pos: Vector, lookAt: Vector, up: Vector) {
this.position = pos;
this.lookAt = lookAt;
this.up = up;
constructor(
public position: Vector,
public lookAt: Vector,
public up: Vector,
) {
this.equator = lookAt.normalize().cross(this.up);
this.screen = Vector.add(
this.position,
@@ -516,13 +473,10 @@ class Camera {
}
class Background {
color;
ambience;
constructor(color: Color, ambience: number) {
this.color = color;
this.ambience = ambience;
}
constructor(
public color: Color,
public ambience: number,
) {}
}
type EngineOptions = {
@@ -539,15 +493,12 @@ type EngineOptions = {
class Engine {
canvas: unknown = null; /* 2d context we can render to */
options;
// Variable used to hold a number that can be used to verify that
// the scene was ray traced correctly.
checkNumber = 0;
constructor(options: EngineOptions) {
this.options = options;
constructor(public options: EngineOptions) {
this.options.canvasHeight /= this.options.pixelHeight;
this.options.canvasWidth /= this.options.pixelWidth;

View File

@@ -335,11 +335,6 @@ const STATE_SUSPENDED_RUNNABLE = 3 /* STATE_SUSPENDED | STATE_RUNNABLE */;
* with it.
*/
class TaskControlBlock {
link: number | null;
id: number;
priority: number;
queue: Packet | null;
task: Task;
state: number;
/**
@@ -351,18 +346,12 @@ class TaskControlBlock {
* @constructor
*/
constructor(
link: number | null,
id: number,
priority: number,
queue: Packet | null,
task: Task,
public link: number | null,
public id: number,
public priority: number,
public queue: Packet | null,
public task: Task,
) {
this.link = link;
this.id = id;
this.priority = priority;
this.queue = queue;
this.task = task;
if (queue == null) {
this.state = STATE_SUSPENDED;
} else {
@@ -445,17 +434,14 @@ type Task = {
* device tasks.
*/
class IdleTask implements Task {
v1: number;
count: number;
/**
* @param {int} v1 a seed value that controls how the device tasks are scheduled
* @param {int} count the number of times this task should be scheduled
*/
constructor(v1: number, count: number) {
this.v1 = v1;
this.count = count;
}
constructor(
public v1: number,
public count: number,
) {}
run(_packet: Packet | null) {
this.count--;
@@ -614,19 +600,14 @@ class Packet {
* @constructor
*/
link: Packet | null;
id: number | null;
kind: number;
a1: number;
a2: (number | null)[];
a1 = 0;
a2: (number | null)[] = [null, null, null, null]; // new Array(DATA_SIZE);
constructor(link: Packet | null, id: number, kind: number) {
this.link = link;
this.id = id;
this.kind = kind;
this.a1 = 0;
this.a2 = [null, null, null, null]; // new Array(DATA_SIZE);
}
constructor(
public link: Packet | null,
public id: number | null,
public kind: number,
) {}
/**
* Add this packet to the end of a worklist, and return the worklist.

View File

@@ -20,14 +20,10 @@ export default function main() {
}
class SmallQueue<T extends NotNullish> {
items: T[];
constructor(items: T[]) {
constructor(public items: T[]) {
if (items.length > 3) {
throw new Error(`${items.length} is too many items for SmallQueue`);
}
this.items = items;
}
pop(): T {

View File

@@ -1,11 +1,7 @@
import { primes } from "./primes.ts";
export default class Range<T = never> implements Iterable<T> {
iterable: Iterable<T>;
constructor(iterable: Iterable<T>) {
this.iterable = iterable;
}
constructor(public iterable: Iterable<T>) {}
[Symbol.iterator]() {
return this.iterable[Symbol.iterator]();