This commit is contained in:
Peter McGoron 2023-10-11 13:03:47 +00:00
parent 72a247f20f
commit 28795d481d
1 changed files with 35 additions and 7 deletions

View File

@ -26,15 +26,38 @@
* *
*/ */
let width = 800; /*****************************************
let height = 600; * Math
let longestDiag = Math.sqrt(width**2 + height**2); ****************************************/
function sign(x) { function sign(x) {
if (x < 0) return -1; if (x < 0) return -1;
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 */ /* Basic infinite line class, useful for physics simulation */
class InfiniteLine { class InfiniteLine {
constructor(x_0, y_0, angle) { 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 { class SimulationMouseHandler {
constructor() { constructor() {
this.curWave = null; this.curWave = null;
@ -113,21 +132,26 @@ class SimulationMouseHandler {
handleMouse() { handleMouse() {
if (mouseIsPressed && !this.mouseDown) { if (mouseIsPressed && !this.mouseDown) {
/* On mouseDown, make new plane wave and allow the user to edit it */
this.mouseDown = true; this.mouseDown = true;
this.curWave = new PlaneWave(mouseX, mouseY, this.oldAngle, 0.1); this.curWave = new PlaneWave(mouseX, mouseY, this.oldAngle, 0.1);
} else if (!mouseIsPressed) { } else if (!mouseIsPressed) {
/* If mouse is released, return current wave if it exists */
this.mouseDown = false; this.mouseDown = false;
const curWave = this.curWave; const curWave = this.curWave;
this.curWave = null; this.curWave = null;
return curWave; return curWave;
} }
/* Change angle to where the mouse cursor is pointing */
if (!veryClose(mouseX, this.curWave.x_0) || if (!veryClose(mouseX, this.curWave.x_0) ||
!veryClose(mouseY, this.curWave.y_0)) { !veryClose(mouseY, this.curWave.y_0)) {
this.oldAngle = Math.atan2(mouseY - this.curWave.y_0, mouseX - this.curWave.x_0); this.oldAngle = Math.atan2(mouseY - this.curWave.y_0, mouseX - this.curWave.x_0);
this.curWave.angle = this.oldAngle; this.curWave.angle = this.oldAngle;
} }
/* TODO: make faster plane waves? */
this.curWave.draw(); this.curWave.draw();
this.curWave.drawArrow(); this.curWave.drawArrow();
} }
@ -154,6 +178,10 @@ class Simulation {
} }
} }
/***************************
* Simulation wrapper
**************************/
var sim; var sim;
function setup() { function setup() {
frameRate(24); frameRate(24);