Created
June 14, 2026 15:12
-
-
Save gkucmierz/e12abacf727d34385b49c8c7a0e33001 to your computer and use it in GitHub Desktop.
Run this code instantly in your browser: https://instacode.app/gist/e12abacf727d34385b49c8c7a0e33001
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
| // Visualization of Euler's Identity e^(i * pi) = -1 | |
| // Animate the point e^(i * theta) moving along the unit circle in the complex plane. | |
| const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
| async function main() { | |
| console.log("=== EULER'S IDENTITY VISUALIZATION ==="); | |
| console.log("Euler's Formula: e^(i * theta) = cos(theta) + i * sin(theta)"); | |
| console.log("For theta = pi: e^(i * pi) = -1 + 0i = -1\n"); | |
| await sleep(1500); | |
| const width = 45; | |
| const height = 14; | |
| const centerX = 22; | |
| const centerY = 11; // Positioned lower since we care about positive imaginary parts (y >= 0) | |
| // Helper function to create an empty grid with coordinate axes | |
| const createGrid = () => { | |
| const grid = []; | |
| for (let y = 0; y < height; y++) { | |
| grid[y] = []; | |
| for (let x = 0; x < width; x++) { | |
| if (y === centerY && x === centerX) grid[y][x] = '+'; // Origin (0, 0) | |
| else if (y === centerY) grid[y][x] = '─'; // Real axis (Re) | |
| else if (x === centerX) grid[y][x] = '│'; // Imaginary axis (Im) | |
| else grid[y][x] = ' '; | |
| } | |
| } | |
| return grid; | |
| }; | |
| const steps = 15; | |
| const rx = 18; // X-axis radius (scaled for character aspect ratio) | |
| const ry = 9; // Y-axis radius | |
| for (let i = 0; i <= steps; i++) { | |
| const theta = (i / steps) * Math.PI; | |
| const x = Math.cos(theta); // Re (Real part) | |
| const y = Math.sin(theta); // Im (Imaginary part) | |
| // Convert to grid coordinates | |
| const screenX = Math.round(centerX + x * rx); | |
| const screenY = Math.round(centerY - y * ry); | |
| const grid = createGrid(); | |
| // Key point labels | |
| grid[centerY][centerX + rx] = '1'; | |
| grid[centerY][centerX - rx] = 'A'; // Will be replaced with '-1' label | |
| grid[centerY - ry][centerX] = 'i'; | |
| // Draw the trace of the path (semicircle) | |
| for (let j = 0; j <= i; j++) { | |
| const t = (j / steps) * Math.PI; | |
| const sx = Math.round(centerX + Math.cos(t) * rx); | |
| const sy = Math.round(centerY - Math.sin(t) * ry); | |
| if (sx >= 0 && sx < width && sy >= 0 && sy < height) { | |
| grid[sy][sx] = '•'; | |
| } | |
| } | |
| // Mark the current position | |
| if (screenX >= 0 && screenX < width && screenY >= 0 && screenY < height) { | |
| grid[screenY][screenX] = '●'; | |
| } | |
| // Clear console and print the new frame | |
| console.clear(); | |
| console.log(`=== STEP ${i}/${steps} ===`); | |
| console.log(`Angle: theta = ${(theta / Math.PI).toFixed(2)} * π rad`); | |
| console.log(`Value: e^(i * theta) = ${x.toFixed(4)} + (${y.toFixed(4)})i`); | |
| console.log("\nComplex Plane (Im/Re):"); | |
| // Print the grid replacing 'A' with '-1' | |
| for (let y = 0; y < height; y++) { | |
| let line = grid[y].join(''); | |
| if (y === centerY) { | |
| line = line.replace('A', '-1'); | |
| } | |
| console.log(line); | |
| } | |
| await sleep(350); | |
| } | |
| console.log("\n[End] Point reached -1 on the real axis."); | |
| console.log("Identity verified: e^(i * π) + 1 = 0"); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment