// Get number of results from Google Search
//Variables for first amoeba
float x1[] = new float[2000]; //x positions of amoeba circumference
float y1[] = new float[2000]; //y positions of amoeba circumference
float r1[] = new float[2000]; //radius variant variable of the amoeba
float radius1 = 50; //Overall radius of the amoeba
float amplitude1 = 15; //Amplitude of amoeba variation - set as function of radius
float resolution1 = 0.1; //Resolution of wobble - set as a function of radius
int count1;
//Variable for second amoeba
float x2[] = new float[2000]; //x positions of amoeba circumference
float y2[] = new float[2000]; //y positions of amoeba circumference
float r2[] = new float[2000]; //radius variant variable of the amoeba
float radius2 = radius1; //Overall radius of the amoeba
float amplitude2 = amplitude1; //Amplitude of amoeba variation - set as function of radius
float resolution2 = resolution1; //Resolution of wobble - set as a function of radius
int count2;
int screenW = 400;
int screenH = 400;
//Text variables
PFont f;
String resultString = ""; //results from Google
String nameString = ""; //name typed in
String name1 = ""; //fist name
String name2 = ""; //second name
int numHits1 = 0; //humber of hits for the first name
int numHits2 = 0; //humber of hits for the second name
boolean go = false; //flag to do display results or not
void setup()
{
size(screenW,screenH);
f = loadFont("CourierNew36.vlw");
frameRate(30);
smooth();
//Initialize first amoeba
int i1 = 0;
for (float theta = -PI; theta <= PI; theta = theta + resolution1)
{
r1[i1] = radius1 + sin(i1)* amplitude1;
x1[i1] = cos(theta) * r1[i1];
y1[i1] = sin(theta) * r1[i1];
i1++;
}
count1 = i1;
//Initialize second amoeba
int i2 = 0;
for (float theta = -PI; theta <= PI; theta = theta + resolution2)
{
r2[i2] = radius2 + sin(i2)* amplitude2;
x2[i2] = cos(theta) * r2[i2];
y2[i2] = sin(theta) * r2[i2];
i2++;
}
count2 = i2;
}
void draw()
{
background(100);
noStroke();
textFont(f,14);
fill(255);
//Instructions
if (go == false)
{
text("Type in 2 names separated by a comma!!", 10, 30);
//Input box
fill(190);
rect(10, 60, 300, 20);
fill(0);
text(nameString, 14, 65, 298, 20);
}
else if (go == true)
{
//Displays
textFont(f,20);
fill(0,0,255);
text("Backspace to start over", 50, 40);
text(name1, 30, 80);
text(name2, 230, 80);
//println("radius1 = " + radius1 + " radius2 = " + radius2);
//Draw Amoebas
//Draw first amoeba
fill(229,59,45);
beginShape();
for (int i = 0; i < count1; i++)
{
vertex(100 + x1[i]*(1+radius1/(radius1+radius2)) + random(0,10), 270 + y1[i]*(1+radius1/(radius1+radius2)) + random(0,10));
}
endShape(CLOSE);
//Draw second amoeba
fill(100,100,237);
beginShape();
for (int i = 0; i < count2; i++)
{
vertex(300 + x2[i]*(1+radius2/(radius1+radius2)) + random(0,10), 270 + y2[i]*(1+radius2/(radius1+radius2)) + random(0,10));
}
endShape(CLOSE);
}
}
// User keyboard input
void keyPressed()
{
//Get string size
int len = nameString.length();
int comma = nameString.indexOf(",");
if (key == '\n') //if return is pressed, save the string and return a message
{
go = true;
//Extract names
if (comma == -1) //if no comma, set it to length and ignore name2
{
comma = len;
name1 = nameString.substring(0, comma);
name2 = "George Bush";
radius1 = doSearch(name1);
radius2 = doSearch(name2);
}
else
{
name1 = nameString.substring(0, comma);
name2 = nameString.substring(comma + 2, len);
//Do the search
radius1 = doSearch(name1);
radius2 = doSearch(name2);
}
}
else if (keyCode == BACKSPACE) //backspace clears entered text or restarts
{
if (go == true) //if results are being shown, reset string
{
nameString = "";
go = false;
}
else
{
nameString = nameString.substring(0, len - 1);
}
}
else if (keyCode == SHIFT) {
nameString = nameString; //correction for shift key problem
}
else {
nameString = nameString + key; //otherwise (aka normally), print the user's entry back to the screen
}
}
int doSearch(String Name)
{
int a;
Name = Name.replaceAll(" ","+");
//Get data from google search
String[] source = loadStrings("http://itp.nyu.edu/icm/proxy/proxy.php?url=http://www.google.com/search?q=%22" + Name + "%22");
String sourceLines = join(source, " ");
//Strings to search between
String startSearchText = " of about ";
String endSearchText = " for ";
int start = sourceLines.indexOf(startSearchText) + startSearchText.length();
int end = sourceLines.indexOf(endSearchText,start);
if (start == -1 || end == -1){
start = 0;
}
String resultTemp = sourceLines.substring(start,end);
String resultTemp2 = resultTemp.replaceAll(",","");
String resultTemp3 = trim(resultTemp2);
a = int(resultTemp3);
println("Name = " + Name + " a = " + a);
return a;
}