import controlP5.*;
ArrayList grid = new ArrayList();
ArrayList flowpts = new ArrayList();
int num = 8;
int[] lpts = new int[num];
int[] dia = new int[num];
int[] pts = new int[num];
float mx = 0;
float my = 0;
float sx = 99999;
float sy = 99999;
PImage img;
int xsize = 70;
int ysize = 35;
int space = 20;
void setup() {
size(1300, 680);
background(0);
smooth(4);
img = loadImage("heat.jpg");
for(int i = 0; i < xsize; ++i){
for(int j = 0; j < ysize; j++){
float xx = i*space + (width/2 - xsize*space/2);
float yy = j*space + (height/2 - ysize*space/2);
float zz = 0;
grid.add(new PVector(xx,yy,zz));
flowpts.add(new PVector(xx,yy,zz));
if(xx > mx){
mx = xx;
}
if(yy > my){
my = yy;
}
if(xx < sx){
sx = xx;
}
if(yy < sy){
sy = yy;
}
}
}
for(int i = 0; i < num; i++){
lpts[i] = int(random(grid.size()-1));
dia[i] = int(random(50)+18);
pts[i] = 18;
}
//noLoop();
fill(30);
rect(0,0,width,height);
}
void draw() {
println(frameCount);
//background(0);
fill(0,0,0,5);
noStroke();
rect(0,0,width,height);
//image(img, 30, 30);
for(int i = 0; i < (grid.size()); ++i){
PVector vs = grid.get(i);
noStroke();
fill(100);
int rad = 5;
for(int j = 0; j < num; j++){
if(lpts[j] == i){
fill(0,102,153);
rad = 20;
}
}
ellipse(vs.x,vs.y,rad,rad);
}
////////////weighted average/////////
////////////get low and high distance//////////////
PVector vs0 = flowpts.get(0);
PVector vsn = grid.get(lpts[0]);
float l = 1/dist(vs0.x,vs0.y,vsn.x,vsn.y);
float h = 1/dist(vs0.x,vs0.y,vsn.x,vsn.y);
for(int i = 0; i < flowpts.size(); i++){
for(int ac = 0; ac < num; ac++){
vsn = grid.get(lpts[ac]);
float d = dist(vs0.x,vs0.y,vsn.x,vsn.y);
if(d < l){
l = d;
}
if(d > h){
h = d;
}
}
}
for(int i = 0; i < flowpts.size(); i++){
PVector vs = flowpts.get(i);
float dv = 0;
float tx = 0;
float ty = 0;
float dd = 99999;
for(int ac = 0; ac < num; ac++){
if(ac < num){
vsn = grid.get(lpts[ac]);
}else{
vsn = (new PVector(mouseX,mouseY,0));
}
float nd = dist(vs.x,vs.y,vsn.x,vsn.y);
if(nd < dd){
dd = nd;
}
float d = 1/dist(vs.x,vs.y,vsn.x,vsn.y);
float rd = map(d,l,h,0.0,1.0);
tx = tx + vsn.x*rd*rd;
ty = ty + vsn.y*rd*rd;
dv = dv + rd*rd;
}
tx = tx/dv;
ty = ty/dv;
float na = -atan2(tx-vs.x, ty-vs.y);
stroke(0,255,255);
//line(vs.x,vs.y,tx,ty);
color c = img.get(int(dd),2);
stroke(c);
int ww = int(7-dd/22);
if(ww < 1){
ww = 1;
}
strokeWeight(ww);
if(vs.x > mx || vs.x < sx){
stroke(25);
}
if(vs.y > my || vs.y < sy){
stroke(25);
}
point(vs.x,vs.y);
float nx = vs.x;
float ny = vs.y;
nx = nx + 1 * cos(na);
ny = ny + 1 *sin(na);
flowpts.set(i,new PVector(nx,ny,0));
pushMatrix();
translate(vs.x,vs.y);
rotate(na);
stroke(255);
//println(na);
//line(-20,0,20,0);
popMatrix();
}
if(frameCount%600 == 0){
flowpts = new ArrayList();
for(int i = 0; i < (grid.size()); ++i){
PVector vs = grid.get(i);
flowpts.add(new PVector(vs.x,vs.y,0));
}
}
}
void mouseClicked() {
for(int i = 0; i < (grid.size()); ++i){
PVector vs = grid.get(i);
if(dist(mouseX,mouseY,vs.x,vs.y) < 9){
fill(255,0,0);
int[] npts = new int[num];
for(int j = 1; j < num; j++){
npts[j] = lpts[j-1];
}
lpts = npts;
lpts[0] = i;
}
}
}