Em3w parensordot
const vals = {
E: 0, M: 90, '3': 180, W: 270
}
// const byDeg = {
// 0: 'E', 90: 'M', 180: '3', 270: 'W'
// }
const byDeg = Object.keys(vals).reduce((acc, key) => {
acc[vals[key]] = key;
return acc;
}, {});
const ops = {
')': 90, '(' : -90, '|': 180
}
function rotToSym(v) {
if (v < 0) v += 360;
const deg = Math.abs(v % 360);
return byDeg[deg]
}
function op(char, op) {
if (char === 'M' && op === '|') return 'M'
if (char === 'W' && op === '|') return 'W'
let v = vals[char]
v += ops[op]
return rotToSym(v)
}
function emw(prog) {
prog = [...prog]
let chars = []
let res = []
for (let i = 0; i < prog.length; i++) {
let char = prog[i]
if (vals[char] != null) {
chars.push(char)
} else if (ops[char]) {
chars = chars.map(v => op(v, char))
} else if (char === '.' && chars.length > 0) {
const num = chars.map(v => vals[v])
.reduce((a, b) => a + b, 0);
chars = [rotToSym(num)]
}
}
return chars.join``
}
function expr(prog) {
const orig = prog;
prog = prog.split(/\s/)
return orig + ' = ' + prog.map(emw).join` `
}
document.body.appendChild(
Object.assign(
document.createElement`pre`, {
innerHTML:
`
Em3w parensordot
// rotate 90 clockwise
${expr('EEE)')}
// rotate -90 clockwise
${expr('W(')}
${expr('W((')}
// mirror
${expr('E|')}
// mirror
${expr('EM|')}
${expr('WW3) MM) EEEM).')}
// sum rotations
${expr('MMM.')}
${expr(
`E) E( E)) EEE))) E)))) MM33).`)}
`}
)
)
Fun thing I wanted to make for awhile… definitely going to do some doodles of this in my sketchbook…
Code description from GPT4 because I’m too lazy to write one…
This code is implementing a form of symbolic computation using a transformational language. This language has four primary symbols: “E”, “M”, “3”, and “W”. Each of these symbols is associated with a specific angle: 0, 90, 180, and 270 degrees, respectively.
There are also special symbols, which act as operators that perform certain transformations on the primary symbols:
- “)”: Represents a rotation 90 degrees clockwise.
- “(“: Represents a rotation 90 degrees counter-clockwise.
- “|”: Represents a mirror operation, which reverses the direction. However, “M” and “W” are unaffected by this operation.
- “.”: This operator sums up all previous primary symbols (by their respective degree values), resulting in a new symbol. The degree value of the new symbol is the sum of the previous degrees modulo 360.
The function
emw(prog)
interprets a stringprog
as a sequence of symbols and operations. It reads each character in the string and based on whether it’s a primary symbol, operator, or a “.”, it either adds the symbol to an array or performs an operation on the existing symbols in the array.The
expr(prog)
function splits the input stringprog
into space-separated substrings. It interprets each substring as a sequence in the transformational language, using theemw
function. It then joins the results together with a space and prepends the original input and an equals sign to give a string representation of the transformations.The final section of the code generates a series of examples of expressions in this language, evaluates them using the
expr
function, and adds the results to the HTML body of a webpage in a formatted manner. This gives the user a way to see the transformations in action.