Created
October 30, 2012 17:01
-
-
Save lizkhoo/3981540 to your computer and use it in GitHub Desktop.
CentralPark_Weather
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PFont font; | |
PImage centralPark; | |
int counter = 0; | |
CPWeather wg; | |
Clouds[] littleClouds; | |
int count; | |
void setup() { | |
size(800, 600); | |
//make object | |
wg = new CPWeather(); | |
//request weather | |
wg.requestWeather(); | |
count = wg.humidity; | |
littleClouds = new Clouds[count]; | |
for (int i = 0; i < count; i++) { | |
littleClouds[i] = new Clouds(random(width), random(height), random(50, 200)); | |
} | |
font = loadFont("Didot-22.vlw"); | |
centralPark = loadImage("http://www.visitingdc.com/images/central-park-picture.jpg"); | |
} | |
void draw() { | |
image (centralPark, 0, 0, width, height); | |
noStroke(); | |
float brightness = map(wg.temperature, 20, 100, 200, 0); | |
fill(0, brightness); | |
rect(0, 0, width, height); | |
for (int i = 0; i < count; i++){ | |
littleClouds[i].draw(); | |
littleClouds[i].move(); | |
littleClouds[i].loopCloud(); | |
} | |
textFont(font); | |
//Get values to display | |
String weather = wg.getWeather(); | |
int temp = wg.getTemp(); | |
int humid = wg.getHumid(); | |
String wind = wg.getWind(); | |
//Display data | |
fill(255); | |
text(weather, 10, 50); | |
text("Temp = " + temp + "F", 10, 100); | |
text("Humidity = " + humid, 10, 150); | |
text(wind, 10, 200); | |
} | |
class CPWeather { | |
String weather = " "; | |
int temperature = 0; | |
int humidity = 0; | |
String wind = " "; | |
String windDir = " "; | |
float speed = 0; | |
//Get weather | |
String getWeather(){ | |
return weather; | |
} | |
//Get temperature | |
int getTemp(){ | |
return temperature; | |
} | |
//Get humidity | |
int getHumid(){ | |
return humidity; | |
} | |
//get wind | |
String getWind(){ | |
return wind; | |
} | |
//Get wind direction | |
String getWindDir(){ | |
return windDir; | |
} | |
//Get wind speed | |
float getSpeed(){ | |
return speed; | |
} | |
//Reqeust XML | |
void requestWeather(){ | |
String url = "http://w1.weather.gov/xml/current_obs/KNYC.xml"; | |
String[] lines = loadStrings(url); | |
//Turn array into one string | |
String xml = join(lines, ""); | |
//Search weather string | |
String lookfor = "<weather>"; | |
String end = "</weather>"; | |
weather = giveMeTextBetween(xml, lookfor, end); | |
println(weather); | |
//Search temperature Farenheit | |
lookfor = "<temp_f>"; | |
end = "</temp_f>"; | |
temperature = int(giveMeTextBetween(xml, lookfor, end)); | |
println(temperature); | |
//Search humidity rating | |
lookfor = "<relative_humidity>"; | |
end = "</relative_humidity>"; | |
humidity = int(giveMeTextBetween(xml, lookfor, end)); | |
println(humidity); | |
//Search wind string | |
lookfor = "<wind_string>"; | |
end = "</wind_string>"; | |
wind = giveMeTextBetween(xml, lookfor, end); | |
println(wind); | |
//Search wind direction string | |
lookfor = "<wind_dir>"; | |
end = "</wind_dir>"; | |
windDir = giveMeTextBetween(xml, lookfor, end); | |
println(windDir); | |
//Search wind speed | |
lookfor = "<wind_mph>"; | |
end = "</wind_mph>"; | |
speed = float(giveMeTextBetween(xml, lookfor, end)); | |
println(speed); | |
} | |
// A function that returns a substring between two substrings | |
String giveMeTextBetween(String s, String before, String after) { | |
String found = ""; | |
int start = s.indexOf(before); // Find the index of the beginning tag | |
if (start == - 1) return""; // If we don't find anything, send back a blank String | |
start += before.length(); // Move to the end of the beginning tag | |
int end = s.indexOf(after,start); // Find the index of the end tag | |
if (end == -1) return""; // If we don't find the end tag, send back a blank String | |
return s.substring(start,end); // Return the text in between | |
} | |
} | |
class Clouds { | |
float xpos, ypos, diameter; | |
Clouds (float x, float y, float d) { | |
xpos = x; | |
ypos = y; | |
diameter = d; | |
} | |
void draw() { | |
stroke(100); | |
fill(155, 50); | |
ellipse(xpos, ypos, diameter, diameter); | |
} | |
void move() { | |
int speedDirX = 0 ; | |
int speedDirY = 0 ; | |
if (wg.windDir.equals("East")) { | |
speedDirX = -1; | |
} | |
else if (wg.windDir.equals("North")) { | |
speedDirY = -1; | |
} | |
else if (wg.windDir.equals("West")||wg.windDir.equals("Variable")) { | |
speedDirX = 1; | |
} | |
else if (wg.windDir.equals("South")) { | |
speedDirY = 1; | |
} | |
xpos += wg.speed*speedDirX; | |
ypos += wg.speed*speedDirY; | |
} | |
void loopCloud() { | |
if (xpos <= 0) { | |
xpos = width; | |
} else if (xpos >= width){ | |
xpos = 0; | |
} | |
if (ypos <= 0) { | |
ypos = height; | |
} else if (ypos >= height){ | |
ypos = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment