// ProfileFx // receives pos from Arduino // // // int F[],x[i], shown as height F[x] "profile" // then as slope of "terrain" dT = F[x] // constrains x[i] sq(F[i]+100-mouseY)+sq(x[i]-mouseX)){ ibest = i; dist = sq(F[i]+100-mouseY)+sq(x[i]-mouseX); } } ellipse(x[ibest],F[ibest]+100,20,20); text (Integer.toString(ibest), mouseX+10, mouseY-12); text (Integer.toString(F[ibest]), mouseX+10, mouseY); text (Integer.toString(x[ibest]), mouseX+10, mouseY+12); if (mousePressed == true){ //now use ibest to move to mouseX, mouseY x[ibest] = min(x[ibest+1],max(x[ibest-1],mouseX)); F[ibest] = mouseY - 100; if (mouseY < 0) F[ibest] = -100; if (mouseY > 200) F[ibest] = 100; } //plot "terrain" assuming 80 point separation of F[]'s pushMatrix(); translate(0,height/2); top = 0; FF = F[0]; for (i = 0; i < n; i = i+1) { s = x[i+1]-x[i]+1; // increment used for plotting inc = 0.01*(F[i+1]-F[i])/s; for (j = 0; j < s; j = j+1) { FF = FF + inc; top = top + FF; // j increments of inc = F diff. line(x[i]+j,height,x[i]+j,top); } } popMatrix(); //receive pos from Arduino while (myPort.available() > 0) { myString = myPort.readStringUntil(lf); if (myString != null) { myString = trim(myString); pos = int(myString); } } // draw pos cursor stroke(150); line(pos,200,pos,800); } void keyPressed() { if (key == CODED) { if (keyCode == UP) --F[ibest]; if (keyCode == DOWN) ++F[ibest]; if (keyCode == RIGHT) ++x[ibest]; if (keyCode == LEFT) --x[ibest]; } }