mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
reworking of IntHash and HashMap examples
This commit is contained in:
@@ -1,15 +1,72 @@
|
||||
class Word {
|
||||
|
||||
int count;
|
||||
// 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;
|
||||
count = 1;
|
||||
}
|
||||
|
||||
void count() {
|
||||
count++;
|
||||
// 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,3);
|
||||
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(121, 0, 31);
|
||||
}
|
||||
else if (countFranken > 0) {
|
||||
fill(33, 72, 122);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user