fin
This commit is contained in:
parent
d6c8ae6f21
commit
117f99087f
36 changed files with 123064 additions and 249 deletions
18
7/index.html
Normal file
18
7/index.html
Normal file
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<title>Sketch</title>
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="style.css">
|
||||
|
||||
<script src="libraries/p5.min.js"></script>
|
||||
<script src="libraries/p5.sound.min.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="sketch.js"></script>
|
||||
</body>
|
||||
</html>
|
10
7/jsconfig.json
Normal file
10
7/jsconfig.json
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6"
|
||||
},
|
||||
"include": [
|
||||
"*.js",
|
||||
"**/*.js",
|
||||
"/home/melody/.var/app/com.visualstudio.code/data/vscode/extensions/samplavigne.p5-vscode-1.2.16/p5types/global.d.ts"
|
||||
]
|
||||
}
|
2
7/libraries/p5.min.js
vendored
Normal file
2
7/libraries/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
3
7/libraries/p5.sound.min.js
vendored
Normal file
3
7/libraries/p5.sound.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
165
7/sketch.js
Normal file
165
7/sketch.js
Normal file
|
@ -0,0 +1,165 @@
|
|||
const numBars = 100;
|
||||
const barWidth = 20;
|
||||
const maxBarHeight = 200;
|
||||
const barColor = [255, 127, 127];
|
||||
const barSpacing = 5;
|
||||
const swapRate = 10;
|
||||
let oldBars = [];
|
||||
let osc;
|
||||
|
||||
function isSorted(arr) {
|
||||
for (let i = 0; i < arr.length - 1; i++) {
|
||||
if (arr[i] > arr[i + 1]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function refreshBars(p, bars) {
|
||||
const barX = (i) => i * (barWidth + barSpacing) + barSpacing;
|
||||
p.clear();
|
||||
p.background(10);
|
||||
for (let i = 0; i < numBars; i++) {
|
||||
p.fill(barColor);
|
||||
p.rect(barX(i), p.height - bars[i], barWidth, bars[i]);
|
||||
}
|
||||
diffIndexArray = bars
|
||||
.map((val, i) => (val !== oldBars[i] ? i : -1))
|
||||
.filter((i) => i !== -1);
|
||||
for (bar of diffIndexArray) {
|
||||
p.fill(180, 0, 180);
|
||||
p.rect(barX(bar), p.height - bars[bar], barWidth, bars[bar]);
|
||||
}
|
||||
oldBars = [...bars];
|
||||
}
|
||||
|
||||
const sketch = function (p) {
|
||||
let bars = [];
|
||||
p.setup = function () {
|
||||
p.createCanvas(
|
||||
numBars * (barWidth + barSpacing) + barSpacing,
|
||||
maxBarHeight * 1.25
|
||||
);
|
||||
p.background(10);
|
||||
for (let i = 0; i < numBars; i++) {
|
||||
bars[i] = p.random(10, maxBarHeight);
|
||||
}
|
||||
oldBars = [...bars];
|
||||
p.noLoop();
|
||||
p.userStartAudio();
|
||||
osc = new p5.Oscillator("sine");
|
||||
executeSortingFunction("insertionSort", bars, p);
|
||||
};
|
||||
};
|
||||
|
||||
function mousePressed() {
|
||||
console.log(p.getAudioContext());
|
||||
userStartAudio();
|
||||
console.log(p.getAudioContext());
|
||||
}
|
||||
|
||||
async function executeSortingFunction(functionAsString, bars, p) {
|
||||
switch (functionAsString) {
|
||||
case "bogoSort":
|
||||
await bogoSort(bars, p);
|
||||
break;
|
||||
case "bozoSort":
|
||||
await bozoSort(bars, p);
|
||||
break;
|
||||
case "stalinSort":
|
||||
await stalinSort(bars, p);
|
||||
break;
|
||||
case "bubbleSort":
|
||||
await bubbleSort(bars, p);
|
||||
break;
|
||||
case "insertionSort":
|
||||
await insertionSort(bars, p);
|
||||
break;
|
||||
}
|
||||
refreshBars(p, bars);
|
||||
p.colorMode(p.HSL);
|
||||
osc.start();
|
||||
for (let i = 0; i < bars.length; i++) {
|
||||
p.fill((i / bars.length) * 360, 100, 50);
|
||||
p.rect(
|
||||
i * (barWidth + barSpacing) + barSpacing,
|
||||
p.height - bars[i],
|
||||
barWidth,
|
||||
bars[i]
|
||||
);
|
||||
osc.freq(((bars[i] * maxBarHeight) / 20 ) + 500);
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
osc.stop();
|
||||
}
|
||||
|
||||
async function bogoSort(arr, p) {
|
||||
while (!isSorted(arr)) {
|
||||
for (let i = arr.length - 1; i > 0; i--) {
|
||||
// Fisher-Yates-Shuffle
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[arr[i], arr[j]] = [arr[j], arr[i]];
|
||||
}
|
||||
refreshBars(p, arr);
|
||||
await new Promise((resolve) => setTimeout(resolve, swapRate));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
async function bozoSort(arr, p) {
|
||||
while (!isSorted(arr)) {
|
||||
const bar1 = Math.floor(Math.random() * arr.length);
|
||||
const bar2 = Math.floor(Math.random() * arr.length);
|
||||
[arr[bar1], arr[bar2]] = [arr[bar2], arr[bar1]];
|
||||
refreshBars(p, arr);
|
||||
await new Promise((resolve) => setTimeout(resolve, swapRate));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
async function stalinSort(arr, p) {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] < arr[i - 1]) {
|
||||
arr.splice(i, 1);
|
||||
i -= 1;
|
||||
refreshBars(p, arr);
|
||||
await new Promise((resolve) => setTimeout(resolve, swapRate));
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
async function bubbleSort(arr, p) {
|
||||
while (!isSorted(arr)) {
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (arr[i] > arr[i + 1]) {
|
||||
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
|
||||
refreshBars(p, arr);
|
||||
await new Promise((resolve) => setTimeout(resolve, swapRate));
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
async function insertionSort(arr, p) {
|
||||
for (let i = 0; i <= arr.length - 1; i++) {
|
||||
let currentBarIndex = i;
|
||||
while (
|
||||
currentBarIndex > 0 &&
|
||||
arr[currentBarIndex - 1] > arr[currentBarIndex]
|
||||
) {
|
||||
[arr[currentBarIndex], arr[currentBarIndex - 1]] = [
|
||||
arr[currentBarIndex - 1],
|
||||
arr[currentBarIndex],
|
||||
];
|
||||
currentBarIndex--;
|
||||
refreshBars(p, arr);
|
||||
await new Promise((resolve) => setTimeout(resolve, swapRate));
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
const sketch2 = function (p) {};
|
||||
|
||||
p5Sketch = new p5(sketch);
|
8
7/style.css
Normal file
8
7/style.css
Normal file
|
@ -0,0 +1,8 @@
|
|||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
canvas {
|
||||
display: block;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue