import React, { useState } from "react"; const PolygonWidget: React.FC = () => { const [n, setN] = useState(5); // Math const interiorSum = (n - 2) * 180; const eachInterior = Math.round((interiorSum / n) * 100) / 100; const eachExterior = Math.round((360 / n) * 100) / 100; // SVG Config const width = 300; const height = 300; const cx = width / 2; const cy = height / 2; const r = 80; // @ts-ignore const points = []; for (let i = 0; i < n; i++) { const angle = (i * 2 * Math.PI) / n - Math.PI / 2; // Start at top points.push({ x: cx + r * Math.cos(angle), y: cy + r * Math.sin(angle), }); } // Generate path string const pathD = points .map((p, i) => (i === 0 ? `M ${p.x} ${p.y}` : `L ${p.x} ${p.y}`)) .join(" ") + " Z"; // Generate exterior lines (extensions) const exteriorLines = points.map((p, i) => { // @ts-ignore const nextP = points[(i + 1) % n]; // Vector from p to nextP const dx = nextP.x - p.x; const dy = nextP.y - p.y; // Normalize and extend const len = Math.sqrt(dx * dx + dy * dy); const exLen = 40; const exX = nextP.x + (dx / len) * exLen; const exY = nextP.y + (dy / len) * exLen; return { x1: nextP.x, y1: nextP.y, x2: exX, y2: exY }; }); return (
setN(parseInt(e.target.value))} className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-emerald-600 mb-6" />
Interior Sum
(n - 2) × 180° ={" "} {interiorSum}°
Each Interior Angle
{interiorSum} / {n} ={" "} {eachInterior}°
Each Exterior Angle
360 / {n} ={" "} {eachExterior}°
{/* Extensions for exterior angles */} {exteriorLines.map((line, i) => ( ))} {/* Polygon */} {/* Vertices */} {points.map((p, i) => ( ))} {/* Center text */} {n}-gon
); }; export default PolygonWidget;