From 28795d481d33900f14bfc433d182dffc651abc6f Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Wed, 11 Oct 2023 13:03:47 +0000 Subject: [PATCH] comments --- scattering.js | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/scattering.js b/scattering.js index 8730be1..0caae35 100644 --- a/scattering.js +++ b/scattering.js @@ -26,15 +26,38 @@ * */ -let width = 800; -let height = 600; -let longestDiag = Math.sqrt(width**2 + height**2); +/***************************************** + * Math + ****************************************/ function sign(x) { if (x < 0) return -1; if (x >= 0) return 1; } +function distance(x,y) { + return Math.sqrt(x**2 + y**2); +} + +function veryClose(x, y) { + return Math.abs(x - y) < 0.001; +} + +/***************************************** + * Parameters + ****************************************/ + +let width = 800; +let height = 600; + +/* Infinite lines are drawn with length = 2*longestDiag which will always + * cross the entire screen. */ +let longestDiag = distance(width, height); + +/***************************************** + * Object Classes + ****************************************/ + /* Basic infinite line class, useful for physics simulation */ class InfiniteLine { constructor(x_0, y_0, angle) { @@ -100,10 +123,6 @@ class PlaneWave extends InfiniteLine { } } -function veryClose(x, y) { - return Math.abs(x - y) < 0.001; -} - class SimulationMouseHandler { constructor() { this.curWave = null; @@ -113,21 +132,26 @@ class SimulationMouseHandler { handleMouse() { if (mouseIsPressed && !this.mouseDown) { + /* On mouseDown, make new plane wave and allow the user to edit it */ this.mouseDown = true; this.curWave = new PlaneWave(mouseX, mouseY, this.oldAngle, 0.1); } else if (!mouseIsPressed) { + /* If mouse is released, return current wave if it exists */ this.mouseDown = false; const curWave = this.curWave; this.curWave = null; return curWave; } + /* Change angle to where the mouse cursor is pointing */ if (!veryClose(mouseX, this.curWave.x_0) || !veryClose(mouseY, this.curWave.y_0)) { this.oldAngle = Math.atan2(mouseY - this.curWave.y_0, mouseX - this.curWave.x_0); this.curWave.angle = this.oldAngle; } + /* TODO: make faster plane waves? */ + this.curWave.draw(); this.curWave.drawArrow(); } @@ -154,6 +178,10 @@ class Simulation { } } +/*************************** + * Simulation wrapper + **************************/ + var sim; function setup() { frameRate(24);