"use client"; import { useState } from "react"; import { COLORS, VizFrame } from "./common"; function makeGrid() { return [ [3, 1, 2, 4, 0, 6], [7, 9, 5, 1, 2, 3], [4, 0, 8, 2, 5, 1], [6, 7, 3, 9, 4, 8], [1, 2, 0, 6, 7, 5], [3, 8, 4, 1, 2, 9], ]; } export function PoolingDemo({ width = 720, height = 360, }: { width?: number; height?: number; }) { const grid = makeGrid(); const [mode, setMode] = useState<"max" | "avg">("max"); const out: number[][] = []; for (let i = 0; i < 3; i++) { const row: number[] = []; for (let j = 0; j < 3; j++) { const block = [ grid[i * 2][j * 2], grid[i * 2][j * 2 + 1], grid[i * 2 + 1][j * 2], grid[i * 2 + 1][j * 2 + 1], ]; row.push( mode === "max" ? Math.max(...block) : Math.round((block.reduce((a, b) => a + b, 0) / 4) * 10) / 10, ); } out.push(row); } const cell = 36; return (
input
{grid.flatMap((row, i) => row.map((v, j) => { const blockI = Math.floor(i / 2); const blockJ = Math.floor(j / 2); const inBlock = mode === "max" ? grid[i][j] === out[blockI][blockJ] : false; return (
{v}
); }), )}
output
{out.flatMap((row, i) => row.map((v, j) => (
{v}
)), )}
{(["max", "avg"] as const).map((m) => ( ))}
); }