/** * Taylor 1 * * Graphs of functions together with their Taylor polynomials. * * Matthew Conroy ( jumbo (at) madandmoonly com (you know where the dot goes)) **/ HScrollbar hs1, hs2, hs3, hs4; PImage top, bottom; // Two image to load int topWidth, bottomWidth; // The width of the top and bottom images float lasta, lastb; // to keep track of whether things have changed int lastm; float[] coeffs = new float[30]; //array for the coefficients of the taylor polynomials void setup() { size(700, 560); noStroke(); //hs1 = new HScrollbar(0, height-80, width, 10, 3*5+1); //hs2 = new HScrollbar(0, height-60, width, 10, 3*5+1); hs3 = new HScrollbar(0, height-40, width, 10, 3*5+1,50); hs4 = new HScrollbar(0, height-20, width, 10, 3*5+1,0); PFont fontA = loadFont("CourierNew36.vlw"); textFont(fontA, 18); } void draw() { background(255); // Get the position of the top scrollbar // and convert to a value to display the top image //float Pos1 = hs1.getPos(); //float Pos2 = hs2.getPos(); float Pos3 = hs3.getPos(); float Pos4 = hs4.getPos(); float xx=0; float yy=0; float xxLast=0,yyLast=0,xx0=0,yy0=0; float scale=30.0; // float a=0.+1.0*Pos1/width; // float b=(Pos2/width-1./2.)*2*3.1415926535; int m = int(Pos3); int n = 1+int(Pos4/50); smooth(); strokeWeight(1); //draw axes stroke(33); line(0,height/2,width,height/2); line(width/2,0,width/2,height); //draw sine curve int numPoints=1000; for(int i = 0; i0) { line(xx,yy,xxLast,yyLast); } else { xx0=xx; yy0=yy; } xxLast=xx; yyLast=yy; } // calculate taylor polynomial int i,j,i4=0, j4=0, fact=1; float xcalc=0.0, blah=1; float mm=(m-width/2)/scale; for(i = 0; i<=n; ++i) { i4=i % 4; if (i4==0) { coeffs[i] = sin(mm); } if (i4==1) { coeffs[i] = cos(mm); } if (i4==2) { coeffs[i] = -sin(mm); } if (i4==3) { coeffs[i] = -cos(mm); } } // draw taylor polynomial float ydiff=0; for(i = 0; i0) { line(xx,yy,xxLast,yyLast); } else { xx0=xx; yy0=yy; } xxLast=xx; yyLast=yy; } //draw dot at center stroke(0); ellipse(m,height/2.-scale*sin((m-width/2.)/scale),4,4); //updatePixels(); stroke(128); //hs1.update(); //hs2.update(); hs3.update(); hs4.update(); //hs1.draw(); //hs2.draw(); hs3.draw(); hs4.draw(); //words //text("a="+int(a*100)/100.+" b="+int(b*10)/10.+" m="+z1Fact+" n="+z2Fact,10,height-100); 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()); } 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() { 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; } }