Prompt specification
Build or customize the Three.js 3D scene "Decision boundary over synthetic points" 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: Decision boundary over synthetic points
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/decision-boundary-over-synthetic-points-x1894
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a perceptron and its decision boundary. Place two classes of synthetic points on a plane with a linear boundary from displayed weights; show which points are misclassified rather than hiding imperfect separation. Stylized solid forms.
Working Three.js Starter (decision-boundary-over-synthetic-points-x1894):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Perceptron Decision Boundary</title>
<style>
:root {
--ink: #24313b;
--muted: #6f766f;
--paper: #f4efe5;
--panel: rgba(249, 246, 239, 0.94);
--line: rgba(36, 49, 59, 0.14);
--coral: #ef6548;
--blue: #355f82;
--gold: #f2b84b;
--red: #d94835;
--green: #34755a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9d5cc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#viewport {
position: absolute;
inset: 0 370px 0 0;
overflow: hidden;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
#panel {
position: absolute;
top: 18px;
right: 18px;
bottom: 18px;
width: 334px;
padding: 22px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(47, 43, 36, 0.18),
inset 0 0 0 1px rgba(36, 49, 59, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
margin-bottom: 9px;
color: #8b765e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 29px;
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.05;
}
.intro {
margin: 12px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section {
padding: 17px 0;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.02em;
text-transform: none;
}
.weight-row {
display: grid;
grid-template-columns: 28px 1fr 66px;
align-items: center;
gap: 10px;
margin: 11px 0;
}
.weight-row label {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
font-weight: 750;
}
.weight-row output {
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.48);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 700;
text-align: right;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
accent-color: var(--ink);
cursor: pointer;
}
.equation {
padding: 15px 14px;
border: 1px solid rgba(36, 49, 59, 0.1);
border-radius: 13px;
background: #fffdf7;
box-shadow: 0 5px 18px rgba(48, 42, 34, 0.045);
}
.equation-main {
min-height: 25px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 14px;
font-weight: 760;
line-height: 1.55;
}
.equation-sub {
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.metric {
padding: 13px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.42);
}
.metric strong {
display: block;
font-size: 25px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric span {
display: block;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.metric.bad strong {
color: var(--red);
}
.metric.good strong {
color: var(--green);
}
.accuracy-track {
height: 6px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(36, 49, 59, 0.1);
}
.accuracy-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: width 220ms ease;
}
.legend {
display: grid;
gap: 9px;
margin-top: 3px;
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
color: #59615e;
font-size: 12px;
}
.swatch {
width: 14px;
height: 14px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--coral);
box-shadow: 0 2px 5px rgba(42, 36, 30, 0.2);
}
.swatch.square {
border-radius: 3px;
background: var(--blue);
}
.swatch.error {
position: relative;
border: 3px solid var(--gold);
background: transparent;
box-shadow: none;
}
.swatch.error::before,
.swatch.error::after {
position: absolute;
top: 50%;
left: 50%;
width: 18px;
height: 3px;
border-radius: 2px;
background: var(--gold);
content: "";
transform: translate(-50%, -50%) rotate(45deg);
}
.swatch.error::after {
transform: translate(-50%, -50%) rotate(-45deg);
}
.note {
margin: 11px 0 0;
padding: 11px 12px;
border-left: 3px solid var(--gold);
border-radius: 0 9px 9px 0;
background: rgba(242, 184, 75, 0.13);
color: #665a48;
font-size: 11px;
line-height: 1.5;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 18px;
}
button {
min-height: 41px;
padding: 9px 10px;
border: 1px solid rgba(36, 49, 59, 0.16);
border-radius: 11px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.055em;
text-transform: uppercase;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
background: #fffdf8;
box-shadow: 0 5px 13px rgba(41, 38, 32, 0.09);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fffaf0;
}
button.primary:hover {
background: #34434f;
}
#scene-title {
position: absolute;
top: 24px;
left: 26px;
z-index: 2;
pointer-events: none;
}
#scene-title strong {
display: block;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#scene-title span {
display: block;
margin-top: 5px;
color: rgba(36, 49, 59, 0.58);
font-size: 11px;
}
.drag-hint {
position: absolute;
right: 18px;
bottom: 15px;
z-index: 2;
padding: 7px 10px;
border: 1px solid rgba(36, 49, 59, 0.1);
border-radius: 999px;
background: rgba(249, 246, 239, 0.72);
color: rgba(36, 49, 59, 0.62);
font-size: 10px;
font-weight: 650;
pointer-events: none;
backdrop-filter: blur(8px);
}
.scene-label {
position: absolute;
top: 0;
left: 0;
z-index: 2;
padding: 5px 8px;
border: 1px solid rgba(36, 49, 59, 0.13);
border-radius: 7px;
background: rgba(249, 246, 239, 0.85);
color: rgba(36, 49, 59, 0.72);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(6px);
}
#boundary-label {
border-color: rgba(36, 49, 59, 0.35);
background: var(--ink);
color: #fffaf0;
}
#tooltip {
position: absolute;
z-index: 5;
min-width: 174px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 11px;
background: rgba(31, 42, 50, 0.94);
color: #f9f4e8;
box-shadow: 0 10px 25px rgba(25, 27, 29, 0.24);
font-size: 11px;
line-height: 1.6;
pointer-events: none;
opacity: 0;
transform: translateY(5px);
transition:
opacity 100ms ease,
transform 100ms ease;
}
#tooltip.visible {
opacity: 1;
transform: translateY(0);
}
#tooltip .tip-title {
margin-bottom: 4px;
color: #fff;
font-weight: 800;
}
.class-a {
color: #ff896f;
}
.class-b {
color: #80b6df;
}
.tip-wrong {
color: #ffd16f;
font-weight: 750;
}
@media (max-width: 1000px) {
html,
body {
overflow: hidden;
}
#viewport {
inset: 0 0 53vh 0;
}
#panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 46vh;
padding: 17px;
border-radius: 18px;
}
#scene-title {
top: 15px;
left: 17px;
}
.drag-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="viewport" aria-label="Interactive three-dimensional perceptron scene">
<div id="scene-title">
<strong>Linear perceptron</strong>
<span>Synthetic classes · live weight geometry</span>
</div>
<div id="boundary-label" class="scene-label">Decision boundary</div>
<div id="model-label" class="scene-label">Perceptron</div>
<div id="x-label" class="scene-label">x₁</div>
<div id="y-label" class="scene-label">x₂</div>
<div id="tooltip"></div>
<div class="drag-hint">Drag to orbit · Scroll to zoom</div>
</main>
<aside id="panel">
<div class="eyebrow">Interactive experiment</div>
<h1>Where does the<br>perceptron disagree?</h1>
<p class="intro">
Change the displayed weights to move the linear boundary.
Every sample remains visible, including the noisy points that cannot
be classified correctly.
</p>
<section class="section">
<div class="section-title">
<span>Model weights</span>
<span>score = w₁x₁ + w₂x₂ + b</span>
</div>
<div class="weight-row">
<label for="weight-1">w₁</label>
<input id="weight-1" type="range" min="-2" max="2" step="0.01" value="0.15">
<output id="weight-1-value">+0.15</output>
</div>
<div class="weight-row">
<label for="weight-2">w₂</label>
<input id="weight-2" type="range" min="-2" max="2" step="0.01" value="1">
<output id="weight-2-value">+1.00</output>
</div>
<div class="weight-row">
<label for="bias">b</label>
<input id="bias" type="range" min="-3" max="3" step="0.01" value="-0.15">
<output id="bias-value">−0.15</output>
</div>
</section>
<section class="section">
<div class="section-title">
<span>Decision boundary</span>
<span>f(x) = 0</span>
</div>
<div class="equation">
<div id="equation-main" class="equation-main">0.15 x₁ + 1.00 x₂ − 0.15 = 0</div>
<div class="equation-sub">
Predict A (+1) when the score is positive; predict B (−1) otherwise.
</div>
</div>
</section>
<section class="section">
<div class="section-title">
<span>Classification</span>
<span id="accuracy-text">81.8% accuracy</span>
</div>
<div class="metrics">
<div class="metric good">
<strong id="correct-count">18</strong>
<span>Correct</span>
</div>
<div class="metric bad">
<strong id="wrong-count">4</strong>
<span>Misclassified</span>
</div>
</div>
<div class="accuracy-track">
<div id="accuracy-fill" class="accuracy-fill"></div>
</div>
</section>
<section class="section">
<div class="section-title">
<span>Legend</span>
<span>True class</span>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch"></span>
<span>Class A / +1</span>
</div>
<div class="legend-item">
<span class="swatch square"></span>
<span>Class B / −1</span>
</div>
<div class="legend-item">
<span class="swatch error"></span>
<span>Gold ring and × = misclassified</span>
</div>
</div>
<p class="note">
Four deliberately noisy samples overlap the opposite class.
The perceptron can reduce errors, but these outliers prevent perfect
separation with a single straight line.
</p>
</section>
<div class="buttons">
<button id="train-button" class="primary" type="button">Train one epoch</button>
<button id="reset-weights" type="button">Reset weights</button>
<button id="reset-view" type="button">Reset camera</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 viewport = document.querySelector("#viewport");
const tooltip = document.querySelector("#tooltip");
const DEFAULT_WEIGHTS = Object.freeze({
w1: 0.15,
w2: 1.0,
bias: -0.15
});
let w1 = DEFAULT_WEIGHTS.w1;
let w2 = DEFAULT_WEIGHTS.w2;
let bias = DEFAULT_WEIGHTS.bias;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9d5cc);
scene.fog = new THREE.Fog(0xd9d5cc, 27, 48);
const camera = new THREE.OrthographicCamera(-7, 7, 6, -6, 0.1, 80);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Perceptron plot");
viewport.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff9e9, 0x77746d, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d7, 3.2);
keyLight.position.set(-7, 11, 13);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb9d7ed, 1.25);
fillLight.position.set(9, 1, 8);
scene.add(fillLight);
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0xeee8dc,
roughness: 0.88,
metalness: 0.02
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xb9ae9c,
roughness: 0.78
});
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xd2cec5,
roughness: 1
});
const backWall = new THREE.Mesh(
new THREE.BoxGeometry(22, 14, 0.3),
wallMaterial
);
backWall.position.set(0, -0.5, -1.15);
backWall.receiveShadow = true;
scene.add(backWall);
const board = new THREE.Mesh(
new THREE.BoxGeometry(12.2, 8.2, 0.22),
boardMaterial
);
board.position.set(0, 0, 0);
board.castShadow = true;
board.receiveShadow = true;
scene.add(board);
function addFramePiece(width, height, x, y) {
const piece = new THREE.Mesh(
new THREE.BoxGeometry(width, height, 0.12),
frameMaterial
);
piece.position.set(x, y, 0.13);
piece.castShadow = true;
scene.add(piece);
}
addFramePiece(12.35, 0.09, 0, 4.13);
addFramePiece(12.35, 0.09, 0, -4.13);
addFramePiece(0.09, 8.35, -6.13, 0);
addFramePiece(0.09, 8.35, 6.13, 0);
const gridVertices = [];
for (let x = -5; x <= 5; x++) {
gridVertices.push(x, -3.6, 0.135, x, 3.6, 0.135);
}
for (let y = -3; y <= 3; y++) {
gridVertices.push(-5.7, y, 0.135, 5.7, y, 0.135);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8d877d,
transparent: true,
opacity: 0.19
})
);
scene.add(grid);
const axisMaterial = new THREE.MeshStandardMaterial({
color: 0x4d595f,
roughness: 0.72
});
function cylinderBetween(a, b, radius, material, radialSegments = 12) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
function coneAt(point, direction, radius, height, material) {
const cone = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 16),
material
);
cone.position.copy(point).addScaledVector(direction, -height * 0.5);
cone.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
cone.castShadow = true;
scene.add(cone);
return cone;
}
const xAxisStart = new THREE.Vector3(-5.7, -3.62, 0.19);
const xAxisEnd = new THREE.Vector3(5.7, -3.62, 0.19);
const yAxisStart = new THREE.Vector3(-5.72, -3.62, 0.19);
const yAxisEnd = new THREE.Vector3(-5.72, 3.62, 0.19);
cylinderBetween(xAxisStart, xAxisEnd, 0.022, axisMaterial);
cylinderBetween(yAxisStart, yAxisEnd, 0.022, axisMaterial);
coneAt(xAxisEnd, new THREE.Vector3(1, 0, 0), 0.09, 0.27, axisMaterial);
coneAt(yAxisEnd, new THREE.Vector3(0, 1, 0), 0.09, 0.27, axisMaterial);
const boundaryMaterial = new THREE.MeshStandardMaterial({
color: 0x24313b,
roughness: 0.42,
metalness: 0.12,
emissive: 0x11171b,
emissiveIntensity: 0.14
});
const boundaryMesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
boundaryMaterial
);
boundaryMesh.castShadow = true;
scene.add(boundaryMesh);
const normalMaterial = new THREE.MeshStandardMaterial({
color: 0xd49b38,
roughness: 0.48,
metalness: 0.12
});
const normalShaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 1, 12),
normalMaterial
);
normalShaft.castShadow = true;
scene.add(normalShaft);
const normalHead = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.3, 16),
normalMaterial
);
normalHead.castShadow = true;
scene.add(normalHead);
const points = [
{ x: -4.9, y: 2.60, label: 1 },
{ x: -3.7, y: 3.10, label: 1 },
{ x: -2.4, y: 2.55, label: 1 },
{ x: -1.1, y: 3.15, label: 1 },
{ x: 0.25, y: 2.55, label: 1 },
{ x: 1.35, y: 3.10, label: 1 },
{ x: 2.55, y: 2.50, label: 1 },
{ x: 3.80, y: 3.00, label: 1 },
{ x: 4.90, y: 2.15, label: 1 },
{ x: -4.9, y: -2.40, label: -1 },
{ x: -3.6, y: -3.00, label: -1 },
{ x: -2.3, y: -2.35, label: -1 },
{ x: -1.0, y: -3.05, label: -1 },
{ x: 0.45, y: -2.50, label: -1 },
{ x: 1.50, y: -3.00, label: -1 },
{ x: 2.50, y: -2.25, label: -1 },
{ x: 3.70, y: -2.85, label: -1 },
{ x: 4.90, y: -1.95, label: -1 },
// Deliberately noisy examples: they make perfect separation impossible.
{ x: -2.20, y: -0.45, label: 1 },
{ x: 0.90, y: -0.55, label: 1 },
{ x: -0.50, y: 0.55, label: -1 },
{ x: 2.25, y: 0.55, label: -1 }
];
const markerMeshes = [];
const pointObjects = [];
const coralMaterial = new THREE.MeshStandardMaterial({
color: 0xef6548,
roughness: 0.5,
metalness: 0.04
});
const blueMaterial = new THREE.MeshStandardMaterial({
color: 0x355f82,
roughness: 0.54,
metalness: 0.07
});
const shadowMaterial = new THREE.MeshBasicMaterial({
color: 0x5d5549,
transparent: true,
opacity: 0.12,
depthWrite: false
});
const errorMaterial = new THREE.MeshStandardMaterial({
color: 0xffc857,
roughness: 0.4,
metalness: 0.05,
emissive: 0x7a4300,
emissiveIntensity: 0.24
});
const ringGeometry = new THREE.TorusGeometry(0.29, 0.036, 10, 32);
const crossGeometry = new THREE.BoxGeometry(0.42, 0.062, 0.075);
points.forEach((point, index) => {
const group = new THREE.Group();
group.position.set(point.x, point.y, 0.38);
scene.add(group);
const shadow = new THREE.Mesh(
new THREE.CircleGeometry(point.label === 1 ? 0.245 : 0.255, 24),
shadowMaterial
);
shadow.position.z = -0.185;
group.add(shadow);
const material = (point.label === 1 ? coralMaterial : blueMaterial).clone();
const marker = point.label === 1
? new THREE.Mesh(new THREE.SphereGeometry(0.17, 20, 14), material)
: new THREE.Mesh(new THREE.BoxGeometry(0.33, 0.33, 0.33), material);
marker.position.z = 0.02;
marker.rotation.z = point.label === 1 ? 0 : Math.PI * 0.08;
marker.castShadow = true;
marker.receiveShadow = true;
marker.userData.pointIndex = index;
group.add(marker);
markerMeshes.push(marker);
const ring = new THREE.Mesh(ringGeometry, errorMaterial);
ring.position.z = 0.06;
ring.castShadow = true;
group.add(ring);
const crossA = new THREE.Mesh(crossGeometry, errorMaterial);
const crossB = new THREE.Mesh(crossGeometry, errorMaterial);
crossA.position.z = 0.18;
crossB.position.z = 0.18;
crossA.rotation.z = Math.PI * 0.25;
crossB.rotation.z = -Math.PI * 0.25;
crossA.castShadow = true;
crossB.castShadow = true;
group.add(crossA, crossB);
pointObjects.push({
group,
marker,
ring,
crosses: [crossA, crossB],
material,
baseColor: material.color.clone(),
score: 0,
prediction: 1,
wrong: false
});
});
// A small solid model below the board gives the scene a tactile,
// physical quality without relying on imported geometry helpers.
const model = new THREE.Group();
scene.add(model);
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x34434d,
roughness: 0.6,
metalness: 0.12
});
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(4.7, 0.25, 0.82),
plinthMaterial
);
plinth.position.set(0, -4.76, 0.34);
plinth.castShadow = true;
plinth.receiveShadow = true;
model.add(plinth);
const rodMaterial = new THREE.MeshStandardMaterial({
color: 0xa99e8d,
roughness: 0.55,
metalness: 0.18
});
const inputGeometry = new THREE.BoxGeometry(0.43, 0.43, 0.43);
const inputColors = [0xef6548, 0x355f82, 0xe9dfcb];
const inputPositions = [-1.55, -0.45, 0.65];
inputPositions.forEach((x, i) => {
const node = new THREE.Mesh(
inputGeometry,
new THREE.MeshStandardMaterial({
color: inputColors[i],
roughness: 0.52,
metalness: 0.06
})
);
node.position.set(x, -4.5, 0.78);
node.castShadow = true;
model.add(node);
cylinderBetween(
new THREE.Vector3(x, -4.5, 0.78),
new THREE.Vector3(1.65, -4.5, 0.78),
0.032,
rodMaterial,
10
);
});
const output = new THREE.Mesh(
new THREE.SphereGeometry(0.39, 24, 16),
new THREE.MeshStandardMaterial({
color: 0xe1ad43,
roughness: 0.42,
metalness: 0.12
})
);
output.position.set(1.65, -4.5, 0.78);
output.castShadow = true;
model.add(output);
const outputRing = new THREE.Mesh(
new THREE.TorusGeometry(0.51, 0.045, 12, 32),
rodMaterial
);
outputRing.position.copy(output.position);
outputRing.castShadow = true;
model.add(outputRing);
const inputs = document.querySelector("#weight-1");
const inputs2 = document.querySelector("#weight-2");
const inputBias = document.querySelector("#bias");
const w1Value = document.querySelector("#weight-1-value");
const w2Value = document.querySelector("#weight-2-value");
const biasValue = document.querySelector("#bias-value");
const equationMain = document.querySelector("#equation-main");
const correctCount = document.querySelector("#correct-count");
const wrongCount = document.querySelector("#wrong-count");
const accuracyText = document.querySelector("#accuracy-text");
const accuracyFill = document.querySelector("#accuracy-fill");
const boundaryLabel = document.querySelector("#boundary-label");
const modelLabel = document.querySelector("#model-label");
const xLabel = document.querySelector("#x-label");
const yLabel = document.querySelector("#y-label");
const clamp = THREE.MathUtils.clamp;
function signed(value, decimals = 2) {
const rounded = Math.abs(value).toFixed(decimals);
if (Math.abs(value) < 0.000001) return "0.00";
return `${value < 0 ? "−" : "+"}${rounded}`;
}
function syncControls() {
inputs.value = w1;
inputs2.value = w2;
inputBias.value = bias;
w1Value.textContent = signed(w1);
w2Value.textContent = signed(w2);
biasValue.textContent = signed(bias);
}
function updateBoundaryGeometry() {
const normalLengthSquared = w1 * w1 + w2 * w2;
if (normalLengthSquared < 0.000001) {
boundaryMesh.visible = false;
normalShaft.visible = false;
normalHead.visible = false;
equationMain.textContent = "0 = 0 · no unique boundary";
return;
}
boundaryMesh.visible = true;
normalShaft.visible = true;
normalHead.visible = true;
const closestPoint = new THREE.Vector3(
(-w1 * bias) / normalLengthSquared,
(-w2 * bias) / normalLengthSquared,
0.255
);
const direction = new THREE.Vector3(w2, -w1, 0).normalize();
boundaryMesh.position.copy(closestPoint);
boundaryMesh.rotation.z = Math.atan2(-w1, w2);
boundaryMesh.scale.set(18, 0.085, 0.065);
const normal = new THREE.Vector3(w1, w2, 0).normalize();
const arrowLength = clamp(Math.sqrt(normalLengthSquared) * 0.9, 0.95, 2.1);
const shaftLength = Math.max(0.2, arrowLength - 0.3);
const start = new THREE.Vector3(0, 0, 0.39);
const end = start.clone().addScaledVector(normal, arrowLength);
normalShaft.scale.set(1, shaftLength, 1);
normalShaft.position.copy(start).addScaledVector(normal, shaftLength * 0.5);
normalShaft.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
normal
);
normalHead.position.copy(end).addScaledVector(normal, -0.15);
normalHead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
normal
);
}
function updateClassification() {
let correct = 0;
let wrong = 0;
points.forEach((point, index) => {
const score = w1 * point.x + w2 * point.y + bias;
const prediction = score >= 0 ? 1 : -1;
const isWrong = prediction !== point.label;
const object = pointObjects[index];
object.score = score;
object.prediction = prediction;
object.wrong = isWrong;
object.ring.visible = isWrong;
object.crosses.forEach(cross => {
cross.visible = isWrong;
});
if (isWrong) {
wrong++;
} else {
correct++;
}
});
const accuracy = correct / points.length;
correctCount.textContent = correct;
wrongCount.textContent = wrong;
accuracyText.textContent = `${(accuracy * 100).toFixed(1)}% accuracy`;
accuracyFill.style.width = `${accuracy * 100}%`;
accuracyFill.style.background = wrong === 0 ? "#34755a" : "#c58c37";
equationMain.textContent =
`${signed(w1)} x₁ ${w2 >= 0 ? "+" : "−"} ${Math.abs(w2).toFixed(2)} x₂ ` +
`${bias >= 0 ? "+" : "−"} ${Math.abs(bias).toFixed(2)} = 0`;
updateBoundaryGeometry();
}
inputs.addEventListener("input", () => {
w1 = Number(inputs.value);
syncControls();
updateClassification();
});
inputs2.addEventListener("input", () => {
w2 = Number(inputs2.value);
syncControls();
updateClassification();
});
inputBias.addEventListener("input", () => {
bias = Number(inputBias.value);
syncControls();
updateClassification();
});
document.querySelector("#reset-weights").addEventListener("click", () => {
w1 = DEFAULT_WEIGHTS.w1;
w2 = DEFAULT_WEIGHTS.w2;
bias = DEFAULT_WEIGHTS.bias;
syncControls();
updateClassification();
});
document.querySelector("#train-button").addEventListener("click", () => {
const learningRate = 0.12;
const order = points.map((_, index) => index);
for (let i = order.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[order[i], order[j]] = [order[j], order[i]];
}
order.forEach(index => {
const point = points[index];
const score = w1 * point.x + w2 * point.y + bias;
const prediction = score >= 0 ? 1 : -1;
if (prediction !== point.label) {
w1 = clamp(w1 + learningRate * point.label * point.x, -2, 2);
w2 = clamp(w2 + learningRate * point.label * point.y, -2, 2);
bias = clamp(bias + learningRate * point.label, -3, 3);
}
});
syncControls();
updateClassification();
});
// Direct camera controls implemented here without importing addons.
const target = new THREE.Vector3(0, -0.35, 0);
let theta = -0.08;
let phi = 1.18;
let zoom = 1;
let dragging = false;
let movedWhileDragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + 21 * sinPhi * Math.sin(theta),
target.y + 21 * Math.cos(phi),
target.z + 21 * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / Math.max(height, 1);
const baseHeight = Math.max(11.3, 14.4 / aspect);
camera.left = (-baseHeight * aspect) / (2 * zoom);
camera.right = (baseHeight * aspect) / (2 * zoom);
camera.top = baseHeight / (2 * zoom);
camera.bottom = -baseHeight / (2 * zoom);
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
movedWhileDragging = false;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
tooltip.classList.remove("visible");
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
if (Math.abs(deltaX) + Math.abs(deltaY) > 1) {
movedWhileDragging = true;
}
theta -= deltaX * 0.005;
phi = clamp(phi + deltaY * 0.005, 0.58, 1.48);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
return;
}
updateHover(event);
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) {
tooltip.classList.remove("visible");
}
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
zoom = clamp(zoom * Math.exp(-event.deltaY * 0.001), 0.68, 1.65);
resize();
},
{ passive: false }
);
document.querySelector("#reset-view").addEventListener("click", () => {
theta = -0.08;
phi = 1.18;
zoom = 1;
updateCamera();
resize();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function updateHover(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(markerMeshes, false);
pointObjects.forEach(object => {
object.material.emissive.setHex(0x000000);
});
if (hits.length === 0) {
tooltip.classList.remove("visible");
return;
}
const index = hits[0].object.userData.pointIndex;
const point = points[index];
const object = pointObjects[index];
object.material.emissive.setHex(object.wrong ? 0x70400d : 0x24343d);
object.material.emissiveIntensity = 0.38;
const trueClass =
point.label === 1
? '<span class="class-a">A / +1</span>'
: '<span class="class-b">B / −1</span>';
const predictedClass =
object.prediction === 1
? '<span class="class-a">A / +1</span>'
: '<span class="class-b">B / −1</span>';
tooltip.innerHTML = `
<div class="tip-title">
${object.wrong ? '<span class="tip-wrong">Misclassified</span>' : 'Correctly classified'}
</div>
<div>x₁ = ${point.x.toFixed(2)}, x₂ = ${point.y.toFixed(2)}</div>
<div>True: ${trueClass}</div>
<div>Predicted: ${predictedClass}</div>
<div>Score: ${signed(object.score)}</div>
`;
tooltip.style.left = `${event.clientX - rect.left + 15}px`;
tooltip.style.top = `${event.clientY - rect.top + 13}px`;
tooltip.classList.add("visible");
}
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
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;
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
x > 5 &&
x < width - 5 &&
y > 5 &&
y < height - 5;
element.style.visibility = visible ? "visible" : "hidden";
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateSceneLabels() {
const normSquared = w1 * w1 + w2 * w2;
if (normSquared > 0.000001) {
const closest = new THREE.Vector3(
(-w1 * bias) / normSquared,
(-w2 * bias) / normSquared,
0.45
);
const tangent = new THREE.Vector3(w2, -w1, 0).normalize();
closest.addScaledVector(tangent, 2.45);
positionLabel(boundaryLabel, closest);
} else {
boundaryLabel.style.visibility = "hidden";
}
positionLabel(modelLabel, new THREE.Vector3(0, -5.18, 0.65));
positionLabel(xLabel, new THREE.Vector3(5.93, -3.72, 0.3));
positionLabel(yLabel, new THREE.Vector3(-5.92, 3.86, 0.3));
}
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
pointObjects.forEach((object, index) => {
if (object.wrong) {
const pulse = 1 + Math.sin(elapsed * 3.2 + index * 0.7) * 0.075;
object.ring.scale.setScalar(pulse);
object.crosses[0].rotation.z = Math.PI * 0.25;
object.crosses[1].rotation.z = -Math.PI * 0.25;
}
});
updateSceneLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
syncControls();
updateClassification();
updateCamera();
resize();
animate();
</script>
</body>
</html>
```