PFont fontA; int drawtype = 3; boolean sampleon = false; int[] simplesample = {1,23}; int[] stratasample = {0,1,0,2,2,3,0,4,3,4}; int[] clustersample = {0,1}; int samplecall = 0; void setup() { size(400,500); smooth(); fontA = loadFont("ArialMT-48.vlw"); textFont(fontA, 24); ellipseMode(CORNER); } void draw() { float mx, my; color button1, button2, button3, button4; background(200); strokeWeight(3); mx = mouseX; my = mouseY; button1 = color(255,255,255); button2 = color(255,255,255); button3 = color(255,255,255); button4 = color(255,255,255); if ((mx >= 30) && (mx <= 130) && (my >=30) && (my <= 80)) { button1 = color(0,0,255); if (mousePressed) drawtype = 1; } if ((mx >= 150) && (mx <= 250) && (my >= 30) && (my <= 80)) { button2 = color(0,0,255); if (mousePressed) drawtype = 2; } if ((mx >= 270) && (mx <= 370) && (my >= 30) && (my <= 80)) { button3 = color(0,0,255); if (mousePressed) drawtype = 3; } if ((mx >= 150) && (mx <= 250) && (my >= 430) && (my <= 480)) { button4 = color(0,0,255); if (mousePressed) sampleon = true; } fill(255); stroke(button1); rect(30,30,100,50,10); stroke(button2); rect(150,30,100,50,10); stroke(button3); rect(270,30,100,50,10); stroke(button4); rect(150,430,100,50,10); fill(0); text("Simple",43,63); text("Stratified",153,63); text("Cluster",283,63); text("Sample", 160,463); switch(drawtype) { case 1: drawSimple(); break; case 2: drawStrata(); break; case 3: drawCluster(); break; } } void drawSimple() { noStroke(); pushMatrix(); fill(255,0,0); translate(10,0); rect(30, 120,40,40); translate(0,120); rect(30, 120,40,40); translate(70,-60); rect(30, 120,40,40); translate(0,120); rect(30, 120,40,40); translate(70,60); rect(30, 120,40,40); popMatrix(); pushMatrix(); fill(255,255,0); translate(10,60); rect(30,120,40,40); translate(70,-60); rect(30,120,40,40); translate(210,0); rect(30,120,40,40); translate(0,60); rect(30,120,40,40); translate(0,120); rect(30,120,40,40); popMatrix(); pushMatrix(); fill(0,255,0); translate(10,180); ellipse(30, 120,40,40); translate(70,-60); ellipse(30, 120,40,40); translate(140,-120); ellipse(30, 120,40,40); translate(0,120); ellipse(30, 120,40,40); translate(70,0); ellipse(30, 120,40,40); popMatrix(); pushMatrix(); fill(0,0,255); translate(10,240); triangle(50,120,30,160,70,160); translate(140,-240); triangle(50,120,30,160,70,160); translate(0,60); triangle(50,120,30,160,70,160); translate(70,0); triangle(50,120,30,160,70,160); translate(0,120); triangle(50,120,30,160,70,160); popMatrix(); pushMatrix(); fill(0,255,255); translate(80, 240); triangle(30,120,70,120,50,160); translate(70, -120); triangle(30,120,70,120,50,160); translate(0, 60); triangle(30,120,70,120,50,160); translate(70, 60); triangle(30,120,70,120,50,160); translate(70, 0); triangle(30,120,70,120,50,160); popMatrix(); if (sampleon) { if (samplecall == 0) { simplesample = chooseSample(25); samplecall = 1; } } noFill(); stroke(0,0,255); int tmp = simplesample[0]; int col = int(tmp/5); int row = tmp-col*5; pushMatrix(); translate(col*70,row*60); rect(30,110,60,60,10); popMatrix(); tmp = simplesample[1]; col = int(tmp/5); row = tmp-col*5; pushMatrix(); translate(col*70,row*60); rect(30,110,60,60,10); popMatrix(); } void mouseReleased() { if (sampleon) { sampleon = false; samplecall = 0; } } int[] chooseSample(int num) { int[] tmplist = new int[num]; int[] outlist = new int[2]; int tmprand, tmprand2; for (int tmp=0; tmp < num; tmp++) tmplist[tmp] = tmp; tmprand = int(random(0,num)); outlist[0] = tmprand; for(int tmp=tmprand; tmp < (num-1); tmp++) tmplist[tmp] = tmplist[tmp+1]; tmprand2 = int(random(0,num-1)); outlist[1] = tmplist[tmprand2]; return(outlist); } void drawStrata() { noStroke(); for (int rep = 0; rep < 5; rep++) { pushMatrix(); translate(10,60*rep); fill(255,0,0); rect(30, 120,40,40); fill(0,255,0); ellipse(100, 120,40,40); fill(0,0,255); triangle(190,120,170,160,210,160); fill(255,255,0); rect(240,120,40,40); fill(0,255,255); triangle(310,120,350,120,330,160); popMatrix(); } int[] tmparray; tmparray = new int[2]; if (sampleon) { if (samplecall == 0) { for (int rep = 0; rep < 5; rep++) { tmparray = chooseSample(5); for (int k = 0; k < 2; k++) stratasample[k+rep*2]=tmparray[k]; } samplecall = 1; } } noFill(); stroke(0,0,255); for (int rep = 0; rep < 10; rep++) { int tmp = stratasample[rep]; int col = int(rep/2); int row = tmp; pushMatrix(); translate(col*70,row*60); rect(30,110,60,60,10); popMatrix(); } } void drawCluster() { noStroke(); for (int rep = 0; rep < 5; rep++) { pushMatrix(); translate(70*rep+10,0); fill(255,0,0); rect(30, 120,40,40); fill(0,255,0); ellipse(30, 180,40,40); fill(0,0,255); triangle(50,240,30,280,70,280); fill(255,255,0); rect(30,300,40,40); fill(0,255,255); triangle(30,360,70,360,50,400); popMatrix(); } if (sampleon) { if (samplecall == 0) { clustersample = chooseSample(5); samplecall = 1; } } noFill(); stroke(0,0,255); noFill(); stroke(0,0,255); for (int rep = 0; rep < 2; rep++) { int col = clustersample[rep]; int row = 0; pushMatrix(); translate(col*70,row*60); rect(30,110,60,300,10); popMatrix(); } }