/** * sinusSum01 * * * Matthew Conroy ( www.matthewconroy.com ) **/ HScrollbar hs1, hs2, hs3, hs4,hs5; int windowWidth=600; float ttime=0.0; float ttimeInc=0.02; float[] Phase = new float[10]; float[] plotY = new float[600]; int halfWidth=0; int goodX=0; void setup() { size(windowWidth, 600); halfWidth=int(width*0.5); noStroke(); hs1 = new HScrollbar(0, height-37, width, 8, 3*5+1,int(random(100))); hs2 = new HScrollbar(0, height-29, width, 8, 3*5+1,int(random(100))); hs3 = new HScrollbar(0, height-21, width, 8, 3*5+1,int(random(100))); hs4 = new HScrollbar(0, height-13, width, 8, 3*5+1,int(random(100))); hs5 = new HScrollbar(0, height-5, width, 8, 3*5+1,int(random(100))); PFont fontA = loadFont("CourierNew36.vlw"); textFont(fontA, 12); smooth(); } void draw() { rectMode(CORNERS); background(255); Phase[5] = TWO_PI*(hs1.getPos()/width); Phase[4] = TWO_PI*(hs2.getPos()/width); Phase[3] = TWO_PI*(hs3.getPos()/width); Phase[2] = TWO_PI*(hs4.getPos()/width); Phase[1] = TWO_PI*(hs5.getPos()/width); //println(Phase[1]+" "+Phase[2]+" "+Phase[3]+" "+Phase[4]); int interRectGap=3; //draw rectangles strokeWeight(1); stroke(0); fill(255); float currHeight=height*0.9; for(int i = 1; i<=5; i++) { float rectHeight = height*(0.1+0.05*sin(Phase[i]+ttime)); float rectWidth=width*0.1; rect(width*0.5-0.5*rectWidth,currHeight,width*0.5+0.5*rectWidth,currHeight-rectHeight); //rect(width*0.5-20,currHeight,width*0.5+20,currHeight-rectHeight); //fill(255,0,0); //rect(100,100,200,200); //rect(150,150,200,200); currHeight -= rectHeight; currHeight -= interRectGap; } // update the plot array for (int i=0; ihalfWidth-goodX) { line(i-1,plotY[i-1],i,plotY[i]); } } // update the handles stroke(128); hs1.update(); hs2.update(); hs3.update(); hs4.update(); hs5.update(); hs1.draw(); hs2.draw(); hs3.draw(); hs4.draw(); hs5.draw(); //words fill(0); //text("a="+int(100*a)/100.,10,height-100); //text("b="+int(100*b)/100.,10+width*0.25,height-100); //text("b="+int(100*mm)/100.,10+width*0.5,height-60); //text("n="+n,10+width*0.75,height-60); // lasta=a; // lastb=b; // lastm=m; //} // end of if things have changed //println(hs1.getPos()); //increment time ttime +=ttimeInc; ++goodX; } class HScrollbar { int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float ratio; HScrollbar (int xp, int yp, int sw, int sh, int l, int sposition) { swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; // note the width and height of the slider are both sheight spos = xpos+sheight/2.+sposition/100.*(swidth-sheight)-sheight/2.; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } } int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void draw() { rectMode(CORNER); fill(255); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(128); } else { fill(0); } rect(spos, ypos, sheight, sheight); } float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; } }