Prompt specification
Build or customize the Three.js 3D scene "Right-angle condition explorer" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Right-angle condition explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/right-angle-condition-explorer-x1728
Scene Description Prompt:
Change a low-poly triangle’s angles and compare side-square areas; show Pythagorean equality only at a right angle. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (right-angle-condition-explorer-x1728):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pythagoras Lab — Side-Square Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: #101827;
--panel-2: #151f31;
--line: rgba(255, 255, 255, 0.09);
--text: #f4f8ff;
--muted: #91a0b8;
--a: #ffbd5c;
--b: #a98bff;
--c: #55d9ff;
--triangle: #50dfbd;
--right: #67f5ae;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
height: 100dvh;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
touch-action: none;
background:
radial-gradient(circle at 48% 42%, rgba(50, 112, 143, 0.17), transparent 38%),
radial-gradient(circle at 70% 80%, rgba(123, 87, 210, 0.1), transparent 32%),
linear-gradient(145deg, #0a1220 0%, #070b14 100%);
}
#stage::before {
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
content: "";
background-image:
linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px);
background-size: 38px 38px;
mask-image: radial-gradient(circle at center, black, transparent 78%);
}
#stage canvas {
position: absolute;
inset: 0;
z-index: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
z-index: 5;
max-width: 440px;
pointer-events: none;
}
.kicker {
display: block;
margin-bottom: 8px;
color: var(--triangle);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.7rem);
line-height: 1.03;
letter-spacing: -0.045em;
}
.brand p {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.55;
}
.stage-footer {
position: absolute;
right: 22px;
bottom: 20px;
left: 22px;
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
pointer-events: none;
color: #8291aa;
font-size: 0.72rem;
letter-spacing: 0.04em;
}
.view-badge {
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 13, 24, 0.72);
color: #aab8cc;
backdrop-filter: blur(12px);
white-space: nowrap;
}
#labels {
position: absolute;
inset: 0;
z-index: 4;
pointer-events: none;
}
.angle-label,
.square-label {
position: absolute;
left: 0;
top: 0;
white-space: nowrap;
will-change: left, top;
}
.angle-label {
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 8px;
background: rgba(8, 13, 24, 0.84);
color: #edf5ff;
font-size: 0.72rem;
font-weight: 750;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(8px);
}
.angle-label.is-right {
border-color: rgba(103, 245, 174, 0.8);
color: var(--right);
box-shadow: 0 0 20px rgba(103, 245, 174, 0.14);
}
.square-label {
padding: 6px 9px;
border: 1px solid color-mix(in srgb, var(--side-color) 65%, transparent);
border-radius: 9px;
background: rgba(8, 13, 24, 0.87);
color: var(--side-color);
font-size: 0.72rem;
font-weight: 800;
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(8px);
}
aside {
position: relative;
z-index: 10;
min-height: 0;
overflow-y: auto;
padding: 24px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(255, 255, 255, 0.018), transparent 30%),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #344159 transparent;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.panel-heading h2 {
margin: 0;
font-size: 1.25rem;
letter-spacing: -0.025em;
}
.small-button {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: #192338;
color: #dce7f7;
cursor: pointer;
transition: 140ms ease;
}
.small-button:hover {
border-color: rgba(255, 255, 255, 0.22);
background: #21304a;
}
.muted {
margin: 10px 0 0;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.55;
}
.section {
margin-top: 22px;
padding-top: 20px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 15px;
}
.section-title h3 {
margin: 0;
font-size: 0.82rem;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title span {
color: #72829c;
font-size: 0.68rem;
}
.quick-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 18px;
}
.quick-buttons button {
padding: 8px 5px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel-2);
color: #b9c7db;
font-size: 0.72rem;
cursor: pointer;
transition: 140ms ease;
}
.quick-buttons button:hover {
border-color: rgba(103, 245, 174, 0.45);
color: var(--right);
}
.angle-control {
margin-bottom: 16px;
}
.angle-control header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
font-size: 0.8rem;
}
.angle-control output {
min-width: 52px;
padding: 4px 7px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.055);
color: #eff5ff;
font-variant-numeric: tabular-nums;
text-align: right;
}
.angle-control.is-right output {
background: rgba(103, 245, 174, 0.12);
color: var(--right);
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background: #27344a;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #eaf6ff;
border-radius: 50%;
appearance: none;
background: #57d7c0;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #eaf6ff;
border-radius: 50%;
background: #57d7c0;
box-shadow: 0 3px 12px rgba(0, 0, 0, 0.35);
}
.area-row {
display: grid;
grid-template-columns: 34px minmax(0, 1fr) 66px;
align-items: center;
gap: 9px;
margin: 13px 0;
font-size: 0.76rem;
}
.area-symbol {
color: var(--side-color);
font-weight: 850;
}
.area-bar {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.065);
}
.area-bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--side-color);
transition: width 180ms ease;
}
.area-value {
color: #dfe8f6;
font-variant-numeric: tabular-nums;
text-align: right;
}
.area-row.is-hypotenuse .area-symbol,
.area-row.is-hypotenuse .area-value {
color: var(--right);
}
.area-row.is-hypotenuse .area-bar i {
box-shadow: 0 0 12px color-mix(in srgb, var(--side-color) 55%, transparent);
}
.equation-shell {
padding: 15px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(6, 11, 20, 0.48);
transition: 180ms ease;
}
.equation-shell.is-right {
border-color: rgba(103, 245, 174, 0.52);
background: rgba(41, 107, 78, 0.13);
box-shadow: inset 0 0 24px rgba(103, 245, 174, 0.04);
}
.status {
display: flex;
align-items: center;
gap: 8px;
color: #a8b5c9;
font-size: 0.66rem;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #66748d;
content: "";
}
.is-right .status {
color: var(--right);
}
.is-right .status::before {
background: var(--right);
box-shadow: 0 0 10px rgba(103, 245, 174, 0.65);
}
.status-sub {
min-height: 18px;
margin: 7px 0 13px;
color: #7f8fa8;
font-size: 0.72rem;
}
.equation {
display: grid;
grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
align-items: center;
gap: 10px;
text-align: center;
}
.equation-term span {
display: block;
color: #e7effb;
font-size: 0.87rem;
font-weight: 800;
}
.equation-term small {
display: block;
margin-top: 5px;
color: #8493aa;
font-size: 0.7rem;
font-variant-numeric: tabular-nums;
}
#relation {
color: #ff8b9c;
font-size: 1.65rem;
line-height: 1;
}
.is-right #relation {
color: var(--right);
}
.difference {
margin: 13px 0 0;
color: #7f8fa8;
font-size: 0.7rem;
line-height: 1.45;
text-align: center;
}
.note {
display: flex;
gap: 10px;
margin-top: 18px;
padding: 12px;
border: 1px solid rgba(85, 217, 255, 0.16);
border-radius: 11px;
background: rgba(85, 217, 255, 0.055);
color: #a7b5ca;
font-size: 0.72rem;
line-height: 1.5;
}
.note strong {
color: var(--c);
}
@media (max-width: 850px) {
body {
grid-template-columns: 1fr;
grid-template-rows: minmax(320px, 57dvh) minmax(0, 43dvh);
}
aside {
padding: 20px;
border-top: 1px solid var(--line);
border-left: 0;
}
.brand {
top: 17px;
left: 18px;
}
.brand h1 {
font-size: 1.55rem;
}
.brand p {
display: none;
}
.stage-footer {
right: 16px;
bottom: 13px;
left: 16px;
}
.stage-footer > span:first-child {
display: none;
}
}
</style>
</head>
<body>
<main id="stage">
<header class="brand">
<span class="kicker">Interactive geometry</span>
<h1>Pythagoras Lab</h1>
<p>
Reshape a low-poly triangle and compare the actual areas of squares
constructed on its sides.
</p>
</header>
<div id="labels" aria-hidden="true">
<div class="angle-label" id="angleLabelA"></div>
<div class="angle-label" id="angleLabelB"></div>
<div class="angle-label" id="angleLabelC"></div>
<div class="square-label" id="squareLabelA"></div>
<div class="square-label" id="squareLabelB"></div>
<div class="square-label" id="squareLabelC"></div>
</div>
<footer class="stage-footer">
<span>Drag to orbit · Scroll to zoom</span>
<span class="view-badge">ORTHOGRAPHIC · SQUARES BUILT TO SCALE</span>
</footer>
</main>
<aside>
<div class="panel-heading">
<div>
<span class="kicker">Triangle controls</span>
<h2>Shape the angles</h2>
</div>
<button class="small-button" id="resetShape" type="button">Reset</button>
</div>
<p class="muted">
Change one angle and the other two rebalance proportionally so the total
always remains 180°.
</p>
<div class="section">
<div class="section-title">
<h3>Right-angle presets</h3>
<span>one click</span>
</div>
<div class="quick-buttons">
<button type="button" data-right="A">A = 90°</button>
<button type="button" data-right="B">B = 90°</button>
<button type="button" data-right="C">C = 90°</button>
</div>
<div class="angle-control" id="controlA">
<header>
<span>Angle A</span>
<output id="outputA"></output>
</header>
<input
id="rangeA"
data-angle="A"
type="range"
min="5"
max="170"
step="0.1"
value="60"
aria-label="Angle A"
>
</div>
<div class="angle-control" id="controlB">
<header>
<span>Angle B</span>
<output id="outputB"></output>
</header>
<input
id="rangeB"
data-angle="B"
type="range"
min="5"
max="170"
step="0.1"
value="60"
aria-label="Angle B"
>
</div>
<div class="angle-control" id="controlC">
<header>
<span>Angle C</span>
<output id="outputC"></output>
</header>
<input
id="rangeC"
data-angle="C"
type="range"
min="5"
max="170"
step="0.1"
value="60"
aria-label="Angle C"
>
</div>
</div>
<div class="section">
<div class="section-title">
<h3>Side-square areas</h3>
<span>square units</span>
</div>
<div class="area-row" id="areaA" style="--side-color: var(--a)">
<span class="area-symbol">a²</span>
<div class="area-bar"><i></i></div>
<span class="area-value"></span>
</div>
<div class="area-row" id="areaB" style="--side-color: var(--b)">
<span class="area-symbol">b²</span>
<div class="area-bar"><i></i></div>
<span class="area-value"></span>
</div>
<div class="area-row" id="areaC" style="--side-color: var(--c)">
<span class="area-symbol">c²</span>
<div class="area-bar"><i></i></div>
<span class="area-value"></span>
</div>
</div>
<div class="section">
<div class="section-title">
<h3>Pythagorean check</h3>
<span id="equationMode"></span>
</div>
<div class="equation-shell" id="equationShell">
<div class="status" id="equationStatus"></div>
<p class="status-sub" id="equationSub"></p>
<div class="equation">
<div class="equation-term">
<span id="leftLabel"></span>
<small id="leftValue"></small>
</div>
<strong id="relation">≠</strong>
<div class="equation-term">
<span id="rightLabel"></span>
<small id="rightValue"></small>
</div>
</div>
<p class="difference" id="difference"></p>
</div>
<div class="note">
<span>↗</span>
<span>
<strong>Equality is shown only for an exact 90° angle.</strong>
For every other shape, the square areas remain unequal.
</span>
</div>
</div>
<div class="section">
<div class="section-title">
<h3>Camera</h3>
<span>direct controls</span>
</div>
<button class="small-button" id="frontView" type="button">
Return to front view
</button>
</div>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const labelLayer = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
stage.prepend(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-10, 10, 10, -10, 0.1, 100);
const ambient = new THREE.HemisphereLight(0xd8f5ff, 0x182038, 2.2);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 8, 10);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x7a8dff, 1.5);
rimLight.position.set(-7, 1, 5);
scene.add(rimLight);
const world = new THREE.Group();
scene.add(world);
const colors = {
A: 0xffbd5c,
B: 0xa98bff,
C: 0x55d9ff,
right: 0x67f5ae
};
const angles = {
A: 60,
B: 60,
C: 60
};
const labelElements = {
angleA: document.querySelector("#angleLabelA"),
angleB: document.querySelector("#angleLabelB"),
angleC: document.querySelector("#angleLabelC"),
squareA: document.querySelector("#squareLabelA"),
squareB: document.querySelector("#squareLabelB"),
squareC: document.querySelector("#squareLabelC")
};
const labelPositions = {};
const labelWorld = new THREE.Vector3();
const orbit = {
azimuth: 0.28,
elevation: 0.16,
zoom: 1
};
const clamp = THREE.MathUtils.clamp;
function clearWorld() {
world.traverse((object) => {
if (object.geometry) {
object.geometry.dispose();
}
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
}
});
world.clear();
}
function addLine(points, color, opacity = 1, loop = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const line = loop
? new THREE.LineLoop(geometry, material)
: new THREE.Line(geometry, material);
world.add(line);
return line;
}
function createTrianglePrism(points) {
const depth = 0.28;
const front = points.map((point) =>
new THREE.Vector3(point.x, point.y, depth / 2)
);
const back = points.map((point) =>
new THREE.Vector3(point.x, point.y, -depth / 2)
);
const positions = [];
const vertexColors = [];
const frontColor = new THREE.Color(0x54dfbd);
const backColor = new THREE.Color(0x176d73);
const sideColors = [
new THREE.Color(0x238b88),
new THREE.Color(0x2ba69b),
new THREE.Color(0x1e747c)
];
function triangle(a, b, c, color) {
positions.push(
a.x, a.y, a.z,
b.x, b.y, b.z,
c.x, c.y, c.z
);
for (let i = 0; i < 3; i++) {
vertexColors.push(color.r, color.g, color.b);
}
}
triangle(front[0], front[1], front[2], frontColor);
triangle(back[2], back[1], back[0], backColor);
for (let i = 0; i < 3; i++) {
const j = (i + 1) % 3;
const color = sideColors[i];
triangle(front[i], back[j], front[j], color);
triangle(front[i], back[i], back[j], color);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(vertexColors, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.68,
metalness: 0.04,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
world.add(mesh);
const edgePoints = [];
for (let i = 0; i < 3; i++) {
const j = (i + 1) % 3;
edgePoints.push(front[i], front[j]);
edgePoints.push(back[i], back[j]);
edgePoints.push(front[i], back[i]);
}
addLine(edgePoints, 0xb9fff0, 0.62);
}
function createSquare(sideKey, p, q, third, color) {
const length = p.distanceTo(q);
const direction = q.clone().sub(p).normalize();
const leftNormal = new THREE.Vector3(
-direction.y,
direction.x,
0
);
const towardThird = third.clone().sub(p);
const outward = leftNormal.dot(towardThird) > 0
? leftNormal.clone().negate()
: leftNormal.clone();
const outerP = p.clone().addScaledVector(outward, length);
const outerQ = q.clone().addScaledVector(outward, length);
const z = -0.035;
const positions = [
p.x, p.y, z,
q.x, q.y, z,
outerQ.x, outerQ.y, z,
p.x, p.y, z,
outerQ.x, outerQ.y, z,
outerP.x, outerP.y, z
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false
});
const square = new THREE.Mesh(geometry, material);
square.renderOrder = 0;
world.add(square);
const border = [
p.clone().setZ(0.015),
q.clone().setZ(0.015),
outerQ.clone().setZ(0.015),
outerP.clone().setZ(0.015)
];
addLine(border, color, 0.9, true);
const center = p.clone().add(q).multiplyScalar(0.5);
center.addScaledVector(outward, length / 2);
center.z = 0.06;
labelPositions[`square${sideKey}`] = center;
}
function createAngleArc(angleKey, points) {
const otherKeys = ["A", "B", "C"].filter(
(key) => key !== angleKey
);
const vertex = points[angleKey];
const u = points[otherKeys[0]].clone().sub(vertex).normalize();
const v = points[otherKeys[1]].clone().sub(vertex).normalize();
const dot = clamp(u.dot(v), -1, 1);
const cross = u.x * v.y - u.y * v.x;
const theta = Math.acos(dot);
const direction = cross >= 0 ? 1 : -1;
const radius = 0.62;
const arcPoints = [];
for (let i = 0; i <= 28; i++) {
const t = (i / 28) * theta;
const cos = Math.cos(t);
const sin = Math.sin(t);
const rotated = new THREE.Vector3(
u.x * cos - u.y * sin * direction,
u.x * sin * direction + u.y * cos,
0
);
arcPoints.push(
vertex.clone().addScaledVector(rotated, radius).setZ(0.19)
);
}
const exactRight = Math.abs(angles[angleKey] - 90) < 1e-6;
addLine(
arcPoints,
exactRight ? colors.right : 0xe9f5ff,
exactRight ? 1 : 0.72
);
const bisector = u.clone().add(v).normalize();
const labelPosition = vertex.clone().addScaledVector(bisector, 0.94);
labelPosition.z = 0.23;
labelPositions[`angle${angleKey}`] = labelPosition;
const label = labelElements[`angle${angleKey}`];
label.textContent = `${angleKey} ${angles[angleKey].toFixed(1)}°`;
label.classList.toggle("is-right", exactRight);
}
function updateControls() {
["A", "B", "C"].forEach((key) => {
const range = document.querySelector(`[data-angle="${key}"]`);
const output = document.querySelector(`#output${key}`);
const control = document.querySelector(`#control${key}`);
range.value = angles[key].toFixed(2);
output.textContent = `${angles[key].toFixed(1)}°`;
control.classList.toggle(
"is-right",
Math.abs(angles[key] - 90) < 1e-6
);
});
}
function updateAreaComparison(areas) {
const maxArea = Math.max(areas.a, areas.b, areas.c);
const rightAngle = ["A", "B", "C"].find(
(key) => Math.abs(angles[key] - 90) < 1e-6
);
const angleToSide = { A: "a", B: "b", C: "c" };
let highlightedSide;
if (rightAngle) {
highlightedSide = angleToSide[rightAngle];
} else {
highlightedSide = ["a", "b", "c"].reduce((largest, side) =>
areas[side] > areas[largest] ? side : largest
);
}
["A", "B", "C"].forEach((key) => {
const side = key.toLowerCase();
const row = document.querySelector(`#area${key}`);
const bar = row.querySelector(".area-bar i");
const value = row.querySelector(".area-value");
bar.style.width = `${(areas[side] / maxArea) * 100}%`;
value.textContent = areas[side].toFixed(2);
row.classList.toggle(
"is-hypotenuse",
side === highlightedSide
);
});
}
function updateEquation(areas) {
const exactRight = ["A", "B", "C"].find(
(key) => Math.abs(angles[key] - 90) < 1e-6
);
const angleToSide = { A: "a", B: "b", C: "c" };
let rightSide;
let leftSides;
if (exactRight) {
rightSide = angleToSide[exactRight];
leftSides = ["a", "b", "c"].filter((side) => side !== rightSide);
} else {
rightSide = ["a", "b", "c"].reduce((largest, side) =>
areas[side] > areas[largest] ? side : largest
);
leftSides = ["a", "b", "c"].filter((side) => side !== rightSide);
}
const leftValue = leftSides.reduce(
(sum, side) => sum + areas[side],
0
);
const rightValue = areas[rightSide];
document.querySelector("#leftLabel").textContent =
leftSides.map((side) => `${side}²`).join(" + ");
document.querySelector("#rightLabel").textContent =
`${rightSide}²`;
document.querySelector("#leftValue").textContent =
leftValue.toFixed(2);
document.querySelector("#rightValue").textContent =
rightValue.toFixed(2);
const shell = document.querySelector("#equationShell");
const relation = document.querySelector("#relation");
const status = document.querySelector("#equationStatus");
const sub = document.querySelector("#equationSub");
const difference = document.querySelector("#difference");
const mode = document.querySelector("#equationMode");
// Equality is deliberately allowed only for an exact 90-degree angle.
if (exactRight) {
relation.textContent = "=";
shell.classList.add("is-right");
status.textContent = "Right angle · equality holds";
sub.textContent = `Angle ${exactRight} is exactly 90°.`;
mode.textContent = "exact match";
difference.textContent =
"The hypotenuse square has exactly the same area as the sum of the two leg squares.";
} else {
relation.textContent = "≠";
shell.classList.remove("is-right");
status.textContent = "No right angle";
mode.textContent = "unequal areas";
const closest = ["A", "B", "C"].reduce((best, key) =>
Math.abs(angles[key] - 90) <
Math.abs(angles[best] - 90)
? key
: best
);
sub.textContent =
`Closest angle to 90°: ${closest} (${angles[closest].toFixed(1)}°).`;
const delta = rightValue - leftValue;
difference.textContent =
`Δ = ${rightSide}² − (${leftSides.map((side) => `${side}²`).join(" + ")}) = ${delta.toFixed(3)}`;
}
}
function rebuild() {
clearWorld();
const angleA = THREE.MathUtils.degToRad(angles.A);
const angleB = THREE.MathUtils.degToRad(angles.B);
const angleC = THREE.MathUtils.degToRad(angles.C);
// Any common circumradius works. Side-square areas are based on
// the resulting actual side lengths.
const circumradius = 2.75;
const a = 2 * circumradius * Math.sin(angleA);
const b = 2 * circumradius * Math.sin(angleB);
const c = 2 * circumradius * Math.sin(angleC);
const points = {
A: new THREE.Vector3(0, 0, 0),
B: new THREE.Vector3(c, 0, 0),
C: new THREE.Vector3(
b * Math.cos(angleA),
b * Math.sin(angleA),
0
)
};
const centroid = points.A.clone()
.add(points.B)
.add(points.C)
.multiplyScalar(1 / 3);
Object.values(points).forEach((point) => point.sub(centroid));
createTrianglePrism([points.A, points.B, points.C]);
createSquare("A", points.B, points.C, points.A, colors.A);
createSquare("B", points.C, points.A, points.B, colors.B);
createSquare("C", points.A, points.B, points.C, colors.C);
createAngleArc("A", points);
createAngleArc("B", points);
createAngleArc("C", points);
const areas = {
a: a * a,
b: b * b,
c: c * c
};
const values = {
a: areas.a.toFixed(2),
b: areas.b.toFixed(2),
c: areas.c.toFixed(2)
};
labelElements.squareA.textContent = `a² ${values.a}`;
labelElements.squareB.textContent = `b² ${values.b}`;
labelElements.squareC.textContent = `c² ${values.c}`;
updateControls();
updateAreaComparison(areas);
updateEquation(areas);
world.updateMatrixWorld(true);
fitCamera();
}
function fitCamera() {
if (!world.children.length) {
return;
}
const width = Math.max(stage.clientWidth, 1);
const height = Math.max(stage.clientHeight, 1);
const aspect = width / height;
world.updateMatrixWorld(true);
const bounds = new THREE.Box3().setFromObject(world, true);
const target = bounds.getCenter(new THREE.Vector3());
const distance = 24;
const horizontal = Math.cos(orbit.elevation) * distance;
camera.position.set(
target.x + Math.sin(orbit.azimuth) * horizontal,
target.y + Math.sin(orbit.elevation) * distance,
target.z + Math.cos(orbit.azimuth) * horizontal
);
camera.lookAt(target);
camera.updateMatrixWorld(true);
const corners = [];
for (const x of [bounds.min.x, bounds.max.x]) {
for (const y of [bounds.min.y, bounds.max.y]) {
for (const z of [bounds.min.z, bounds.max.z]) {
corners.push(new THREE.Vector3(x, y, z));
}
}
}
const inverse = camera.matrixWorld.clone().invert();
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;
corners.forEach((corner) => {
corner.applyMatrix4(inverse);
minX = Math.min(minX, corner.x);
maxX = Math.max(maxX, corner.x);
minY = Math.min(minY, corner.y);
maxY = Math.max(maxY, corner.y);
});
const centerX = (minX + maxX) / 2;
const centerY = (minY + maxY) / 2;
const right = new THREE.Vector3(1, 0, 0)
.applyQuaternion(camera.quaternion);
const up = new THREE.Vector3(0, 1, 0)
.applyQuaternion(camera.quaternion);
const offset = right.multiplyScalar(centerX)
.add(up.multiplyScalar(centerY));
camera.position.add(offset);
target.add(offset);
camera.lookAt(target);
camera.updateMatrixWorld(true);
const halfHeight = Math.max(
(maxY - minY) / 2,
(maxX - minX) / (2 * aspect)
) * 1.22;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function updateLabels() {
const width = stage.clientWidth;
const height = stage.clientHeight;
camera.updateMatrixWorld(true);
Object.entries(labelPositions).forEach(([key, position]) => {
const element = labelElements[key];
if (!element) {
return;
}
labelWorld.copy(position).project(camera);
const x = (labelWorld.x * 0.5 + 0.5) * width;
const y = (-labelWorld.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.display =
labelWorld.z >= -1 && labelWorld.z <= 1 ? "block" : "none";
});
}
function resize() {
renderer.setSize(
Math.max(stage.clientWidth, 1),
Math.max(stage.clientHeight, 1),
false
);
fitCamera();
}
function setAngle(key, value) {
const oldValue = angles[key];
const delta = value - oldValue;
const otherKeys = ["A", "B", "C"].filter((item) => item !== key);
const oldOtherTotal = 180 - oldValue;
angles[key] = value;
otherKeys.forEach((otherKey) => {
angles[otherKey] +=
delta * (angles[otherKey] / oldOtherTotal);
});
}
document.querySelectorAll("[data-angle]").forEach((range) => {
range.addEventListener("input", () => {
setAngle(range.dataset.angle, Number(range.value));
rebuild();
});
});
document.querySelectorAll("[data-right]").forEach((button) => {
button.addEventListener("click", () => {
const rightKey = button.dataset.right;
angles.A = rightKey === "A" ? 90 : 45;
angles.B = rightKey === "B" ? 90 : 45;
angles.C = rightKey === "C" ? 90 : 45;
rebuild();
});
});
document.querySelector("#resetShape").addEventListener("click", () => {
angles.A = 60;
angles.B = 60;
angles.C = 60;
rebuild();
});
document.querySelector("#frontView").addEventListener("click", () => {
orbit.azimuth = 0;
orbit.elevation = 0;
orbit.zoom = 1;
fitCamera();
});
// Direct camera controls, implemented without Three.js addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
stage.addEventListener("pointerdown", (event) => {
if (event.target.closest("button, input")) {
return;
}
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
stage.setPointerCapture(event.pointerId);
});
stage.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.azimuth -= deltaX * 0.006;
orbit.elevation = clamp(
orbit.elevation + deltaY * 0.004,
0.02,
1.2
);
fitCamera();
});
function endDrag(event) {
dragging = false;
if (stage.hasPointerCapture(event.pointerId)) {
stage.releasePointerCapture(event.pointerId);
}
}
stage.addEventListener("pointerup", endDrag);
stage.addEventListener("pointercancel", endDrag);
stage.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.55,
2.5
);
fitCamera();
},
{ passive: false }
);
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
rebuild();
resize();
function animate() {
requestAnimationFrame(animate);
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```