import React, { useState } from "react"; const CompositeAreaWidget: React.FC = () => { const [mode, setMode] = useState<"add" | "subtract">("add"); const [width, setWidth] = useState(10); const [height, setHeight] = useState(6); // Scale for display const scale = 20; const displayW = width * scale; const displayH = height * scale; const radius = width / 2; // Semicircle on top (width is diameter) const displayR = radius * scale; // Areas const rectArea = width * height; const semiArea = 0.5 * Math.PI * radius * radius; const totalArea = mode === "add" ? rectArea + semiArea : rectArea - semiArea; return (
{/* Rectangle */} {mode === "add" && ( // Semicircle on TOP )} {mode === "add" && ( // Hide the seam line )} {mode === "subtract" && ( // Semicircle Cutting INTO top )} {/* Labels */} Rect {mode === "add" && ( Semicircle )} {mode === "subtract" && ( Hole )}
setWidth(parseInt(e.target.value))} className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-slate-600 mt-2" />
{width}
setHeight(parseInt(e.target.value))} className="w-full h-2 bg-slate-200 rounded-lg appearance-none cursor-pointer accent-slate-600 mt-2" />
{height}
Calculation {mode === "add" ? "Sum" : "Difference"}
Rectangle Area (w×h) {width} × {height} = {rectArea}
Semicircle Area (½πr²) ½ × π × {radius}² ≈ {semiArea.toFixed(1)}
Total Area {totalArea.toFixed(1)}
); }; export default CompositeAreaWidget;