Created
August 18, 2021 11:25
-
-
Save zakuroishikuro/dc92d9d6a873e24f40ace683df177245 to your computer and use it in GitHub Desktop.
atom.ts
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
// deno run --unstable https://raw.githubusercontent.com/zakuroishikuro/sharin/main/asobu/atom.ts | |
// | |
// original source code: | |
// https://github.com/mame/trance-book/blob/master/8-1/subcharacter-rendering-demo.rb | |
// | |
// from: | |
// あなたの知らない超絶技巧プログラミングの世界 by 遠藤侑介 | |
// 8-1-2 アスキーアートの生成(1):サブキャラクターレンダリング | |
import { complex, multiply } from "https://cdn.skypack.dev/mathjs"; | |
/** n秒待つ関数 (await忘れずに) */ | |
function sleep(sec: number) { | |
return new Promise((res) => { | |
setTimeout(() => res(null), sec * 1000); | |
}); | |
} | |
const encoder = new TextEncoder(); | |
/** console.logだと毎回改行されて困るので、改行せず文字列を出力する関数 */ | |
async function print(str: string) { | |
const bytes = encoder.encode(str); | |
await Deno.stdout.write(bytes); | |
} | |
// コンソールの大きさを取得。--unstalbeフラグが必要。 | |
const {rows} = Deno.consoleSize(Deno.stdout.rid) | |
const S = rows; // 描画領域のの | |
const A = S / 2.1; // 長径 | |
const B = S / 8.0; // 短径 | |
print("\x1b[2J"); // 画面クリア | |
let n = 0; | |
while (true) { | |
// バッファ | |
const s = [...Array(S)].map(() => [..." ".repeat(S * 2)]); | |
[...Array(100)].forEach((_, i) => { | |
// 楕円の媒介変数表示 | |
const t = Math.PI * 2 * (i + n) / 100; | |
const e = complex(A * Math.cos(t), B * Math.sin(t)); | |
// 楕円を 3 つ描く | |
[-1, 0, 1].forEach((j) => { | |
// 楕円を傾ける | |
const e2 = multiply( | |
e, | |
complex({ r: 1.0, phi: Math.PI * 2 / 3 * j + n / 500 }), | |
); | |
// ドットを置く | |
const x = Math.floor(e2.re * 2 + S); | |
const y = Math.floor(e2.im * 2 + S); | |
const row = s[Math.floor(y / 2)]; | |
if (i === 99) { | |
[row[x - 1], row[x]] = "()"; | |
} else if (y % 2 == 0) { | |
row[x] = row[x] == " " || row[x] == "'" ? "'" : ";"; | |
} else { | |
row[x] = row[x] == " " || row[x] == "," ? "," : ";"; | |
} | |
}); | |
}); | |
print("\x1b[1;1H" + s.map((row) => row.join("")).join("\n")); | |
await sleep(0.03); | |
n++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment