Skip to content

Commit

Permalink
Add count to url and randomize count on reload
Browse files Browse the repository at this point in the history
  • Loading branch information
GniLudio committed May 18, 2024
1 parent 2f6314f commit 6fb560f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
21 changes: 16 additions & 5 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ let pendulum = new Pendulum();
/**
* The pendulum count (of real pendulums).
*/
let pendulumCount = Math.max(2, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed())) + 1;
let pendulumCount = parseInt(urlParameter.get("count") ?? "-1") ?? -1;
if (performance.navigation?.type == window.performance.navigation?.TYPE_RELOAD || pendulumCount == -1) {
pendulumCount = randomIntInRange(5, 15);
}
updateURLParameter('count', pendulumCount.toString());
console.log('Pendulum Count', pendulumCount);
/**
* The animation speed.
*/
let speed = Math.max(0, parseFloat(urlParameter.get("speed") ?? "1"));
let speed = parseFloat(urlParameter.get("speed") ?? "-1") ?? -1;
if (performance.navigation?.type == window.performance.navigation?.TYPE_RELOAD || speed < 0) {
speed = 1;
}
updateURLParameter('speed', speed.toString());
console.log('Speed', speed);
/**
* The time that has passed since the start. (in seconds)
*/
Expand Down Expand Up @@ -63,12 +73,13 @@ window.addEventListener("focus", _ => {
* @see https://proofwiki.org/wiki/Labeled_Tree_from_Pr%C3%BCfer_Sequence
*/
function generateRandomPendulumTree() {
const count = pendulumCount + 1;
// 0: create the pruefer sequence of length n-2 with values from 0 to n-1
const pruefer_sequence = new Array(pendulumCount - 2).fill(0).map(_ => randomIntInRange(0, pendulumCount - 1));
const pruefer_sequence = new Array(count - 2).fill(0).map(_ => randomIntInRange(0, count - 1));
// 1: generate n nodes
const nodes = new Array(pendulumCount).fill(undefined).map(_ => new Pendulum());
const nodes = new Array(count).fill(undefined).map(_ => new Pendulum());
// 2: make a list of all the integers (0, ..., n-1)
const list = new Array(pendulumCount).fill(0).map((_, i) => i);
const list = new Array(count).fill(0).map((_, i) => i);
// 3: while there are more than 2 elements in the list
while (list.length > 2) {
// 4: find the smallest number in the list which is not in the sequence
Expand Down
6 changes: 6 additions & 0 deletions build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ function randomColor() {
function colorToString(color) {
return `rgb(${color.r},${color.g}, ${color.b})`;
}
function updateURLParameter(key, value) {
const parameter = new URLSearchParams(window.location.search);
parameter.set(key, value);
const newQuery = window.location.pathname + "?" + parameter.toString();
history.pushState(null, '', newQuery);
}
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ let pendulum: Pendulum = new Pendulum();
/**
* The pendulum count (of real pendulums).
*/
let pendulumCount: number = Math.max(2, parseInt(urlParameter.get("count") ?? randomIntInRange(5, 10).toFixed())) + 1;
let pendulumCount: number = parseInt(urlParameter.get("count") ?? "-1") ?? -1;
if (performance.navigation?.type == window.performance.navigation?.TYPE_RELOAD || pendulumCount == -1) {
pendulumCount = randomIntInRange(5, 15);
}
updateURLParameter('count', pendulumCount.toString());
console.log('Pendulum Count', pendulumCount);

/**
* The animation speed.
*/
let speed: number = Math.max(0, parseFloat(urlParameter.get("speed") ?? "1"));
let speed: number = parseFloat(urlParameter.get("speed") ?? "-1") ?? -1;
if (performance.navigation?.type == window.performance.navigation?.TYPE_RELOAD || speed < 0) {
speed = 1;
}
updateURLParameter('speed', speed.toString());
console.log('Speed', speed);

/**
* The time that has passed since the start. (in seconds)
Expand All @@ -45,6 +55,7 @@ let lastDraw: number = 0;
*/
let paused: boolean = false;


// EVENTS
window.addEventListener("load", _ => {
canvas.width = window.innerWidth;
Expand Down Expand Up @@ -73,12 +84,13 @@ window.addEventListener("focus", _ => {
* @see https://proofwiki.org/wiki/Labeled_Tree_from_Pr%C3%BCfer_Sequence
*/
function generateRandomPendulumTree(): void {
const count = pendulumCount + 1;
// 0: create the pruefer sequence of length n-2 with values from 0 to n-1
const pruefer_sequence: number[] = new Array(pendulumCount-2).fill(0).map(_ => randomIntInRange(0, pendulumCount-1));
const pruefer_sequence: number[] = new Array(count-2).fill(0).map(_ => randomIntInRange(0, count-1));
// 1: generate n nodes
const nodes: Pendulum[] = new Array(pendulumCount).fill(undefined).map(_ => new Pendulum());
const nodes: Pendulum[] = new Array(count).fill(undefined).map(_ => new Pendulum());
// 2: make a list of all the integers (0, ..., n-1)
const list: number[] = new Array(pendulumCount).fill(0).map((_, i) => i);
const list: number[] = new Array(count).fill(0).map((_, i) => i);

// 3: while there are more than 2 elements in the list
while (list.length > 2) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ function randomColor(): RGBAColor {

function colorToString(color: RGBAColor): string {
return `rgb(${color.r},${color.g}, ${color.b})`;
}

function updateURLParameter(key: string, value: string): void {
const parameter = new URLSearchParams(window.location.search);
parameter.set(key, value);
const newQuery = window.location.pathname + "?" + parameter.toString();
history.pushState(null, '', newQuery);
}

0 comments on commit 6fb560f

Please sign in to comment.