/** * norman01 * * For messing around with a norman window optimization problem * * Matthew Conroy ( conroy atatatat math dot washington dot edu ) **/ HScrollbar hs1, hs2; //, hs3, hs4, hs5, hs6, hs7, hs8; void setup() { size(700, 560); noStroke(); hs1 = new HScrollbar(0, height-70, width-1, 10, 3*5+1,50); hs2 = new HScrollbar(0, height-20, width-1, 10, 3*5+1,50); //hs3 = new HScrollbar(0, height-60, width, 10, 3*5+1,50); //hs4 = new HScrollbar(0, height-50, width, 10, 3*5+1,50); //hs5 = new HScrollbar(0, height-40, width, 10, 3*5+1,50); //hs6 = new HScrollbar(0, height-30, width, 10, 3*5+1,50); //hs7 = new HScrollbar(0, height-20, width, 10, 3*5+1,50); //hs8 = new HScrollbar(0, height-10, width, 10, 3*5+1,50); PFont fontA = loadFont("CourierNew36.vlw"); textFont(fontA, 18); } void draw() { background(255); float Pos1 = hs1.getPos(); float Pos2 = hs2.getPos(); float scalee=25.0; float perim = 1+int(Pos1/width*26); float radius = (Pos2/width*perim/(PI+2)); smooth(); strokeWeight(3); stroke(0); noFill(); float centerx=width/2.0; float centery=height/4.0; arc(centerx,centery,2*radius*scalee,2*radius*scalee,PI,2*PI); float h = (perim-(PI+2)*radius)/2.0; line(centerx-radius*scalee,centery,centerx-radius*scalee,centery+h*scalee); line(centerx+radius*scalee,centery,centerx+radius*scalee,centery+h*scalee); line(centerx-radius*scalee,centery+h*scalee,centerx+radius*scalee,centery+h*scalee); strokeWeight(1); hs1.update(); hs2.update(); hs1.draw(); hs2.draw(); //words fill(0); text("perimeter="+int(1000*perim)/1000.,10,height-80); text("radius="+int(1000*radius)/1000.,10,height-30); float area = 0.5*PI*radius*radius+2*h*radius; text("area="+int(1000*area)/1000.,10,height/2.0); } 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; } }