Created
February 7, 2022 23:16
-
-
Save jcreedcmu/e4e4a5c25d63c41ac7f8faabf8c8d0a8 to your computer and use it in GitHub Desktop.
Generate a PBM file full of dithering patterns
This file contains hidden or 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
const SCALE = 3; | |
// return the nth dithering point in an 2ᵏ × 2ᵏ square | |
// n ∈ [0, 4ᵏ - 1] | |
type Point = { x: number, y: number }; | |
function ksquare(k: number): Point[] { | |
if (k == 0) | |
return [{ x: 0, y: 0 }]; | |
else { | |
const prev = ksquare(k - 1); | |
function shift(dx: number, dy: number): Point[] { | |
return prev.map(({ x, y }) => ({ x: 2 * x + dx, y: 2 * y + dy })); | |
} | |
return ([] as Point[]).concat(shift(0, 0), shift(1, 1), shift(1, 0), shift(0, 1)); | |
} | |
} | |
function tableau(k: number): number[][] { | |
const edge = 1 << k; | |
const bigEdge = SCALE * edge * edge; | |
const table: number[][] = [...Array(bigEdge).keys()].map(k => []); | |
const sqr = ksquare(k); | |
for (let ii = 0; ii < edge; ii++) { | |
for (let jj = 0; jj < edge; jj++) { | |
for (let zi = 0; zi < SCALE; zi++) { | |
for (let zj = 0; zj < SCALE; zj++) { | |
for (let t = 0; t < ii * edge + jj; t++) { | |
const x = ii * edge * SCALE + zi * edge + sqr[t].x; | |
const y = jj * edge * SCALE + zj * edge + sqr[t].y; | |
table[y][x] = 1; | |
} | |
} | |
} | |
} | |
} | |
return table; | |
} | |
function bitmap(k: number): string { | |
const tab = tableau(k); | |
let str = `P1 ${tab.length} ${tab.length}\n`; | |
for (let i = 0; i < tab.length; i++) { | |
for (let j = 0; j < tab.length; j++) { | |
str += (tab[i][j] ? 1 : 0) + ' '; | |
} | |
str += '\n'; | |
} | |
return str; | |
} | |
import * as fs from 'fs'; | |
fs.writeFileSync('/tmp/a.pbm', bitmap(4), 'utf8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment