aboutsummaryrefslogtreecommitdiff
path: root/src/physics/test4.ts
diff options
context:
space:
mode:
authorLalleSX2026-10-24 14:18:00 +0200
committerLalleSX2026-10-24 14:18:00 +0200
commit389fb3a592832092835da0c2cc1608a57ba5f629 (patch)
treead1f89333ec3c84b906fb6546cee7824f58236fa /src/physics/test4.ts
parentinit (diff)
download3D-Electrophysics-389fb3a592832092835da0c2cc1608a57ba5f629.tar.gz
3D-Electrophysics-389fb3a592832092835da0c2cc1608a57ba5f629.zip
Add tests for math rendering and interactive scenes; update navigation and viewport handling
- Updated `lab.spec.ts` to navigate directly to the field lesson. - Created `math.spec.ts` with tests for math rendering, error checking, and responsive design. - Added `test4.spec.ts` for interactive scenes, ensuring all questions are functional and accessible. - Modified `vite.config.ts` to keep original question/answer links navigable in production.
Diffstat (limited to 'src/physics/test4.ts')
-rw-r--r--src/physics/test4.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/physics/test4.ts b/src/physics/test4.ts
new file mode 100644
index 0000000..a4d4be5
--- /dev/null
+++ b/src/physics/test4.ts
@@ -0,0 +1,36 @@
+import { COULOMB_CONSTANT, type Charge } from './electricField';
+
+export function uniformWork(q: number, field: number, distance: number, angle: number): number {
+ return q * field * distance * Math.cos(angle);
+}
+
+export function centralWork(q: number, q0: number, ra: number, rb: number): number {
+ if (!Number.isFinite(ra) || ra <= 0 || rb <= 0 || Number.isNaN(rb)) throw new RangeError('Distances must be positive; only the final distance may be infinity.');
+ return COULOMB_CONSTANT * q * q0 * (1 / ra - 1 / rb);
+}
+
+export function energyTransfer(initialPotential: number, potential: number, initialKinetic: number, mass: number) {
+ const kinetic = initialKinetic + initialPotential - potential;
+ if (kinetic < -1e-20 || mass <= 0) throw new RangeError('This position is energetically inaccessible or the mass is invalid.');
+ return { kinetic: Math.max(0, kinetic), total: initialKinetic + initialPotential, speed: Math.sqrt(2 * Math.max(0, kinetic) / mass) };
+}
+
+export function assembledEnergy(charges: readonly Charge[]): number {
+ let energy = 0;
+ for (let i = 0; i < charges.length; i++) for (let j = i + 1; j < charges.length; j++) {
+ const a = charges[i], b = charges[j];
+ const distance = Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z);
+ if (distance === 0) throw new RangeError('Point charges must have distinct positions.');
+ energy += COULOMB_CONSTANT * a.coulombs * b.coulombs / distance;
+ }
+ return energy;
+}
+
+/** Dipole in the xy plane, E along +x; positive theta is counterclockwise. */
+export function dipoleState(moment: number, field: number, theta: number, thetaA: number) {
+ return {
+ energy: -moment * field * Math.cos(theta),
+ torqueZ: -moment * field * Math.sin(theta),
+ work: moment * field * (Math.cos(theta) - Math.cos(thetaA)),
+ };
+}