comments
This commit is contained in:
parent
72a247f20f
commit
28795d481d
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue