> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.eGdxWbek1EM/rev.1293
 * 
 * authors: 
 *   
 *   Don Spaulding

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "Untitled Sketch", created by Don
// http://sketchpad.cc/sp/pad/view/ro.R1MVswihJl6/rev.561



// Displays the front of a sound-wave originating
// at the (X,Y) coordinates of each mouse click.
// Sensors detect when the sound wave has reached
// them and record a timestamp when it happens.

Sensor[] sensors;
SoundWave wave;
int num_hit;

class SoundWave {
    int epicenterX, epicenterY;
    int radius;
    int speed;

    SoundWave(int posX, int posY){
        epicenterX = posX;
        epicenterY = posY;
        radius = 0;
        speed = 1;
    }
    
    void move(){
        radius += speed;
    }

    void stop(){
        speed = 0;
    }
    
    void draw(){
        stroke(0xFFFFFFFF);
        strokeWeight(2);
        ellipse(epicenterX, epicenterY, radius, radius);    
    }

}

class Sensor {
    int x,y;
    int size = 2;
    float hit_time;
    
    Sensor(int posX, int posY){
        x = posX;
        y = posY;
        hit_time = 0;
    }
    
    void draw(){
        stroke(0xFFFFFFFF);
        strokeWeight(1);
        if (is_hit()){
            stroke(0xFFFF0000);
        }
        ellipse(x,y,size,size);
    }
    
    boolean detect_hit(){
        // This function only exists in the simulation.  In meatspace, we don't know the radius
        // of the soundwave or its epicenter (the whole point is to find the epicenter), but also
        // in meatspace, this detection will be provided by a microphone on an Arduino input pin.
        float distance = dist(x,y,wave.epicenterX,wave.epicenterY);
        text("Distance: "+distance, 20, 50);
        text("Radius: "+wave.radius, 20, 65);
        if ( distance <= wave.radius){
            hit_time = millis();
        }
        return is_hit(); 
    }
        
    boolean is_hit(){
        if (hit_time==0){
            return false;
        }
        return true;
    }
}

void mousePressed(){
    for (int i=0;i<sensors.length;i++) {
        if (sensors[i]){
            sensors[i].hit_time = 0;
        }
    }
    wave = new SoundWave(mouseX, mouseY);
}

void setup(){
    frameRate(60);
    size(600,600);
    ellipseMode(RADIUS);
    sensors = new Sensor[4];
    sensors[0] = new Sensor(130, 150);
    sensors[1] = new Sensor(230, 150);
    sensors[2] = new Sensor(130, 250);
    sensors[3] = new Sensor(230, 250);
    wave = new SoundWave(100, 100);
}

void draw(){
    background(0x666666);
    noFill();
    
    // move and redraw the front
    wave.move();
    wave.draw();
    
    // detect hits/redraw sensor
    num_hit = 0;
    num_active = 0;
    for (int i=0; i<sensors.length; i++){
        Sensor sensor = sensors[i];
        if (sensor) {
            num_active++;
            if (sensor.detect_hit()){
                num_hit++;
            }
            sensor.draw();
        }
    }
    if (num_hit==num_active){
        wave.stop();
        stroke(0xFFFF0000);
        ellipse(wave.epicenterX, wave.epicenterY, wave.radius, wave.radius);
        // calculate_position();
    }
    
    // draw the labels
    text("epicenterX: "+wave.epicenterX, 20, 20);
    text("epicenterY: "+wave.epicenterY, 20, 35);
    text("mouseX: "+mouseX, 20, 80);
    text("mouseY: "+mouseY, 20, 95);
}