Skip to content

Instantly share code, notes, and snippets.

@graninas
Created November 26, 2022 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graninas/40a1a3167f833426b67e376f69d07239 to your computer and use it in GitHub Desktop.
Save graninas/40a1a3167f833426b67e376f69d07239 to your computer and use it in GitHub Desktop.
Sherlock Holmes and a Code Riddle
// https://www.tutorialspoint.com/online_processingjs_editor.php
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.min.js"></script>
<script type="application/processing" data-processing-target="pjs">
dW = 4; dH = 20; dWH = dW/2; dHH = dH/2;
gSh = dWH/2; r = dW/sqrt(2); fs = 2 * (dH);
function stick() { beginShape();
vertex(-gSh + 0, gSh + 0); vertex(-gSh + r, gSh+r);
vertex(-gSh + r, gSh+r+dH); vertex(-gSh + 0, gSh+r+r+dH);
vertex(-gSh - r, gSh+r+dH); vertex(-gSh - r, gSh+r);
endShape(CLOSE); }
function drawCode(code) { stroke(0, 0, 0); fill(0, 0, 0);
if (code[0] === "add") { stroke(0, 80, 11); fill(0, 80, 11);}
for (let i = 0; i < code[1].length; i++) {
let pos = Number(code[1][i][0] + code[1][i][1]);
let isOdd = (pos % 2) != 0;
let shift = isOdd ? dHH + (fs/2)*(pos - 1) : (pos/2) * fs;
if (code[1][i][2] === "a") {pushMatrix(); translate(shift, 0); stick();
popMatrix();
if (isOdd) {pushMatrix();translate(shift, -2); rotate(-PI/2); stick();
translate(-dH-3*r - 1, 0); stick();popMatrix();}}}}
size(600, 200);background(0);translate(60, 90);
code1 = ["add", ["01a", "02a", "05b", "06a", "13a",
"09a", "10a", "12a", "04a"]];
code2 = ["del", ["05b", "12b"]];
code3 = ["add", ["05a", "06a", "03a", "04a", "07a", "08a",
"11a", "12a", "14a"]];
code4 = ["del", ["08b","04b", "05b", "06b", "07a", "01a",
"12b", "13a"]];
drawCode(code1);
drawCode(code2);
drawCode(code3);
drawCode(code4);
</script>
</head>
<body>
<canvas id="pjs"></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment