Skip to content

Commit

Permalink
update sort simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
HoangTran0410 committed Jul 29, 2023
1 parent 3b976a3 commit ee2cb97
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions 2022/sort-simulator-2/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function sleep(ms) {
}

// https://bost.ocks.org/mike/shuffle/
async function shuffleArray(array) {
async function shuffleArray(array, willSleep = true) {
var m = array.length,
t,
i;
Expand All @@ -25,7 +25,7 @@ async function shuffleArray(array) {

highlightIndexes[0] = m;
highlightIndexes[1] = i;
await sleep(sleepTime);
await sleep(willSleep ? sleepTime : 0);
}

highlightIndexes = [];
Expand Down
50 changes: 33 additions & 17 deletions 2022/sort-simulator-2/sketch.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
let arr = [];
let highlightIndexes = [];
let sleepTime = 10;
let arraySize = 100;
let arrToAnimations = [];

let delaySlider;
let delayInput, arrSizeInput;
let isSorting = false;

const lineWidth = 7;

async function runSort(sortFunc, array, button) {
if (!isSorting) {
button.classList.add("loading");
Expand All @@ -27,18 +26,34 @@ function getCurrentState() {
}));
}

function createArray() {
for (let i = 0; i < arraySize; i++) arr.push(map(i, 0, arraySize, 0, 100));
arrToAnimations = getCurrentState();
shuffleArray(arr, false);
}

function setup() {
createCanvas(min(windowWidth, 800), 500);
colorMode(HSB, 100);

let len = ~~(width / lineWidth);
for (let i = 0; i < len; i++) arr.push(map(i, 0, len, 0, 100));
arrToAnimations = getCurrentState();
console.log(arrToAnimations);

shuffleArray(arr);

delaySlider = createSlider(0, 100, 10, 2);
createArray(arraySize);

createElement("lable", "Delay (ms):");
delayInput = createInput("10");
delayInput.attribute("type", "number");
delayInput.attribute("min", "0");
delayInput.attribute("max", "1000");
delayInput.attribute("step", "10");

createElement("lable", "Array size:");
arrSizeInput = createInput("100");
arrSizeInput.attribute("type", "number");
arrSizeInput.attribute("min", "10");
arrSizeInput.attribute("max", "400");
arrSizeInput.input(() => {
arraySize = arrSizeInput.value();
createArray();
});

createButton("Shuffle").mouseClicked((e) => {
runSort(shuffleArray, arr, e.target);
Expand Down Expand Up @@ -76,7 +91,7 @@ function setup() {
function draw() {
background(20);

sleepTime = delaySlider.value();
sleepTime = delayInput.value();

noStroke();
for (let i = arrToAnimations.length - 1; i >= 0; i--) {
Expand All @@ -92,21 +107,22 @@ function draw() {
lerpSpeed
);

let w = width / arraySize;
let h = map(value, 0, 100, 0, height);
let top = height - h;
let left = currentIndex * lineWidth;
let left = currentIndex * w;

// fill(value, 255, 100);
// rect(left, top, lineWidth, h);
// rect(left, top, w, h);
stroke(value, 255, 100);
strokeWeight(lineWidth - 2);
line(left + lineWidth / 2, top, left + 50, height);
strokeWeight(w - 2);
line(left + w / 2, top, left + 50, height);

let hi = highlightIndexes.indexOf(indexInArr);
if (hi >= 0) {
fill(255);
noStroke();
rect(left, 0, lineWidth, height - h);
rect(left, 0, w, height - h);
}
}
}
13 changes: 11 additions & 2 deletions 2022/sort-simulator-2/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ body {
margin: 0;
padding: 0;
background-color: #151515;
color: #eee;
}

body {
Expand Down Expand Up @@ -46,16 +47,24 @@ button.loading::after {
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}

@keyframes changeLetter {
0% {
content: ' 🤨';
}

50% {
content: ' 🤯';
}

100% {
content: ' 🤔';
}
Expand Down

0 comments on commit ee2cb97

Please sign in to comment.