Created
January 27, 2015 13:17
-
-
Save CSchoel/ec03a5cfa61abedeb2e7 to your computer and use it in GitHub Desktop.
Bar chart with modification and sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Autor: Christopher Schölzel | |
class Bar { | |
int value; | |
boolean marked; | |
Bar(int value) { | |
this.value = value; | |
marked = false; | |
} | |
//Zeichnet den balken an position (x,y) mit der Breite b und | |
//dem Streckungsfaktor hFac für die Balkenhöhe. | |
void display(float x,float y,float w,float hFac) { | |
stroke(0); | |
fill(marked ? #FF0000 : 255); | |
rect(x,y,w,-hFac*value); | |
} | |
void setMarked(boolean marked) { | |
this.marked = marked; | |
} | |
void setValue(int value) { | |
this.value = value; | |
} | |
void incrementValue() { | |
value++; | |
} | |
void decrementValue() { | |
value--; | |
} | |
float getValue() { | |
return value; | |
} | |
} | |
//Klasse zum schrittweisen sortieren eines Arrays | |
//Implementiert den Sortieralgorithmus aus dem Aufgabenblatt | |
class StepSorter { | |
int sortedFrom; | |
Bar[] subject; | |
StepSorter(Bar[] bars) { | |
subject = bars; | |
sortedFrom = bars.length; | |
} | |
void sortStep() { | |
assert sortedFrom > 0; | |
//Ein Schritt: | |
//Suche das größte noch nicht sortierte Element | |
int maxIndex = 0; | |
Bar maxBar = subject[0]; | |
for(int i = 0; i < sortedFrom; i++) { | |
if(subject[i].getValue() > maxBar.getValue()) { | |
maxBar = subject[i]; | |
maxIndex = i; | |
} | |
} | |
//und packe es an die höchste unsortierte Stelle | |
subject[maxIndex] = subject[sortedFrom-1]; | |
subject[sortedFrom-1] = maxBar; | |
//Markiere dabei die beiden vertauschten Elemente | |
clearMarkings(); | |
subject[maxIndex].setMarked(true); | |
subject[sortedFrom-1].setMarked(true); | |
sortedFrom--; //Ein Element mehr wurde sortiert | |
} | |
boolean isDone() { | |
return sortedFrom == 0; | |
} | |
} | |
int SELECT = 0; //Zustandswert für freies Selektieren | |
int SORT = 1; //Zustandswert für Sortiermodus | |
int sortInterval = 500; //Ein Schritt alle 1000 ms | |
int lastStepMillis = 0; //wann wurde letzter Schritt ausgeführt? | |
Bar[] bars; //Array mit Balken | |
int state = SELECT; //Aktueller Programmzustand (SELECT oder SORT) | |
int selectIndex = 0; //Index des aktuell ausgewählten Balkens | |
StepSorter sorter = null; //Aktuell verwendeter Sortierer | |
void setup() { | |
size(400,300); | |
bars = new Bar[20]; | |
randomFill(); | |
selectIndex = 0; | |
bars[selectIndex].setMarked(true); | |
} | |
//Befüllt das Array bars mit zufälligen Werten | |
void randomFill() { | |
for(int i = 0; i < bars.length; i++) { | |
bars[i] = new Bar((int)random(0,21)-10); //Zufallszahl zwischen -10 und 10 | |
} | |
} | |
//Setzt alle Markierungen zurück | |
void clearMarkings() { | |
for(Bar b : bars) b.setMarked(false); | |
} | |
void draw() { | |
background(0); | |
if(state == SORT) { | |
assert sorter != null; | |
if(millis()-lastStepMillis > sortInterval) { | |
sorter.sortStep(); | |
if (sorter.isDone()) { | |
state = SELECT; | |
clearMarkings(); | |
bars[selectIndex].setMarked(true); | |
} | |
lastStepMillis = millis(); | |
} | |
} | |
float barWidth = 10; | |
float padding = 5; | |
for(int i = 0; i < bars.length; i++) { | |
bars[i].display(50+i*(barWidth+padding),height/2.0,barWidth,10); | |
} | |
} | |
void keyPressed() { | |
if (state != SELECT) return; | |
bars[selectIndex].setMarked(false); //remove old marking | |
if (keyCode == LEFT) { | |
selectIndex--; | |
} else if (keyCode == RIGHT) { | |
selectIndex++; | |
} else if (keyCode == UP) { | |
bars[selectIndex].incrementValue(); | |
} else if (keyCode == DOWN) { | |
bars[selectIndex].decrementValue(); | |
} else if (keyCode == CONTROL) { //Srg füllt mit Zufallswerten | |
randomFill(); | |
selectIndex = 0; | |
bars[selectIndex].setMarked(true); | |
} else if (key == ' ') { //Space startet die Sortierung | |
state = SORT; | |
sorter = new StepSorter(bars); | |
lastStepMillis = millis(); | |
} | |
//index anpassen, falls nötig | |
if(selectIndex >= bars.length) selectIndex = 0; | |
else if (selectIndex < 0) selectIndex = bars.length-1; | |
bars[selectIndex].setMarked(true); //apply new marking | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment