Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Shape[] shapes;
Table table;
PFont font;
int currentColor = 0;
void setup() {
fullScreen();
background(0);
//LOAD FONT FROM FILE
font = loadFont("Geo-Regular-48.vlw");
loadData();
}
void draw() {
background(0);
for (int i = 0; i < shapes.length; i++) {
shapes[i].opacity = 255;
for (int j = 0; j <shapes.length; j++) {
if (i!=j) {
shapes[i].transparentCollide(shapes[j]);
}
}
}
//CALL FUNCTIONS FOR EACH SHAPE
for (Shape shape : shapes) {
shape.onHover();
shape.move();
shape.display();
}
}
//ON CLICK CHANGE COLORS OF WORDS
void mouseClicked() {
currentColor++;
if (currentColor >= 5) currentColor = 0;
//RED
if (currentColor == 0) {
for (Shape shape : shapes) {
shape.red = shape.colorLevel;
shape.green = 0;
shape.blue = 0;
}
//BLUE
} else if (currentColor == 1) {
for (Shape shape : shapes) {
shape.red = 0;
shape.green = 0;
shape.blue = shape.colorLevel;
}
//GREEN
} else if (currentColor == 2) {
for (Shape shape : shapes) {
shape.red = 0;
shape.green = shape.colorLevel;
shape.blue = 0;
}
//YELLOW
} else if (currentColor == 3) {
for (Shape shape : shapes) {
shape.red = shape.colorLevel;
shape.green = shape.colorLevel;
shape.blue = 0;
}
//PURPLE
} else if (currentColor == 4) {
for (Shape shape : shapes) {
shape.red = shape.colorLevel;
shape.green = 0;
shape.blue = shape.colorLevel;
}
}
}
void loadData() {
//LOAD THE CSV FILE INTO A TABLE OBJECT, USING "HEADER"
table = loadTable("table.csv", "header");
shapes = new Shape[table.getRowCount()];
//ITERATE OVER ALL THE ROWS IN THE TABLE
for (int i = 0; i< table.getRowCount(); i++) {
TableRow row = table.getRow(i); //sets row to "row" variable (to use later)
//ACCESSING DATA VIA COLUMN NAME, SETTING X AND Y POSITIONS
float r = row.getFloat("rank"); //gets float value from specified row
float f = row.getFloat("frequency");
String w = row.getString("word");
shapes[i] = new Shape(f, r, w);
}
}
class Shape {
float frequency, rank, opacity;
float xdir, ydir, textwidth, textheight;
PVector position;
int red, green, blue, colorLevel;
String word, backup_word;
Shape(float f, float r, String w) {
frequency = f;
rank = r;
word = w;
backup_word = w; //HELPS WITH THE ON HOVER FUNCTION
//DETERMINES THE SHADE OF COLOR OF EACH WORD BASED ON RANK
colorLevel = (31 - int(rank)) * 6 + 75;
red = colorLevel;
green = 0;
blue = 0;
textwidth = textWidth(word);
textFont(font, frequency*3); //DETERMINES FONT SIZE BASED ON FREQUENCY
textheight = textAscent() + textDescent();
position = new PVector(int(random(textwidth, width-textwidth)), int(random(textheight, height-textheight)));
opacity = 255;
//ASSIGNS RANDOM X AND Y DIRECTIONS THAT ARE EITHER -1 OR 1 (NOT ZERO)
int[] dir = {-1, 1};
xdir = random(0.7, 1.3) * dir[int(random(2))];
ydir = random(0.7, 1.3) * dir[int(random(2))];
}
void display() {
//float textwidth = textWidth(Word); //getting textwidth to avoid text cutting
//float textSize = textwidth;
fill(red, green, blue, opacity);
textAlign(RIGHT);
textFont(font, frequency*3);
text(word, position.x, position.y);
//fill(red, green, blue, opacity*1.5);
//line(position.x,position.y,position.x+textwidth,position.y);
//rect(position.x-Frequency/2,position.y-Frequency/2,Frequency,Frequency);
}
void move() {
//float textwidth = textWidth(Word);
//println(textwidth);
//println("rank", rank, " ", word);
position.x = position.x + xdir;
position.y = position.y + ydir;
//MAKES SURE WORDS STAY ON SCREEN (WIDTH)
if (position.x+textwidth > width || position.x-textwidth < 0) {
int[] dir = {-1, 1};
xdir = -xdir;
ydir = random(0.7, 1.3) * dir[int(random(2))];
}
//MAKES SURE WORDS STAY ON SCREEN (HEIGHT)
if (position.y > height || position.y - textheight < 0) {
int[] dir = {-1, 1};
xdir = random(0.7, 1.3) * dir[int(random(2))];
ydir = -ydir;
}
}
//void bounceCollide(Shape othershape) {
// float dx = position.x - othershape.position.x;
// float dy = position.y - othershape.position.y;
// float distance = sqrt(dx*dx + dy*dy);
// float miniDist = textwidth + othershape.textwidth;
// if (distance < miniDist) {
// xdir *= -1;
// ydir *= -1;
// othershape.xdir *= -1;
// othershape.ydir *= -1;
// move();
// display();
// othershape.move();
// othershape.display();
// }
//}
//MAKES THE SMALLER SHAPE TEMPORARILY TRANSPARENT UPON OVERLAPS
void transparentCollide(Shape otherShape) {
if (position.x < otherShape.position.x + otherShape.textwidth && position.x + textwidth > otherShape.position.x && position.y-textheight < otherShape.position.y && position.y > otherShape.position.y - otherShape.textheight)
if (frequency < otherShape.frequency) {
opacity = 50;
}
}
//ON HOVER THE RANK IS DISPLAYED INSTEAD OF THE WORD
void onHover() {
if (mouseX > position.x - textwidth && mouseX < position.x && mouseY > position.y-textheight && mouseY < position.y) {
word = "#" + int(rank);
} else {
word = backup_word;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment