class Net{ Spot [] spot; int wide, high; boolean exclusiveLock = false; Net(int wide, int high){ this.wide = wide; this.high = high; spot = new Spot[wide * high]; for(int i = 0; i < wide; i++){ for(int j = 0; j < high; j++){ int id = i + j * wide; float wUnit = width / wide; float hUnit = height / high; spot[id] = new Spot((wUnit / 2) + i * wUnit, (hUnit / 2) + j * hUnit, id); } } } void draw(){ for(int i = 0; i < wide; i++){ for(int j = 0; j < high; j++){ int id = i + j * wide; if(overCircle(spot[id].x, spot[id].y, spot[id].wide)){ if(mousePressed && !exclusiveLock){ spot[id].locked = true; exclusiveLock = true; } else if (!mousePressed && exclusiveLock){ spot[id].locked = false; exclusiveLock = false; } } //REMOVE ENERGY FROM SYSTEM if (!exclusiveLock){ int idPixel = int(spot[id].x) + int(spot[id].y) * width; if(spot[id].x < width && spot[id].x > -1 && spot[id].y < height && spot[id].y > -1){ if(red(pimage.pixels[idPixel]) < 50 || red(pimage.pixels[idPixel]) > 205){ spot[id].xv *= 0.1; spot[id].yv *= 0.1; } else{ spot[id].xv *= 0.8; spot[id].yv *= 0.8; } } else{ spot[id].xv *= 0.8; spot[id].yv *= 0.8; } } if (spot[id].locked){ spot[id].x = mouseX; spot[id].y = mouseY; if(keyPressed){ switch(key){ case '+': case '=': grav(id, false); break; case '-': case '_': grav(id, true); break; } } fill(255); } else fill(255,0,0); if(drawLinks){ if(i < wide - 1){ line(spot[id].x, spot[id].y, spot[id + 1].x, spot[id + 1].y); } if(j < high - 1){ line(spot[id].x, spot[id].y, spot[id + wide].x, spot[id + wide].y); } } updateSpotSize(i, j); fill(255,0,0); ellipse(spot[id].x, spot[id].y, spot[id].wide * 2, spot[id].wide * 2); fill(0,0,0); String s = nf(spot[id].wide, 2, 1); } } } void reactor(){ pimage.loadPixels(); for(int i = 0; i < wide; i++){ for(int j = 0; j < high; j++){ int id = i + j * wide; if(!spotNear(id, 15.0)){ if(spot[id].x < width && spot[id].x > -1 && spot[id].y < height && spot[id].y > -1){ int idPixel = int(spot[id].x) + int(spot[id].y) * width; if(red(pimage.pixels[idPixel]) < 50){ grav(id, true); } else if(red(pimage.pixels[idPixel]) > 205){ grav(id, false); } } } } } } void updateSpotSize(int i, int j){ int id = i + j * wide; int connections = 0; float leastDistance = 100000.0; for(int k = -1; k < 2; k++){ for(int m = -1; m < 2; m++){ if(i + k > -1 && i + k < wide && j + m > -1 && j + m < high){ int id2 = (i + k) + (j + m) * wide; if(id != id2){ float distance = dist(spot[id2].x, spot[id2].y, spot[id].x, spot[id].y); if(leastDistance > distance){ leastDistance = distance; } } } } } spot[id].wide = 2 + leastDistance / 3; } boolean spotNear(int id, float distance){ for(int i = 0; i < wide * high; i++){ if( i != id){ float spotDistance = dist(spot[id].x, spot[id].y, spot[i].x, spot[i].y); if(spotDistance < distance){ return true; } } } return false; } void grav(int id, boolean implode){ for(int i = 0; i < wide; i++){ for(int j = 0; j < high; j++){ int id2 = i + j * wide; if(id != id2){ float m1 = 5; float m2 = 5; float distX = spot[id].x - spot[id2].x; float distY = spot[id].y - spot[id2].y; float distance = sqrt(sq(distX) + sq(distY)); if (distance < 5){ distance = 5; } float grav = (m1 * m2) / pow(distance * 50, 1.1) * 5; float xGrav = grav * (distX/distance); float yGrav = grav * (distY/distance); float xAccel = xGrav / m1; float yAccel = yGrav / m1; if(implode){ spot[id2].xv += xAccel; spot[id2].yv += yAccel; } else { spot[id2].xv -= xAccel; spot[id2].yv -= yAccel; } spot[id2].x += spot[id2].xv; spot[id2].y += spot[id2].yv; } } } } } static class Spot{ float x, y, xv, yv, wide; int id; boolean locked; Spot(float x, float y, int id){ this.x = x; this.y = y; this.id = id; locked = false; xv = 0.0; yv = 0.0; wide = 15; } } boolean overCircle(float x, float y, float diameter) { float disX = x - mouseX; float disY = y - mouseY; if(sqrt(sq(disX) + sq(disY)) < diameter) { return true; } else { return false; } }