Prompt specification
Build or customize the Three.js 3D scene "Complexity crossover exploration tool" 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: Complexity crossover exploration tool
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/complexity-crossover-exploration-tool-x1844
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Let users vary constant costs in simple linear and quadratic models; show finite-size crossovers while explaining that asymptotic growth describes the large-input trend.
Acceptance checks: Determine the winner from the same two evaluated numeric values displayed for the selected input, with an explicit tie state. Include an adjustable input size and a visible test point on either side of the crossover.
Return a single complete HTML document. It must work in a sandbox that allows scripts, with the cdnjs Three.js r160 module and inline CSS. Use procedural geometry and local button handlers; no native form submission, pointer lock, remote assets, or runtime network requests besides Three.js.
Working Three.js Starter (complexity-crossover-exploration-tool-x1844):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Finite-Size Crossover Laboratory</title>
<style>
:root {
--bg: #07101d;
--bg-2: #0b1726;
--panel: rgba(13, 25, 42, 0.94);
--panel-soft: rgba(21, 37, 58, 0.72);
--line: rgba(154, 181, 211, 0.16);
--text: #edf5ff;
--muted: #94a9bf;
--linear: #59ddd3;
--quadratic: #ff8a62;
--gold: #ffd166;
--danger: #ff6b7a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--text);
background:
radial-gradient(circle at 18% 12%, rgba(43, 107, 132, 0.2), transparent 32rem),
radial-gradient(circle at 90% 85%, rgba(111, 58, 99, 0.14), transparent 30rem),
var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.topbar {
height: 66px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(6, 14, 25, 0.78);
backdrop-filter: blur(16px);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 34px;
flex: 0 0 auto;
border: 1px solid rgba(89, 221, 211, 0.45);
border-radius: 10px;
background: linear-gradient(145deg, rgba(89, 221, 211, 0.25), rgba(255, 138, 98, 0.18));
position: relative;
box-shadow: 0 0 26px rgba(89, 221, 211, 0.12);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
bottom: 8px;
width: 8px;
border-radius: 2px 2px 0 0;
transform-origin: bottom center;
}
.brand-mark::before {
height: 15px;
left: 8px;
background: var(--linear);
transform: rotate(-22deg);
}
.brand-mark::after {
height: 9px;
right: 8px;
background: var(--quadratic);
transform: rotate(22deg);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.2;
letter-spacing: 0.02em;
}
.brand p {
margin: 3px 0 0;
color: var(--muted);
font-size: 11px;
}
.top-note {
color: var(--muted);
font-size: 12px;
text-align: right;
}
.top-note strong {
color: var(--gold);
font-weight: 650;
}
.app {
height: calc(100vh - 66px);
height: calc(100dvh - 66px);
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
}
.viewport {
position: relative;
min-width: 0;
overflow: hidden;
isolation: isolate;
background:
radial-gradient(circle at 48% 42%, rgba(46, 91, 120, 0.14), transparent 36rem),
linear-gradient(180deg, rgba(7, 16, 29, 0.55), rgba(5, 11, 20, 0.9));
}
.viewport canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
.viewport canvas:active {
cursor: grabbing;
}
.scene-hud {
position: absolute;
z-index: 4;
pointer-events: none;
}
.legend {
top: 18px;
left: 18px;
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.legend-chip,
.scene-help {
border: 1px solid rgba(166, 193, 220, 0.16);
border-radius: 999px;
background: rgba(8, 18, 31, 0.72);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(10px);
}
.legend-chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
font-size: 11px;
color: #dce9f6;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.legend-dot.linear {
color: var(--linear);
background: var(--linear);
}
.legend-dot.quadratic {
color: var(--quadratic);
background: var(--quadratic);
}
.legend-dot.crossover {
color: var(--gold);
background: var(--gold);
}
.scene-help {
left: 18px;
bottom: 18px;
padding: 8px 11px;
color: var(--muted);
font-size: 11px;
}
.camera-tools {
position: absolute;
z-index: 5;
top: 18px;
right: 18px;
display: flex;
gap: 6px;
}
.icon-button {
width: 36px;
height: 36px;
border: 1px solid rgba(166, 193, 220, 0.18);
border-radius: 10px;
background: rgba(8, 18, 31, 0.76);
cursor: pointer;
transition: 160ms ease;
}
.icon-button:hover {
border-color: rgba(89, 221, 211, 0.5);
background: rgba(19, 42, 61, 0.9);
transform: translateY(-1px);
}
.scene-label {
position: absolute;
z-index: 3;
pointer-events: none;
white-space: nowrap;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.01em;
text-shadow: 0 2px 8px rgba(0, 0, 0, 0.85);
}
.scene-label.linear {
color: var(--linear);
}
.scene-label.quadratic {
color: var(--quadratic);
}
.scene-label.crossover {
color: var(--gold);
}
.scene-label.test {
color: #f4f8ff;
}
.scene-label.point {
padding: 4px 7px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(5, 13, 23, 0.84);
font-size: 10px;
}
.axis-label {
position: absolute;
z-index: 2;
pointer-events: none;
color: #7890a8;
font-size: 10px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.tick-label {
position: absolute;
z-index: 2;
pointer-events: none;
color: #71879e;
font-size: 9px;
}
.panel {
overflow-y: auto;
padding: 20px;
border-left: 1px solid var(--line);
background: var(--panel);
scrollbar-width: thin;
scrollbar-color: #294057 transparent;
}
.eyebrow {
margin: 0 0 7px;
color: var(--linear);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 24px;
line-height: 1.12;
letter-spacing: -0.025em;
}
.intro {
margin: 9px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.card {
margin-top: 13px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel-soft);
}
.card-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.card-title h3 {
margin: 0;
font-size: 12px;
font-weight: 700;
letter-spacing: 0.02em;
}
.micro {
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.formula-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 13px;
}
.formula {
padding: 9px 10px;
border: 1px solid rgba(166, 193, 220, 0.11);
border-radius: 10px;
background: rgba(4, 12, 22, 0.34);
}
.formula span {
display: block;
margin-bottom: 4px;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.formula.linear span {
color: var(--linear);
}
.formula.quadratic span {
color: var(--quadratic);
}
.formula code {
color: #e8f1fb;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 11px;
}
.n-display {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.n-display label {
color: var(--muted);
font-size: 11px;
}
.n-readout {
display: flex;
align-items: center;
gap: 5px;
}
.n-readout output {
min-width: 68px;
color: #fff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 14px;
font-weight: 700;
text-align: right;
}
.step-button {
width: 27px;
height: 27px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(5, 13, 23, 0.45);
cursor: pointer;
}
.step-button:hover {
border-color: rgba(89, 221, 211, 0.45);
}
input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
accent-color: var(--linear);
cursor: pointer;
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 2px;
color: #627991;
font-size: 9px;
}
.placement-state {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(166, 193, 220, 0.13);
border-radius: 999px;
color: var(--muted);
background: rgba(4, 12, 22, 0.3);
font-size: 9px;
font-weight: 650;
}
.placement-state::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.placement-state.before {
color: var(--linear);
}
.placement-state.after {
color: var(--quadratic);
}
.placement-state.at {
color: var(--gold);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
}
.small-button {
min-height: 35px;
padding: 7px 6px;
border: 1px solid rgba(166, 193, 220, 0.16);
border-radius: 10px;
background: rgba(4, 12, 22, 0.38);
color: #dbe8f5;
font-size: 10px;
font-weight: 650;
cursor: pointer;
transition: 150ms ease;
}
.small-button:hover {
border-color: rgba(89, 221, 211, 0.46);
background: rgba(27, 56, 73, 0.58);
transform: translateY(-1px);
}
.small-button.gold:hover {
border-color: rgba(255, 209, 102, 0.55);
}
.value-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.value-box {
min-width: 0;
padding: 11px;
border: 1px solid rgba(166, 193, 220, 0.12);
border-radius: 11px;
background: rgba(4, 12, 22, 0.38);
}
.value-box span {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.value-box strong {
display: block;
overflow: hidden;
color: #fff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 17px;
text-overflow: ellipsis;
}
.value-box.linear strong {
color: var(--linear);
}
.value-box.quadratic strong {
color: var(--quadratic);
}
.winner-card {
margin-top: 9px;
padding: 12px 13px;
border: 1px solid rgba(166, 193, 220, 0.15);
border-radius: 12px;
background: rgba(4, 12, 22, 0.42);
}
.winner-card.tie {
border-color: rgba(255, 209, 102, 0.48);
background: rgba(255, 209, 102, 0.08);
}
.winner-card.linear {
border-color: rgba(89, 221, 211, 0.42);
background: rgba(89, 221, 211, 0.07);
}
.winner-card.quadratic {
border-color: rgba(255, 138, 98, 0.42);
background: rgba(255, 138, 98, 0.07);
}
.winner-card small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.winner-card strong {
display: block;
font-size: 15px;
}
.winner-card.tie strong {
color: var(--gold);
}
.winner-card.linear strong {
color: var(--linear);
}
.winner-card.quadratic strong {
color: var(--quadratic);
}
.winner-card p {
margin: 5px 0 0;
color: #9eb2c7;
font-size: 10px;
line-height: 1.45;
}
.control {
margin-top: 12px;
}
.control:first-of-type {
margin-top: 0;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
}
.control-head label {
color: #c7d5e4;
font-size: 11px;
}
.control-head output {
color: #fff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
font-weight: 700;
}
.control-note {
margin-top: 4px;
color: #70869d;
font-size: 9px;
line-height: 1.4;
}
.linear-control input {
accent-color: var(--linear);
}
.quadratic-control input {
accent-color: var(--quadratic);
}
.text-button {
padding: 0;
border: 0;
background: transparent;
color: var(--muted);
font-size: 10px;
cursor: pointer;
}
.text-button:hover {
color: #fff;
}
.crossover-readout {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
padding: 9px 10px;
border: 1px solid rgba(255, 209, 102, 0.2);
border-radius: 10px;
background: rgba(255, 209, 102, 0.055);
}
.crossover-readout span {
color: var(--muted);
font-size: 10px;
}
.crossover-readout strong {
color: var(--gold);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
}
.insight {
padding: 11px 0;
border-top: 1px solid var(--line);
}
.insight:first-child {
padding-top: 0;
border-top: 0;
}
.insight h4 {
margin: 0 0 5px;
color: #dce9f6;
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.insight p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.insight p strong {
color: #e8f2fc;
}
.footnote {
margin: 15px 2px 2px;
color: #61778e;
font-size: 9px;
line-height: 1.5;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.topbar {
height: auto;
min-height: 64px;
padding: 12px 15px;
}
.top-note {
display: none;
}
.app {
height: auto;
grid-template-columns: 1fr;
}
.viewport {
height: 62vh;
min-height: 440px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
}
@media (max-width: 480px) {
.panel {
padding: 16px;
}
.formula-row,
.value-grid {
grid-template-columns: 1fr;
}
.scene-help {
max-width: 170px;
line-height: 1.45;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Finite-Size Crossover Laboratory</h1>
<p>Linear versus quadratic evaluated at one shared input</p>
</div>
</div>
<div class="top-note"><strong>Finite input</strong> can differ from the <strong>asymptotic trend</strong>.</div>
</header>
<main class="app">
<section class="viewport" id="viewport" aria-label="Interactive three-dimensional cost comparison chart">
<div class="scene-hud legend" aria-hidden="true">
<div class="legend-chip"><span class="legend-dot linear"></span>Linear O(n)</div>
<div class="legend-chip"><span class="legend-dot quadratic"></span>Quadratic O(n²)</div>
<div class="legend-chip"><span class="legend-dot crossover"></span>Crossover</div>
</div>
<div class="camera-tools">
<button class="icon-button" id="zoomIn" type="button" aria-label="Zoom in">+</button>
<button class="icon-button" id="zoomOut" type="button" aria-label="Zoom out">−</button>
<button class="icon-button" id="resetCamera" type="button" aria-label="Reset camera view" title="Reset camera view">↺</button>
</div>
<div class="scene-help">Drag to orbit · wheel to zoom</div>
<div class="scene-label linear" id="linearCurveLabel">Linear · O(n)</div>
<div class="scene-label quadratic" id="quadraticCurveLabel">Quadratic · O(n²)</div>
<div class="scene-label crossover" id="crossoverLabel">Crossover</div>
<div class="scene-label test" id="testLabel">Test n</div>
<div class="scene-label point linear" id="linearPointLabel">L</div>
<div class="scene-label point quadratic" id="quadraticPointLabel">Q</div>
<div class="axis-label" id="axisXLabel">Input size n</div>
<div class="axis-label" id="axisYLabel">Evaluated cost</div>
<div class="tick-label" id="xTick0"></div>
<div class="tick-label" id="xTick1"></div>
<div class="tick-label" id="xTick2"></div>
<div class="tick-label" id="xTick3"></div>
<div class="tick-label" id="xTick4"></div>
<div class="tick-label" id="yTick0"></div>
<div class="tick-label" id="yTick1"></div>
<div class="tick-label" id="yTick2"></div>
<div class="tick-label" id="yTick3"></div>
<div class="tick-label" id="yTick4"></div>
</section>
<aside class="panel" aria-label="Model controls and evaluation results">
<p class="eyebrow">Cost models</p>
<h2>Same input.<br>Different costs.</h2>
<p class="intro">
Adjust constant coefficients, then move the test point before, at, or after the crossover.
The winner is calculated only from the two evaluations shown below.
</p>
<section class="card">
<div class="card-title">
<h3>Selected input</h3>
<span class="placement-state before" id="placementState">before crossover</span>
</div>
<div class="n-display">
<label for="inputSize">Input size, n</label>
<div class="n-readout">
<button class="step-button" id="stepDown" type="button" aria-label="Decrease input size">−</button>
<output id="inputValue" for="inputSize">10.17</output>
<button class="step-button" id="stepUp" type="button" aria-label="Increase input size">+</button>
</div>
</div>
<input id="inputSize" type="range" min="0.01" max="80" step="any" value="10.17" aria-label="Selected input size">
<div class="range-ends"><span>0.01</span><span id="inputMaxLabel">80</span></div>
<div class="button-row" style="margin-top: 11px;">
<button class="small-button" id="placeBefore" type="button">Before n*</button>
<button class="small-button gold" id="placeAt" type="button">At n* · tie</button>
<button class="small-button" id="placeAfter" type="button">After n*</button>
</div>
</section>
<section class="card">
<div class="card-title">
<h3>Evaluated values</h3>
<span class="micro">same n for both</span>
</div>
<div class="formula-row">
<div class="formula linear">
<span>Linear</span>
<code id="linearFormula">L(n) = 4.0n + 12</code>
</div>
<div class="formula quadratic">
<span>Quadratic</span>
<code id="quadraticFormula">Q(n) = 0.25n² + 8</code>
</div>
</div>
<div class="value-grid">
<div class="value-box linear">
<span>Linear evaluation</span>
<strong id="linearValue">52.66</strong>
</div>
<div class="value-box quadratic">
<span>Quadratic evaluation</span>
<strong id="quadraticValue">33.42</strong>
</div>
</div>
<div class="winner-card linear" id="winnerCard" aria-live="polite">
<small>Decision at selected n</small>
<strong id="winnerTitle">Linear wins</strong>
<p id="winnerReason">52.66 < 33.42 at the selected input.</p>
</div>
</section>
<section class="card">
<div class="card-title">
<h3>Constant costs</h3>
<button class="text-button" id="resetConstants" type="button">Reset constants</button>
</div>
<div class="control linear-control">
<div class="control-head">
<label for="linearSlope">Linear slope, a</label>
<output id="linearSlopeValue" for="linearSlope">4.0</output>
</div>
<input id="linearSlope" type="range" min="0.5" max="8" step="0.1" value="4">
<div class="control-note">Constant coefficient of n in L(n) = an + b.</div>
</div>
<div class="control linear-control">
<div class="control-head">
<label for="linearSetup">Linear setup cost, b</label>
<output id="linearSetupValue" for="linearSetup">12</output>
</div>
<input id="linearSetup" type="range" min="10" max="40" step="1" value="12">
<div class="control-note">Fixed overhead paid by the linear model.</div>
</div>
<div class="control quadratic-control">
<div class="control-head">
<label for="quadraticCoefficient">Quadratic coefficient, c</label>
<output id="quadraticCoefficientValue" for="quadraticCoefficient">0.25</output>
</div>
<input id="quadraticCoefficient" type="range" min="0.05" max="1.5" step="0.01" value="0.25">
<div class="control-note">Constant coefficient of n² in Q(n) = cn² + d.</div>
</div>
<div class="control quadratic-control">
<div class="control-head">
<label for="quadraticSetup">Quadratic setup cost, d</label>
<output id="quadraticSetupValue" for="quadraticSetup">8</output>
</div>
<input id="quadraticSetup" type="range" min="0" max="10" step="1" value="8">
<div class="control-note">Fixed overhead paid by the quadratic model.</div>
</div>
</section>
<section class="card">
<div class="card-title">
<h3>Crossover and interpretation</h3>
</div>
<div class="crossover-readout">
<span>Equal-cost crossover, n*</span>
<strong id="crossoverValue">16.94</strong>
</div>
<div class="insight">
<h4>Finite-size result</h4>
<p id="finiteInsight"></p>
</div>
<div class="insight">
<h4>Asymptotic direction</h4>
<p>
As <strong>n → ∞</strong>, the fixed <strong>cn²</strong> term eventually dominates the
<strong>an</strong> term. Thus quadratic is the large-input trend even when linear wins at a smaller finite n.
</p>
</div>
</section>
<p class="footnote">
The crossover marker shows where L(n*) = Q(n*). Asymptotic growth describes the eventual large-input ranking;
it does not guarantee that the same model wins at every finite input.
</p>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const ui = {
inputSize: document.getElementById("inputSize"),
inputValue: document.getElementById("inputValue"),
inputMaxLabel: document.getElementById("inputMaxLabel"),
placementState: document.getElementById("placementState"),
linearValue: document.getElementById("linearValue"),
quadraticValue: document.getElementById("quadraticValue"),
winnerCard: document.getElementById("winnerCard"),
winnerTitle: document.getElementById("winnerTitle"),
winnerReason: document.getElementById("winnerReason"),
linearFormula: document.getElementById("linearFormula"),
quadraticFormula: document.getElementById("quadraticFormula"),
linearSlope: document.getElementById("linearSlope"),
linearSetup: document.getElementById("linearSetup"),
quadraticCoefficient: document.getElementById("quadraticCoefficient"),
quadraticSetup: document.getElementById("quadraticSetup"),
linearSlopeValue: document.getElementById("linearSlopeValue"),
linearSetupValue: document.getElementById("linearSetupValue"),
quadraticCoefficientValue: document.getElementById("quadraticCoefficientValue"),
quadraticSetupValue: document.getElementById("quadraticSetupValue"),
crossoverValue: document.getElementById("crossoverValue"),
finiteInsight: document.getElementById("finiteInsight"),
linearCurveLabel: document.getElementById("linearCurveLabel"),
quadraticCurveLabel: document.getElementById("quadraticCurveLabel"),
crossoverLabel: document.getElementById("crossoverLabel"),
testLabel: document.getElementById("testLabel"),
linearPointLabel: document.getElementById("linearPointLabel"),
quadraticPointLabel: document.getElementById("quadraticPointLabel"),
axisXLabel: document.getElementById("axisXLabel"),
axisYLabel: document.getElementById("axisYLabel")
};
const xTickElements = [0, 1, 2, 3, 4].map(i => document.getElementById(`xTick${i}`));
const yTickElements = [0, 1, 2, 3, 4].map(i => document.getElementById(`yTick${i}`));
const params = {
a: 4,
b: 12,
c: 0.25,
d: 8
};
const defaults = { ...params };
let inputN = 10;
let crossing = 16;
let maxInput = 80;
let nChart = 42;
let yMax = 100;
let lastEvaluation = { linear: 0, quadratic: 0, winner: "linear" };
const CHART_WIDTH = 10;
const CHART_HEIGHT = 6;
const CHART_LEFT = -CHART_WIDTH / 2;
const CHART_RIGHT = CHART_LEFT + CHART_WIDTH;
const CHART_BOTTOM = -CHART_HEIGHT / 2;
const CHART_TOP = CHART_BOTTOM + CHART_HEIGHT;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07101d, 18, 38);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.id = "sceneCanvas";
renderer.domElement.setAttribute("aria-label", "Low-poly three-dimensional cost graph");
viewport.prepend(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xb9ddff, 0x172033, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(5, 9, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x59ddd3, 1.15);
rimLight.position.set(-7, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8a62, 16, 22, 2);
warmLight.position.set(5, -1, 5);
scene.add(warmLight);
const staticGroup = new THREE.Group();
const chartGroup = new THREE.Group();
scene.add(staticGroup, chartGroup);
function buildStaticScene() {
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1421,
roughness: 0.94,
metalness: 0.02
});
const floor = new THREE.Mesh(new THREE.PlaneGeometry(38, 28), floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.set(0, -3.48, -3.2);
staticGroup.add(floor);
const rockColors = [0x15283a, 0x1a3042, 0x20364a, 0x172c3d];
for (let i = 0; i < 15; i++) {
const radius = 0.18 + ((i * 7) % 5) * 0.055;
const geometry = new THREE.IcosahedronGeometry(radius, 0);
const material = new THREE.MeshStandardMaterial({
color: rockColors[i % rockColors.length],
roughness: 0.9,
flatShading: true
});
const rock = new THREE.Mesh(geometry, material);
const side = i % 2 === 0 ? -1 : 1;
rock.position.set(
side * (6.1 + (i % 4) * 0.75),
-3.42 + radius * 0.45,
-2.8 + (i % 5) * 1.35
);
rock.rotation.set(i * 0.31, i * 0.47, i * 0.19);
staticGroup.add(rock);
}
}
function disposeGroup(group) {
const geometries = new Set();
const materials = new Set();
group.traverse(object => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach(material => materials.add(material));
} else {
materials.add(object.material);
}
}
});
geometries.forEach(geometry => geometry.dispose());
materials.forEach(material => material.dispose());
group.clear();
}
function costLinear(n) {
return params.a * n + params.b;
}
function costQuadratic(n) {
return params.c * n * n + params.d;
}
function computeCrossing() {
const discriminant = params.a * params.a + 4 * params.c * (params.b - params.d);
return (params.a + Math.sqrt(Math.max(0, discriminant))) / (2 * params.c);
}
function mapX(n) {
return CHART_LEFT + (n / nChart) * CHART_WIDTH;
}
function mapY(cost) {
return CHART_BOTTOM + (cost / yMax) * CHART_HEIGHT;
}
function makePoint(n, cost, z = 0.12) {
return new THREE.Vector3(mapX(n), mapY(cost), z);
}
function makeLine(points, material) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
return new THREE.Line(geometry, material);
}
function addCurve(costFunction, color, z) {
const segments = 56;
const points = [];
const railGeometry = new THREE.CylinderGeometry(0.037, 0.037, 1, 6, 1, false);
const railMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.2,
roughness: 0.5,
metalness: 0.04,
flatShading: true
});
const nodeGeometry = new THREE.SphereGeometry(0.065, 8, 6);
const nodeMaterial = new THREE.MeshBasicMaterial({ color });
for (let i = 0; i <= segments; i++) {
const n = nChart * i / segments;
points.push(makePoint(n, costFunction(n), z + 0.06));
}
for (let i = 0; i < segments; i++) {
const n1 = nChart * i / segments;
const n2 = nChart * (i + 1) / segments;
const p1 = makePoint(n1, costFunction(n1), z);
const p2 = makePoint(n2, costFunction(n2), z);
const direction = p2.clone().sub(p1);
const length = direction.length();
const segment = new THREE.Mesh(railGeometry, railMaterial);
segment.position.copy(p1).add(p2).multiplyScalar(0.5);
segment.scale.set(1, length, 1);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
chartGroup.add(segment);
if (i % 7 === 0) {
const node = new THREE.Mesh(nodeGeometry, nodeMaterial);
node.position.copy(p1);
chartGroup.add(node);
}
}
chartGroup.add(makeLine(
points,
new THREE.LineBasicMaterial({ color, transparent: true, opacity: 0.85 })
));
}
function addMarker(position, color, radius = 0.15, wireframe = false) {
const geometry = new THREE.OctahedronGeometry(radius, 0);
const material = wireframe
? new THREE.MeshBasicMaterial({ color, wireframe: true, transparent: true, opacity: 0.95 })
: new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.34,
roughness: 0.42,
flatShading: true
});
const marker = new THREE.Mesh(geometry, material);
marker.position.copy(position);
chartGroup.add(marker);
return marker;
}
function buildChart() {
disposeGroup(chartGroup);
const board = new THREE.Mesh(
new THREE.PlaneGeometry(CHART_WIDTH, CHART_HEIGHT),
new THREE.MeshStandardMaterial({
color: 0x0d1c2c,
emissive: 0x07111d,
emissiveIntensity: 0.12,
roughness: 0.92,
metalness: 0.02,
side: THREE.DoubleSide
})
);
board.position.z = -0.42;
chartGroup.add(board);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x2b435a,
roughness: 0.62,
metalness: 0.18,
flatShading: true
});
const topFrame = new THREE.Mesh(
new THREE.BoxGeometry(CHART_WIDTH + 0.14, 0.07, 0.14),
frameMaterial
);
topFrame.position.set(0, CHART_TOP, -0.31);
chartGroup.add(topFrame);
const bottomFrame = topFrame.clone();
bottomFrame.position.y = CHART_BOTTOM;
chartGroup.add(bottomFrame);
const leftFrame = new THREE.Mesh(
new THREE.BoxGeometry(0.07, CHART_HEIGHT + 0.14, 0.14),
frameMaterial
);
leftFrame.position.set(CHART_LEFT, 0, -0.31);
chartGroup.add(leftFrame);
const rightFrame = leftFrame.clone();
rightFrame.position.x = CHART_RIGHT;
chartGroup.add(rightFrame);
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x39516b,
transparent: true,
opacity: 0.48
});
for (let i = 0; i <= 4; i++) {
const y = CHART_BOTTOM + CHART_HEIGHT * i / 4;
chartGroup.add(makeLine([
new THREE.Vector3(CHART_LEFT, y, -0.29),
new THREE.Vector3(CHART_LEFT + CHART_WIDTH, y, -0.29)
], gridMaterial));
const n = nChart * i / 4;
const x = mapX(n);
chartGroup.add(makeLine([
new THREE.Vector3(x, CHART_BOTTOM, -0.28),
new THREE.Vector3(x, CHART_TOP, -0.28)
], gridMaterial));
}
const axisMaterial = new THREE.LineBasicMaterial({
color: 0x8aa4bc,
transparent: true,
opacity: 0.8
});
chartGroup.add(makeLine([
new THREE.Vector3(CHART_LEFT, CHART_BOTTOM, -0.2),
new THREE.Vector3(CHART_LEFT + CHART_WIDTH, CHART_BOTTOM, -0.2)
], axisMaterial));
chartGroup.add(makeLine([
new THREE.Vector3(CHART_LEFT, CHART_BOTTOM, -0.2),
new THREE.Vector3(CHART_LEFT, CHART_TOP, -0.2)
], axisMaterial));
addCurve(costLinear, 0x59ddd3, 0.02);
addCurve(costQuadratic, 0xff8a62, 0.02);
const crossingCost = costLinear(crossing);
const crossingPoint = makePoint(crossing, crossingCost, 0.42);
const crossingLine = makeLine([
new THREE.Vector3(crossingPoint.x, CHART_BOTTOM, 0.12),
crossingPoint.clone()
], new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.12,
gapSize: 0.09,
transparent: true,
opacity: 0.8
}));
crossingLine.computeLineDistances();
chartGroup.add(crossingLine);
addMarker(crossingPoint, 0xffd166, 0.18);
const linearCost = costLinear(inputN);
const quadraticCost = costQuadratic(inputN);
const linearPoint = makePoint(inputN, linearCost, 0.34);
const quadraticPoint = makePoint(inputN, quadraticCost, 0.34);
const testTop = makePoint(
inputN,
Math.max(linearCost, quadraticCost),
0.2
);
const testLine = makeLine([
new THREE.Vector3(linearPoint.x, CHART_BOTTOM, 0.16),
testTop
], new THREE.LineDashedMaterial({
color: 0xd9e9f8,
dashSize: 0.1,
gapSize: 0.09,
transparent: true,
opacity: 0.72
}));
testLine.computeLineDistances();
chartGroup.add(testLine);
chartGroup.add(makeLine([
linearPoint.clone(),
quadraticPoint.clone()
], new THREE.LineBasicMaterial({
color: 0xd8e6f2,
transparent: true,
opacity: 0.8
})));
const linearMarker = addMarker(linearPoint, 0x59ddd3, 0.145);
const quadraticMarker = addMarker(quadraticPoint, 0xff8a62, 0.145);
linearMarker.rotation.y = 0.35;
quadraticMarker.rotation.y = -0.35;
const winnerPoint = lastEvaluation.winner === "linear"
? linearPoint
: lastEvaluation.winner === "quadratic"
? quadraticPoint
: linearPoint;
const winnerHalo = addMarker(
new THREE.Vector3(winnerPoint.x, winnerPoint.y, 0.52),
lastEvaluation.winner === "tie" ? 0xffd166 : (lastEvaluation.winner === "linear" ? linearMarker : quadraticMarker).material.color,
0.25,
true
);
winnerHalo.rotation.z = Math.PI / 4;
const testFoot = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.27, 4),
new THREE.MeshBasicMaterial({ color: 0xf4f8ff })
);
testFoot.position.set(linearPoint.x, CHART_BOTTOM + 0.135, 0.36);
chartFootMarker(testFoot);
chartGroup.add(testFoot);
}
function chartFootMarker(marker) {
marker.userData.isTestFoot = true;
}
function formatCost(value) {
if (Math.abs(value) >= 1000) {
return value.toLocaleString("en-US", { maximumFractionDigits: 0 });
}
return value.toFixed(2);
}
function formatCompact(value) {
if (Math.abs(value) >= 1000) {
return `${(value / 1000).toFixed(value >= 10000 ? 0 : 1)}k`;
}
if (Math.abs(value) >= 100) {
return value.toFixed(0);
}
return value.toFixed(1);
}
function decideWinner(linear, quadratic) {
const tolerance = Math.max(1e-9, Math.max(Math.abs(linear), Math.abs(quadratic)) * 1e-9);
if (Math.abs(linear - quadratic) <= tolerance) return "tie";
return linear < quadratic ? "linear" : "quadratic";
}
function updateUI() {
const linear = costLinear(inputN);
const quadratic = costQuadratic(inputN);
const winner = decideWinner(linear, quadratic);
lastEvaluation = { linear, quadratic, winner };
ui.inputValue.textContent = inputN.toFixed(2);
ui.inputSize.setAttribute("aria-valuetext", `Input size ${inputN.toFixed(2)}`);
ui.inputMaxLabel.textContent = maxInput.toFixed(0);
ui.linearValue.textContent = formatCost(linear);
ui.quadraticValue.textContent = formatCost(quadratic);
ui.winnerCard.classList.remove("tie", "linear", "quadratic");
ui.winnerCard.classList.add(winner);
if (winner === "tie") {
ui.winnerTitle.textContent = "Tie";
ui.winnerReason.textContent =
`Both models evaluate to ${formatCost(linear)} at n = ${inputN.toFixed(2)}.`;
} else if (winner === "linear") {
ui.winnerTitle.textContent = "Linear wins";
ui.winnerReason.textContent =
`${formatCost(linear)} < ${formatCost(quadratic)} at the selected input.`;
} else {
ui.winnerTitle.textContent = "Quadratic wins";
ui.winnerReason.textContent =
`${formatCost(quadratic)} < ${formatCost(linear)} at the selected input.`;
}
const crossingTolerance = Math.max(
1e-9,
Math.max(Math.abs(linear), Math.abs(quadratic)) * 1e-9
);
ui.placementState.classList.remove("before", "after", "at");
if (winner === "tie") {
ui.placementState.classList.add("at");
ui.placementState.textContent = "at crossover";
} else if (inputN < crossing) {
ui.placementState.classList.add("before");
ui.placementState.textContent = "before crossover";
} else {
ui.placementState.classList.add("after");
ui.placementState.textContent = "after crossover";
}
ui.linearFormula.textContent = `L(n) = ${params.a.toFixed(1)}n + ${params.b.toFixed(0)}`;
ui.quadraticFormula.textContent = `Q(n) = ${params.c.toFixed(2)}n² + ${params.d.toFixed(0)}`;
ui.linearSlopeValue.textContent = params.a.toFixed(1);
ui.linearSetupValue.textContent = params.b.toFixed(0);
ui.quadraticCoefficientValue.textContent = params.c.toFixed(2);
ui.quadraticSetupValue.textContent = params.d.toFixed(0);
ui.crossoverValue.textContent = crossing.toFixed(2);
ui.crossoverLabel.textContent = `Crossover n* = ${crossing.toFixed(2)}`;
ui.testLabel.textContent = `Test n = ${inputN.toFixed(2)}`;
ui.linearPointLabel.textContent = `L ${formatCost(linear)}`;
ui.quadraticPointLabel.textContent = `Q ${formatCost(quadratic)}`;
if (winner === "tie") {
ui.finiteInsight.innerHTML =
`At the crossover, the two finite evaluations coincide at <strong>${formatCost(linear)}</strong>. ` +
`The tie occurs at one input size, not across the whole input range.`;
} else if (winner === "linear") {
const gap = quadratic - linear;
ui.finiteInsight.innerHTML =
`At this finite n, linear is lower by <strong>${formatCost(gap)}</strong>. ` +
`That finite-size result does not change the eventual asymptotic ranking.`;
} else {
const gap = linear - quadratic;
ui.finiteInsight.innerHTML =
`At this finite n, quadratic is lower by <strong>${formatCost(gap)}</strong>. ` +
`Farther beyond the crossover, its n² term increasingly dominates.`;
}
for (let i = 0; i <= 4; i++) {
xTickElements[i].textContent = formatCompact(nChart * i / 4);
yTickElements[i].textContent = formatCompact(yMax * i / 4);
}
void crossingTolerance;
}
function refresh() {
crossing = computeCrossing();
maxInput = Math.max(80, Math.ceil(crossing * 1.8));
inputN = THREE.MathUtils.clamp(inputN, 0.01, maxInput);
nChart = Math.min(
maxInput,
Math.max(30, Math.ceil(crossing * 2.4), Math.ceil(inputN * 1.18))
);
yMax = Math.max(
costLinear(nChart),
costQuadratic(nChart),
costLinear(inputN),
costQuadratic(inputN)
) * 1.06;
ui.inputSize.max = maxInput.toFixed(2);
ui.inputSize.value = inputN;
const linear = costLinear(inputN);
const quadratic = costQuadratic(inputN);
lastEvaluation = {
linear,
quadratic,
winner: decideWinner(linear, quadratic)
};
buildChart();
updateUI();
}
ui.inputSize.addEventListener("input", event => {
inputN = Number(event.target.value);
refresh();
});
document.getElementById("stepDown").addEventListener("click", () => {
inputN = Math.max(0.01, inputN - 1);
refresh();
});
document.getElementById("stepUp").addEventListener("click", () => {
inputN = Math.min(maxInput, inputN + 1);
refresh();
});
document.getElementById("placeBefore").addEventListener("click", () => {
inputN = Math.max(0.01, crossing * 0.6);
refresh();
});
document.getElementById("placeAt").addEventListener("click", () => {
inputN = crossing;
refresh();
});
document.getElementById("placeAfter").addEventListener("click", () => {
inputN = crossing * 1.4;
refresh();
});
const parameterBindings = [
[ui.linearSlope, "a", value => value],
[ui.linearSetup, "b", value => value],
[ui.quadraticCoefficient, "c", value => value],
[ui.quadraticSetup, "d", value => value]
];
parameterBindings.forEach(([element, key]) => {
element.addEventListener("input", event => {
params[key] = Number(event.target.value);
refresh();
});
});
document.getElementById("resetConstants").addEventListener("click", () => {
Object.assign(params, defaults);
ui.linearSlope.value = params.a;
ui.linearSetup.value = params.b;
ui.quadraticCoefficient.value = params.c;
ui.quadraticSetup.value = params.d;
crossing = computeCrossing();
inputN = Math.max(0.01, crossing * 0.6);
refresh();
});
let yaw = 0.43;
let pitch = 0.29;
let radius = 14.2;
const cameraTarget = new THREE.Vector3(0, -0.12, -0.15);
function updateCamera() {
const horizontal = radius * Math.cos(pitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(yaw),
cameraTarget.y + radius * Math.sin(pitch),
cameraTarget.z + horizontal * Math.cos(yaw)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
yaw = 0.43;
pitch = 0.29;
radius = 14.2;
updateCamera();
}
document.getElementById("resetCamera").addEventListener("click", resetCamera);
document.getElementById("zoomIn").addEventListener("click", () => {
radius = THREE.MathUtils.clamp(radius - 1.25, 8.5, 21);
updateCamera();
});
document.getElementById("zoomOut").addEventListener("click", () => {
radius = THREE.MathUtils.clamp(radius + 1.25, 8.5, 21);
updateCamera();
});
const canvas = renderer.domElement;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", event => {
if (event.pointerType === "mouse" && event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
yaw -= deltaX * 0.006;
pitch = THREE.MathUtils.clamp(pitch + deltaY * 0.0045, 0.08, 1.08);
updateCamera();
});
const stopDragging = () => {
dragging = false;
};
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", stopDragging);
canvas.addEventListener("wheel", event => {
event.preventDefault();
radius = THREE.MathUtils.clamp(radius + event.deltaY * 0.009, 8.5, 21);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
function placeLabel(element, worldPosition, translateX = "-50%", translateY = "-50%") {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.transform = `translate(${translateX}, ${translateY})`;
}
function updateOverlayPositions() {
const linearCost = costLinear(inputN);
const quadraticCost = costQuadratic(inputN);
const linearPoint = makePoint(inputN, linearCost, 0.38);
const quadraticPoint = makePoint(inputN, quadraticCost, 0.38);
const crossingPoint = makePoint(crossing, costLinear(crossing), 0.48);
placeLabel(
ui.linearCurveLabel,
makePoint(nChart * 0.79, costLinear(nChart * 0.79), 0.22),
"-50%", "-145%"
);
placeLabel(
ui.quadraticCurveLabel,
makePoint(nChart * 0.94, costQuadratic(nChart * 0.94), 0.22),
"-50%", "-145%"
);
placeLabel(ui.crossoverLabel, crossingPoint, "12%", "-135%");
placeLabel(
ui.testLabel,
new THREE.Vector3(mapX(inputN), CHART_BOTTOM + 0.03, 0.42),
"-50%", "32%"
);
placeLabel(ui.linearPointLabel, linearPoint, "-108%", "-50%");
placeLabel(ui.quadraticPointLabel, quadraticPoint, "14%", "-50%");
placeLabel(
ui.axisXLabel,
new THREE.Vector3(0, CHART_BOTTOM - 0.25, 0),
"-50%", "0%"
);
placeLabel(
ui.axisYLabel,
new THREE.Vector3(CHART_LEFT - 0.3, 0, 0),
"-100%", "-50%"
);
for (let i = 0; i <= 4; i++) {
const n = nChart * i / 4;
placeLabel(
xTickElements[i],
new THREE.Vector3(mapX(n), CHART_BOTTOM - 0.04, 0),
"-50%", "18%"
);
const cost = yMax * i / 4;
placeLabel(
yTickElements[i],
new THREE.Vector3(CHART_LEFT - 0.1, mapY(cost), 0),
"-108%", "-50%"
);
}
}
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
buildStaticScene();
crossing = computeCrossing();
inputN = Math.max(0.01, crossing * 0.6);
refresh();
resize();
updateCamera();
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateOverlayPositions();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```