// set up the global variables int fieldsize = 400; // this is just the screen size - MUST BE AN EVEN NUMBER float mag = 0.5; // magnification factor - < 1 to shrink int[] originalsketch; float[] newsketch = new float [1000]; float[] transformedsketch = new float [1000]; int iterationcounter = 0; int stopflag = 0; int counter = 0; int drawingstopflag = 0; int xorigin = 670; int yorigin = 280; float picwidth = 0; float picheight = 0; // values will be assigned in the mag finding routine float yb = 1; // linear term for y=py + qyx + ryx^2 float ym = 0.0; // term for x float ye = 0.; // term for exp(x) float ys = 0; // term for sin float yc = 0; // term for cos float yr = 0.00; // term for y float dyb = 0; // just initializing -these are display copies for rounding float dym = 0; float dye = 0; float dys = 0; float dyc = 0; float dyr = 0; int targetparam = 0; // used to keep track of which param is being altered. void setup() { size(fieldsize* 2,fieldsize); // set the size of the viewing window PFont f; f = loadFont("ArialMT-16.vlw"); textFont(f,16); fill(0); text("JAD 8 Jun 2014",(fieldsize * 2 - 120),(fieldsize - 18)); String[] sketch = loadStrings("Fish.txt"); originalsketch = int(split(sketch[0],',')); transformcoordinates(); copytonewsketch(); workoutnewmag(); } void draw() { iterationcounter = iterationcounter+1; // println ("iteration " + iterationcounter); adddistortion(); showonscreen(); text("Y growth is set by this equation:",(fieldsize +70),28); text("b + mx + e.exp(x) + s.sin(x) + c.cos(x) + ry",(fieldsize + 70),60); text("For the linear and exponential function, x is", (fieldsize + 70), 90); text("normalized to the range 0-1, for the harmonic", (fieldsize + 70), 110); text("functions it is normalized to the range 0-pi/2.", (fieldsize + 70), 130); text("CURRENT VALUES:", fieldsize + 70, 170); dyb = fixDec(yb,2); // fixDec is a function defined below somewhere dym = fixDec(ym,2); dye = fixDec(ye,2); dys = fixDec(ys,2); dyc = fixDec(yc,2); dyr = fixDec(yr,4); text("down b = " + dyb + " up", fieldsize + 140, 200); text("down m = " + dym + " up", fieldsize + 140, 230); text("down e = " + dye + " up", fieldsize + 140, 260); text("down s = " + dys + " up", fieldsize + 140, 290); text("down c = " + dyc + " up", fieldsize + 140, 320); text("down r = " + dyr + " up", fieldsize + 140, 350); rectMode (CORNER); fill(128); rect((fieldsize +120),187,16,16); rect((fieldsize +120),217,16,16); rect((fieldsize +120),247,16,16); rect((fieldsize +120),277,16,16); rect((fieldsize +120),307,16,16); rect((fieldsize +120),337,16,16); rect((fieldsize +300),187,16,16); rect((fieldsize +300),217,16,16); rect((fieldsize +300),247,16,16); rect((fieldsize +300),277,16,16); rect((fieldsize +300),307,16,16); rect((fieldsize +300),337,16,16); text("JAD 8 Jun 2014",(fieldsize * 2 - 120),(fieldsize - 18)); } // end draw routine (ie loop back) float fixDec(float n, int d) { return Float.parseFloat(String.format("%." + d + "f", n)); } void adddistortion() { //println ("entering add distortion routine"); float x = 0; float y = 0; float normalizedx = x/picwidth; stopflag = 0; counter = 0; while (stopflag == 0) { x = transformedsketch[counter]; normalizedx = x/picwidth; y = transformedsketch[counter+1]; newsketch[counter] = x; if (y < 888888) { y = y * yb + ym * normalizedx * y + ye * exp(normalizedx) * y + ys * sin(normalizedx * PI/2)* y + yc * cos(normalizedx * PI/2)* y + yr*y*y; // *** this is the function newsketch[counter+1] = y; } if (y > 888887) { newsketch[counter+1] = y; } if (y > 888887 && y < 900000){ stopflag = 1; } counter = counter + 2; } // end the while loop } void showonscreen() { workoutnewmag(); drawingstopflag = 0; counter = 0; float startx = 0; float starty = 0; float endx = 0; float endy = 0; while (drawingstopflag == 0) { startx = newsketch[counter]; counter = counter + 1; starty = newsketch[counter] * -1; counter = counter + 1; endx = newsketch[counter]; endy = newsketch[counter+1] * -1; if (startx < 999999 && endx < 888888) { //not at a line end line(int(mag*startx + 100), int(mag*starty+ fieldsize/2), int(mag*endx + 100), int(mag * endy + fieldsize/2)); } if (endx == 888888) { drawingstopflag = 1; } } } void workoutnewmag() { counter = 0; float maxx = 0; float maxy = 0; float minx = 999999; float miny = 999999; stopflag = 0; while (stopflag == 0) { if (newsketch[counter] > maxx && newsketch[counter] < 888888) { maxx = newsketch[counter]; } if (newsketch[counter] < minx) { minx = newsketch[counter]; } if (newsketch[counter] == 888888) { stopflag = 1; } if (newsketch[counter+1] > maxy && newsketch[counter+1] < 888888) { maxy = newsketch[counter+1]; } if (newsketch[counter+1] < miny){ miny = newsketch[counter+1]; } counter = counter + 2; }// end the while loop picwidth = maxx - minx; picheight = maxy - miny; mag = fieldsize / (picwidth * 4); // if (fieldsize / (picheight * 2) < mag) { //mag = fieldsize/ ((maxy-miny)+20); //} } // end of subroutine void transformcoordinates() { println ("Transforming coordinates"); counter = 0; stopflag = 0; int x = 0; int y = 0; int newx = 0; int newy = 0; while (stopflag == 0) { x = originalsketch[counter]; y = originalsketch[counter+1]; newx = xorigin - x; if (x > 9999) { // ie if it is a control value not a data one newx = x; } newy = yorigin-y; if (y > 9999) { // ie if it is a control value not a data one newy = y; } transformedsketch[counter] = newx; transformedsketch[counter+1] = newy; if (x == 888888) { stopflag = 1; } counter = counter + 2; } // end while loop } void copytonewsketch() { println ("Copy to newsketch entered"); counter = 0; stopflag = 0; while (stopflag == 0) { newsketch[counter] = transformedsketch[counter]; newsketch[counter+1] = transformedsketch[counter+1]; if (transformedsketch[counter] == 888888) { stopflag = 1; } counter = counter +2; } println ("leaving copytonewskectch"); } void mousePressed() { println("mouse!"); int X = mouseX; int Y = mouseY; //println (X + " " + Y); if (X > fieldsize + 119 && X < fieldsize + 137 && Y > 186 && Y < 204){ // b down println("mouse b down"); if (yb > 0.2){ yb = yb - 0.2; } } if (X > fieldsize + 299 && X < fieldsize + 317 && Y > 186 && Y < 204){ // b up if (yb < 8){ yb = yb + 0.2; } } if (X > fieldsize + 119 && X < fieldsize + 137 && Y > 216 && Y < 234){ // m down if (ym >= 0.2){ ym = ym - 0.2; } } if (X > fieldsize +299 && X < fieldsize +317 && Y > 216 && Y < 234){ // m up if (ym < 10){ ym = ym + 0.2; } } if (X > fieldsize +119 && X < fieldsize +137 && Y > 246 && Y < 264){ // e down if (ye >= 0.1){ ye = ye - 0.1; } } if (X > fieldsize +299 && X < fieldsize +317 && Y > 246 && Y < 264){ // e up if (ye < 5) { ye = ye + 0.1; } } if (X > fieldsize +119 && X < fieldsize +137 && Y > 276 && Y < 294){ // s down if (ys >= 0.5){ ys = ys - 0.5; } } if (X > fieldsize +299 && X < fieldsize +317 && Y > 276 && Y < 294){ // s up if (ys < 5) { ys = ys + 0.5; } } if (X > fieldsize +119 && X < fieldsize +137 && Y > 306 && Y < 324){ // c down if (yc >= 0.5){ yc = yc - 0.5; } } if (X > fieldsize +299 && X < fieldsize +317 && Y > 306 && Y < 324){ // c up if (yc < 5) { yc = yc + 0.5; } } if (X > fieldsize +119 && X < fieldsize +137 && Y > 336 && Y < 354){ // r down if (yr >= -0.003){ yr = yr - 0.001; } } if (X > fieldsize +299 && X < fieldsize +317 && Y > 336 && Y < 354){ // c up if (yr < 0.005) { yr = yr + 0.0005; } } background(230); } // end mousepressed