// Particle class for 2D space.
// Mark Brand, 22 march 2009.
class Particle
{
PVector acc;
PVector vel;
PVector loc;
float mass;
float lim;
boolean isLimited;
color c;
// Constructor
Particle(PVector a_, PVector v_, PVector l_, float m_)
{
acc = a_.get();
vel = v_.get();
loc = l_.get();
mass = m_;
lim = 0;
isLimited = false;
c = color(255);
}
Particle(PVector a_, PVector v_, PVector l_)
{
acc = a_.get();
vel = v_.get();
loc = l_.get();
mass = 10;
lim = 0;
isLimited = false;
c = color(255);
}
Particle(PVector l_)
{
acc = new PVector();
vel = new PVector();
loc = l_.get();
mass = 10;
lim = 0;
isLimited = false;
c = color(255);
}
void run()
{
update();
render();
}
void update()
{
vel.add(acc);
if (isLimited) vel.limit(lim);
loc.add(vel);
}
void render()
{
drawStar(pg1, loc.x, loc.y);
}
//renders a vector v_, s is a scaling
void renderVector(PVector v_, float s_)
{
pushMatrix();
translate(loc.x, loc.y);
rotate(vel.heading2D());
float len = v_.mag()*s_; //get magnitude and scale
line(0, 0, len, 0);
line(len, 0, len-4, 2);
line(len, 0, len-4, -2);
popMatrix();
}
void setLimit(float l_)
{
lim = l_;
isLimited = true;
}
void addForce(PVector v_)
{
// force=mass*acc, added acc=force/mass
PVector force = v_.get();
force.div(mass);
acc.add(force);
}
void setForce(PVector v_)
{
// force=mass*acc, added acc=force/mass
PVector force = v_.get();
force.div(mass);
acc.set(force);
}
void setColor(color c_) {
c = c_;
}
}
/* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/17672*@* */
/* !do not delete the line above, required for linking your tweak if you re-upload */
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.internal.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
//import twitter4j.org.json.*;
import twitter4j.*;
import twitter4j.http.*;
//import twitter4j.examples.*;
/*
* Lorenz Attractor
* by Rick Companje, 5 december 2008, for Processing 1.0.1
* based on a Processing sketch by Sunghun Kim
* showing the noninear diferential equation described by Lorenz, E. N. (1963). "Deterministic nonperiodic flow". J. Atmos. Sci. 20: 130-141.
* the Lorenz atractor is generated by the following noninear diferential equations:
* dx/dt = s ( y - x )
* dy/dt = r x - y - xz
* dz/dt = xy - b z
*/
//-------------------------------- EXTERNAL LIBRARIES
import controlP5.*;
//Global variables for Twitter search
int y[] = new int[3];
String searchTerms[] = {
"love", "hate",
"brizlogotest"
};
String gt[] = new String[3];
String gf[] = new String[3];
String gn[] = new String[3];
int now;
int lastSecond;
int maximum;
int total;
String startTime;
PImage profile;
long lastId[] = new long[3];
boolean connected = true;
//End of global variables for twitter search
/*-----------------------------------------------------------------------------
*** GLOBAL VARIABLES ***
-----------------------------------------------------------------------------*/
//-------------------------------- OTHER
public float par1 = 0.5;
public float par2 = 0.5;
public float par3 = 0.5;
public float brizreset = 0;
PVector v = new PVector(0, 0, 0);
PVector s = new PVector(.1, 0, 0);
float dt = 0.01;
//default values for the parameters
float sigmaDef = 10;
float betaDef = 8.0 / 3.0;
float rohmDef = 28;
int c=1;
float sigma;
float forceScale=0.01;
float tantemp, rx1, ry1, r1, rx2, ry2, r2, rx3, ry3, r3, brizatt, perc1, perc2;
Particle[] p;
int numP = 51200;
PGraphics pg1;
void setup() {
frameRate(24);
SetupTwitter();
total=1;
size(512, 512, P2D);
colorMode(RGB, 255);
background(224);
smooth();
brizatt = 0;
perc1=0.50;
perc2=0.50;
pg1 = makeTexture(2);
p = new Particle[numP];
for (int i=0; i<numP; i++) {
PVector a = new PVector(0.0f, 0.0f);
float r1 = random(0.3);
float r2 = random(0.3);
float r3 = random(0.3);
PVector v = new PVector(0.0f, 0.0f);
v.add(r1-0.15, r2-0.15, r3-0.15);
PVector l = new PVector(random(50, width-50), random(50, height-50));
p[i] = new Particle(a, v, l);
p[i].setLimit(5.0);
}
colorMode(HSB, 255);
stroke(126);
}
void keyReleased() {
p = new Particle[numP];
for (int i=0; i<numP; i++) {
PVector a = new PVector(0.0f, 0.0f);
PVector v = new PVector(0.0f, 0.0f);
PVector l = new PVector(random(50, width-50), random(50, height-50));
p[i] = new Particle(a, v, l);
float r = 64;
float g = 128;
float b = 192;
p[i].setColor(color(r, g, b, 32));
p[i].setLimit(5.0);
}
}
void draw() {
background(224);
thread("FetchTwitterMsgs");
if (y[0] > 1 & y[1] > 0) {
perc1 =(float)y[0]/total;
perc2 = 1-perc1;
println("Love: "+perc1+" Hate: "+perc2);
}
//if the bristol keyword is detected
if (y[2] > 0){
brizatt=5000;
y[2]=0;
brizreset = 255;
}
if(brizreset>0){
//also do the profile picture thing....
profile = loadImage(gn[2], "png");
profile.resize(width/6, height/6); // .25 replaces 150
tint(255,brizreset);
image(profile,width/2+50,width/2+50); //.094 replaces 100, .467 replaces 280
brizreset-=2;
}
fill(200);
rect(0, 0, 7, 100);
fill(100);
rect(0, 0, 7, perc1*100);
forceScale = 1;
//sigma=sin(map(second(), 0, 60, 0, TWO_PI) - HALF_PI);
sigma=2;
for (int i=0; i<numP; i++) {
//need to define two attractors and you can pull from one to another
//center = width/2,height/2
//define center for attractor one
int at1x = (width/2) + 100;
int at1y = (height/2) + 100;
int at2x = (width/2) -100;
int at2y = (height/2) - 100;
//third mystery 'brizatt' attractor can be center, it's strength decays
brizatt -= brizatt*0.0001;
rx1= at1x - p[i].loc.x;
ry1= at1y - p[i].loc.y;
rx2= at2x - p[i].loc.x;
ry2= at2y - p[i].loc.y;
rx3 = (width/2) -p[i].loc.x;
ry3 = (height/2) - p[i].loc.y;
r1= pow(rx1, 2)+pow(ry1, 2);
r2= pow(rx2, 2)+pow(ry2, 2);
r3= pow(rx3, 2)+pow(ry3, 2);
//follow the path of the vector field
float boost1=1, boost2=1;
int percpow=1;
//work on principle that the bigger one gets a boost
if (perc1 > perc2) {
boost1=pow((perc2-perc1), 3);
}
if (perc1 < perc2) {
boost2=pow((perc2-perc1), 3);
;
}
if (perc1==perc2){
boost1=1;
boost2=2;}
v.x = boost1*rx1/r1;
v.y = boost1*ry1/r1;
v.x += boost2*rx2/r2;
v.y += boost2*ry2/r2;
v.x += brizatt*rx3/r3;
v.y += brizatt*ry3/r3;
v.mult(forceScale);
//p[i].renderVector(v,1);
p[i].setForce(v);
p[i].run();
}
//add a blur ot make it more liquidy
//filter(BLUR, 0.4);
}
PVector getGravForce(Particle p_) {
PVector loc = new PVector(mouseX, mouseY);
float mass = 10.0f;
float g = 100;
PVector dir = new PVector();
dir = dir.sub(loc, p_.loc);
float d = dir.mag();
d = constrain(d, 5.0, 15.0);
dir.normalize();
float f = (g*mass*p_.mass)/(d*d);
dir.mult(f);
return dir;
}
PGraphics makeTexture(int r) {
PGraphics res = createGraphics(r, r, P2D);
res.beginDraw();
res.loadPixels();
for ( int x = 0; x < res.width; x++) {
for ( int y = 0; y < res.height; y++ ) {
res.pixels[y*res.width+x] = color(128, 64, 64, 64);
}
}
res.updatePixels();
res.endDraw();
return res;
}
void drawStar( PImage img, float x, float y ) {
blend(img, 0, 0, img.width, img.height, int(x) - img.width/2, int(y) - img.height/2, img.width, img.height, SUBTRACT);
}
void mousePressed(){
y[2]+=1;}
void SetupTwitter(){
smooth();
if (connected){
Twitter twitter = new Twitter();
for (int party = 0; party < 3; party++){
try{
Query query = new Query(searchTerms[party]);
QueryResult result = (QueryResult) twitter.search(query);
java.util.List tweets = result.getTweets();
println(tweets.size());
Tweet t = (Tweet) tweets.get(0);
lastId[party] = t.getId();
println(lastId[party]);
gt[party] = t.getText();
gf[party] = t.getFromUser();
gn[party] = t.getProfileImageUrl();
}
catch (Exception e){
System.out.println(e);
}
}
}
// for (int i = 0; i < 3; i++){
// target[i]=height-12;
// }
startTime = hour() + ":" + trim(nfs(minute(),2));
}
void FetchTwitterMsgs(){
now = second();
maximum = max(y[0],y[1],y[2]);
total = y[0]+y[1]+y[2];
//every 3 seconds this gets the tweets
if (now!= lastSecond && now%3==0){
//do a new twitter search since the last ID
if (connected){
Twitter twitter = new Twitter();
for (int party = 0; party < 3; party++){
try{
Query query = new Query(searchTerms[party]);
//returns tweets with status ids greater than the given id
query.setSinceId(lastId[party]);
//QueryResult is A data interface representing search API response
QueryResult result = (QueryResult) twitter.search(query);
java.util.List tweets = result.getTweets();
//println(tweets.size());
if (tweets.size()>0){
Tweet t = (Tweet) tweets.get(0);
//guess it must be sorted so that the 0th is the most recent
lastId[party] = t.getId();
//gt is the text of the 0th tweet
gt[party] = t.getText();
//gf us user profile name
gf[party] = t.getFromUser();
//gn is the URL of the profile image for the 0th tweeter
gn[party] = t.getProfileImageUrl();
//println(gf[party]);
//println(lastId[party]);
y[party]+=tweets.size(); // height/6 replaces 100
}
}
catch (Exception e){
System.out.println(e);
}
}
}
else{
//if it fails it just makes up random numbers!
for (int i = 0; i < 3; i++){
y[i]+= int(random(10));
}
}
}
lastSecond = now;
}
void DisplayProfilePics(){
//following does the displaying
for (int i = 0; i < 2; i++){
profile = loadImage(gn[i], "png");
profile.resize(width/6, height/4); // .25 replaces 150
image(profile,(2*i+1)*width/6-width*.094,height-height*.467); //.094 replaces 100, .467 replaces 280
}
if(gn[2]!=null){
profile = loadImage(gn[2], "png");
profile.resize(width/6, height/4); // .25 replaces 150
image(profile,(2*2+1)*width/6-width*.094,height-height*.467); //.094 replaces 100, .467 replaces 280
}
}