Skip to content

Instantly share code, notes, and snippets.

@marsimaria
Created December 4, 2014 00:00
Show Gist options
  • Save marsimaria/84529c07e26f3d8b5d31 to your computer and use it in GitHub Desktop.
Save marsimaria/84529c07e26f3d8b5d31 to your computer and use it in GitHub Desktop.
Processing code of my marijuana article count project
// A simple Particle class, renders the particle as an image
class Particle {
PVector loc;
PVector vel;
PVector acc;
float lifespan;
PImage img;
//fadeout later
float fadeout;
//Don't exceed this speed
float maxspeed;
Particle(PVector l,PImage img_) {
acc = new PVector(0,0);
float vx = randomGaussian()*0.3;
float vy = randomGaussian()*0.3;
vel = new PVector(vx,vy);
// vel = new PVector(0,0);
loc = l.get();
lifespan = 150.0;
img = img_;
fadeout = random(3, 4);
maxspeed = random(7,8);
}
void run() {
update();
render();
}
// Method to apply a force vector to the Particle object
// Note we are ignoring "mass" here
void applyForce(PVector f) {
acc.add(f);
}
// Method to update location
void update() {
vel.add(acc);
vel.limit(maxspeed);
loc.add(vel);
lifespan -= fadeout;
acc.mult(0); // clear Acceleration
}
// Method to display
void render() {
imageMode(CENTER);
tint(255,lifespan);
image(img,loc.x,loc.y);
// Drawing a circle instead
// fill(255,lifespan);
// noStroke();
// ellipse(loc.x,loc.y,img.width,img.height);
}
// Is the particle still useful?
boolean isDead() {
if (lifespan <= 0.0) {
return true;
} else {
return false;
}
}
}
import processing.pdf.*;
JSONObject json;
JSONArray list;
String query = "";
String url = "";
String setup = "";
String apiKey = "";
String[] date;
String[] title;
String[] snippet;
int[] months = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
String[] month_label = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
// read/write to file
Table table;
// Smoke particle
ParticleSystem[] ps;
PFont font1, font2;
void setup() {
size(1000, 1000, P2D);
/**
apiKey = "74548056debe1698254a3a07babc9f84:3:60371965";
url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=";
query = "marijuana";
setup = "&fl=keywords%2Cheadline";
table = new Table();
table.addColumn("year");
table.addColumn("count");
table.addColumn("article");
// Get data from NYT API and write to wordcount.csv file
for (int i=1926; i<2014; i++) {
try {
json = loadJSONObject(url+query+"&api-key="+apiKey+setup+"&begin_date="+i+"0101&"+"end_date="+i+"1231");
TableRow newRow = table.addRow();
newRow.setInt("year", i);
newRow.setInt("count", json.getJSONObject("response").getJSONObject("meta").getInt("hits"));
String article_title = "";
if (json.getJSONObject("response").getJSONObject("meta").getInt("hits") > 0) {
article_title = json.getJSONObject("response").getJSONArray("docs").getJSONObject(0).getJSONObject("headline").getString("main");
}
newRow.setString("article", article_title);
delay(100);
}
catch (Exception e) {
println("error with year: "+i + " error: "+e);
}
}
saveTable(table, "wordcount.csv");
*/
// read from wordcount.csv file
table = loadTable("marijuana2013.csv", "header");
// initialize variables
int table_size = table.getRowCount();
date = new String[table_size];
title = new String[table_size];
snippet = new String[table_size];
int i = 0;
for (TableRow row : table.rows ()) {
date[i] = row.getString("date");
title[i] = row.getString("title");
snippet[i] = row.getString("snippet");
i++;
}
// get number of articles per month
for (i=0; i< table_size; i++) {
int m = int(date[i].substring(5, 7))-1;
months[m] = months[m] + 1;
}
//Load smoke Image
PImage img = loadImage("texture_p2.png");
//initiate particle system array
ps = new ParticleSystem[12];
//set position of particles
for (int j=0; j<12; j++) {
//set Y away from center point to create hole in middle
ps[j] = new ParticleSystem(0, new PVector(0, -50), img);
}
font1 = loadFont("CenturyGothic-Bold-48.vlw");
font2 = loadFont("CenturyGothic-BoldItalic-48.vlw");
// PDF load font
// font = createFont("ProximaNova-Regular-12.vlw", 8);
println(months);
}
void draw() {
//beginRecord(PDF, "mariInDraw.pdf");
background(0);
fill(#FFFFFF);
textAlign(CENTER);
textFont(font2);
textSize(22);
text("2013", width/2-1, height/2+4);
for (int i=0; i < 12; i++) {
pushMatrix();
translate(width/2, height/2);
// create new value as reference to rotate
int newYear = (int)map(i, 0, 12, 0, 360);
//println(newYear);
// Use radians cus PI is wrong command
rotate(radians(newYear));
//Add year to each smoke/flame
// pushMatrix();
// translate(0, 0);
// rotate(radians(PI));
if (i == 0) {
pushMatrix();
translate(1, -350);
//rotate(PI/2);
fill(#FFDF42);//#C290DB
textFont(font1);
textSize(10);
text(month_label[i], 0, 8);
popMatrix();
} else {
pushMatrix();
translate(1, -350);
//rotate(PI/2);
fill(#FFDF42); //#816BC8, #9E9E9E
textFont(font1);
textSize(10);
text(month_label[i], 0, 8);
popMatrix();
}
// popMatrix();
//Use wordcount as windforce = how strong it will blow up
//PVector wind = new PVector(0, -0.001*((wordcounts[i])));
float windY = map(months[i], 0, 100, 0, -0.45);
PVector wind = new PVector(0, windY);
//println(wind);
//Draw particle for each year. DO NOT divide wordcount[i] directly cus it meant skip years
for (int j=0; j< months[i]; j++) {
//Use j%40 to divide/reduce the total wordcount of each year
if (j%60 ==0) {
ps[i].applyForce(wind);
ps[i].run();
ps[i].addParticle();
}
}
popMatrix();
}
}
// void mousePressed() {
// saveFrame();
// }
// A class to describe a group of Particles
// An ArrayList is used to manage the list of Particles
class ParticleSystem {
ArrayList<Particle> particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
PImage img;
ParticleSystem(int num, PVector v, PImage img_) {
particles = new ArrayList<Particle>(); // Initialize the arraylist
origin = v.get(); // Store the origin point
img = img_;
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin, img)); // Add "num" amount of particles to the arraylist
}
}
void run() {
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = particles.get(i);
p.run();
if (p.isDead()) {
particles.remove(i);
}
}
}
// Method to add a force vector to all particles currently in the system
void applyForce(PVector dir) {
// Enhanced loop!!!
for (Particle p: particles) {
p.applyForce(dir);
}
}
void addParticle() {
particles.add(new Particle(origin,img));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment