Initial Commit

This commit is contained in:
plane000
2018-04-20 10:15:15 +01:00
parent 49150ccfe4
commit 62101e8e61
2870 changed files with 520122 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
/**
* HashMap example
* by Daniel Shiffman.
*
* This example demonstrates how to use a HashMap to store
* a collection of objects referenced by a key. This is much like an array,
* only instead of accessing elements with a numeric index, we use a String.
* If you are familiar with associative arrays from other languages,
* this is the same idea.
*
* A simpler example is CountingStrings which uses IntDict instead of
* HashMap. The Processing classes IntDict, FloatDict, and StringDict
* offer a simpler way of pairing Strings with numbers or other Strings.
* Here we use a HashMap because we want to pair a String with a custom
* object, in this case a "Word" object that stores two numbers.
*
* In this example, words that appear in one book (Dracula) only are colored white
* while words the other (Frankenstein) are colored black.
*/
HashMap<String, Word> words; // HashMap object
void setup() {
size(640, 360);
// Create the HashMap
words = new HashMap<String, Word>();
// Load two files
loadFile("dracula.txt");
loadFile("frankenstein.txt");
// Create the font
textFont(createFont("SourceCodePro-Regular.ttf", 24));
}
void draw() {
background(126);
// Show words
for (Word w : words.values()) {
if (w.qualify()) {
w.display();
w.move();
}
}
}
// Load a file
void loadFile(String filename) {
String[] lines = loadStrings(filename);
String allText = join(lines, " ").toLowerCase();
String[] tokens = splitTokens(allText, " ,.?!:;[]-\"'");
for (String s : tokens) {
// Is the word in the HashMap
if (words.containsKey(s)) {
// Get the word object and increase the count
// We access objects from a HashMap via its key, the String
Word w = words.get(s);
// Which book am I loading?
if (filename.contains("dracula")) {
w.incrementDracula();
}
else if (filename.contains("frankenstein")) {
w.incrementFranken();
}
}
else {
// Otherwise make a new word
Word w = new Word(s);
// And add to the HashMap put() takes two arguments, "key" and "value"
// The key for us is the String and the value is the Word object
words.put(s, w);
if (filename.contains("dracula")) {
w.incrementDracula();
} else if (filename.contains("frankenstein")) {
w.incrementFranken();
}
}
}
}

View File

@@ -0,0 +1,72 @@
class Word {
// Store a count for occurences in two different books
int countDracula;
int countFranken;
// Also the total count
int totalCount;
// What is the String
String word;
// Where is it on the screen
PVector position;
Word(String s) {
position = new PVector(random(width), random(-height, height*2));
word = s;
}
// We will display a word if it appears at least 5 times
// and only in one of the books
boolean qualify() {
if ((countDracula == totalCount || countFranken == totalCount) && totalCount > 5) {
return true;
}
else {
return false;
}
}
// Increment the count for Dracula
void incrementDracula() {
countDracula++;
totalCount++;
}
// Increment the count for Frankenstein
void incrementFranken() {
countFranken++;
totalCount++;
}
// The more often it appears, the faster it falls
void move() {
float speed = map(totalCount, 5, 25, 0.1, 0.4);
speed = constrain(speed,0,10);
position.y += speed;
if (position.y > height*2) {
position.y = -height;
}
}
// Depending on which book it gets a color
void display() {
if (countDracula > 0) {
fill(255);
}
else if (countFranken > 0) {
fill(0);
}
// Its size is also tied to number of occurences
float fs = map(totalCount,5,25,2,24);
fs = constrain(fs,2,48);
textSize(fs);
textAlign(CENTER);
text(word, position.x, position.y);
}
}