Created
May 10, 2022 16:05
-
-
Save micuat/60ccaaa162fab58a5923346744e81472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://refactorman.com/2015/04/28/exploring-the-dct-part-i/ | |
var dct = function (input) { | |
var output = [], v, u, x, y, sum, val, au, av; | |
for (v = 0; v < 8; v++) { | |
output.push([]) | |
for (u = 0; u < 8; u++) { | |
sum = 0; | |
for (y = 0; y < 8; y++) { | |
for (x = 0; x < 8; x++) { | |
val = input[y][x]; | |
val *= Math.cos(((2 * x + 1) * u * Math.PI) / 16); | |
val *= Math.cos(((2 * y + 1) * v * Math.PI) / 16); | |
sum += val; | |
} | |
} | |
au = u === 0 ? 1 / Math.SQRT2 : 1; | |
av = v === 0 ? 1 / Math.SQRT2 : 1; | |
output[v][u] = 1 / 4 * au * av * sum; | |
} | |
} | |
return output; | |
} | |
// let v = randomVector(8*8) | |
let v = [] | |
for(let i = 0; i < 8; i++) { | |
let u=[] | |
for(let j = 0; j < 8; j++) { | |
// if (i < 4) | |
// if (i < 4&&j<4) | |
// u.push(1) | |
// else | |
// u.push(0) | |
u.push(Math.max(0, 1-Math.sqrt((i-3.5)*(i-3.5) + (j-3.5)*(j-3.5)))) | |
} | |
v.push(u) | |
} | |
console.log(v) | |
let o = dct(v) | |
let code = "solid()" | |
for(let i = 0; i < o.length; i++) { | |
for(let j = 0; j < o[i].length; j++) { | |
if(Math.abs(o[i][j]) < 1e-9) { | |
o[i][j] = 0 | |
} | |
else { | |
let au,av,val | |
au = i === 0 ? 1/Math.SQRT2 : 1; | |
av = j === 0 ? 1/Math.SQRT2 : 1; | |
val = o[i][j]; | |
val *= au; | |
val *= av; | |
// let c = `osc(Math.PI*2*${i},0,-Math.PI/2*${i/32}).brightness(-.5).color(2,2,2).r().rotate(Math.PI/2).mult(osc(Math.PI*2*${j},0,-Math.PI/2*${j/32}).brightness(-.5).color(2,2,2).r()).color(${val},${val},${val})`; | |
let y = `osc(Math.PI*${i*2},0,-Math.PI/2-Math.PI*${i/16}).brightness(-.5).color(2).rotate(Math.PI/2)` | |
let x = `osc(Math.PI*${j*2},0,-Math.PI/2-Math.PI*${j/16}).brightness(-.5).color(2)` | |
let c = `${y}.mult(${x}).color(${val})`; | |
if (i == 0) c = `${x}.color(${val})` | |
else if (j == 0) c = `${y}.color(${val})` | |
// console.log(i, j, val, c) | |
code = `${code}.add(${c})` | |
// val *= Math.cos(((2*x+1) * u * Math.PI)/16); | |
// val *= Math.cos(((2*y+1) * v * Math.PI)/16); | |
} | |
} | |
} | |
console.log(code + ".color(1/8).r().modulate(solid(1,1),-.25).out()") | |
//console.log(o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment