Skip to content

Commit

Permalink
auto mode
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-stamate committed Jul 1, 2021
1 parent d745800 commit 0d29a22
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/main/java/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MainClass extends PApplet {
private Window window;

public void settings() {
size(1600, 640, P2D);
size(1600, 640);
}

public void setup() {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/window/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void pre() {
if (pApplet.width != windowWidth || pApplet.height != windowHeight) {
windowWidth = pApplet.width;
windowHeight = pApplet.height;
System.out.println("resize");
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/window/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package window.config;

/* TODO: add setters and getters */
/* TODO: see if a listener when a parameter change is useful */
public class Config {
public static final int FONT_SIZE = 11;
public static final int PAUSE_DELAY_TIME = 100;
Expand All @@ -10,7 +12,18 @@ public class Config {

public static final int MIN_ARRAY_SIZE = 100;
public static int arraySize = 128;
public static final int MAX_ARRAY_SIZE = 2000;
public static final int MAX_ARRAY_SIZE = 2048;

public static boolean sound = true;

private static boolean autoRunning = false;

/* SETTERS AND GETTERS */
public static void setAutoRunning(boolean bool) {
autoRunning = bool;
}

public static boolean isAutoRunning() {
return autoRunning;
}
}
1 change: 1 addition & 0 deletions src/main/java/window/config/Controls.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public final class Controls {
public final static String DELAY = "Delay";
public static final String THEME = "Theme";
public static final String TOGGLE_SOUND = "Sound";
public static final String AUTO = "Auto";

}
29 changes: 29 additions & 0 deletions src/main/java/window/config/SortingAlgorithms.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package window.config;

import java.util.HashMap;
import java.util.Map;

public class SortingAlgorithms {

public final static String BUBBLE_SORT = "Bubble Sort";
Expand Down Expand Up @@ -52,4 +55,30 @@ public class SortingAlgorithms {
BOGO_SORT
};

public static final Map<String, int[]> idealParameters = new HashMap<>() {{
put(BUBBLE_SORT, new int[]{220, 1});
put(ODD_EVEN_SORT, new int[]{220, 1});
put(INSERTION_SORT, new int[]{220, 1});
put(SELECTION_SORT, new int[]{220, 1});
put(COCKTAIL_SORT, new int[]{220, 1});
put(GNOME_SORT, new int[]{220, 1});
put(COMB_SORT, new int[]{1500, 3});
put(CIRCLE_SORT, new int[]{1000, 3});
put(CYCLE_SORT, new int[]{2048, 4});
put(SMOOTH_SORT, new int[]{700, 1});
put(SHELL_SORT, new int[]{1600, 1});
put(QUICK_SORT, new int[]{2048, 1});
put(RADIX_SORT, new int[]{1000, 1});
put(MERGE_SORT, new int[]{1500, 1});
put(ITERATIVE_MERGE_SORT, new int[]{1500, 1});
put(IN_PLACE_MERGE_SORT_ONE, new int[]{1500, 1});
put(IN_PLACE_MERGE_SORT_TWO, new int[]{1500, 1});
put(HEAP_SORT, new int[]{1500, 1});
put(BITONIC_SORT, new int[]{2048, 1});
put(COUNTING_SORT, new int[]{2048, 2});
put(PIGEONHOLE_SORT, new int[]{2048, 2});
put(GRAVITY_SORT, new int[]{2048, 3});
put(BOGO_SORT, new int[]{128, 70});
}};

}
142 changes: 114 additions & 28 deletions src/main/java/window/main/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ public void draw() {
float lineSpace = 1.0f * width / vector.length;

for (int i = 0; i < vector.length; i++) {
Color col = color[i];

Color col = Colors.DEFAULT_LINE_COLOR;

/* yeah, sometimes it crashed */
try {
col = color[i];
} catch (Exception e) { }

pApplet.stroke(col.r, col.g, col.b);
if (col == Colors.DEFAULT_LINE_COLOR) {
Color defCol = Theme.lineColor;
Expand All @@ -56,6 +63,22 @@ public void draw() {
}
}

@Override
public void stop() {
super.stop();

sortingAlgorithm.stop();
}

@Override
public void mouseReleased() {
if (Config.arraySize != vector.length) {
resizeVector(Config.arraySize);
sortingAlgorithm = new Blank(pApplet, vector, color);
}
}

/* VECTOR MANIPULATION */
public static double mapValueToWindowSize(int windowHeight, int vectorMaxValue, int x) {
windowHeight = windowHeight - 50;
return (1.0 * x / vectorMaxValue) * windowHeight + 5;
Expand All @@ -64,9 +87,10 @@ public static double mapValueToWindowSize(int windowHeight, int vectorMaxValue,
public void resizeVector(int newSize) {
vector = new int[newSize];
color = new Color[newSize];
int n = vector.length;

for (int i = 0; i < vector.length; i++) {
vector[i] = (int) mapValueToWindowSize(height, vector.length - 1, vector.length - i - 1);
for (int i = 0; i < n; i++) {
vector[i] = (int) MainPanel.mapValueToWindowSize(Size.mainWindowHeight, n - 1, i);
color[i] = Colors.DEFAULT_LINE_COLOR;
}

Expand All @@ -77,11 +101,55 @@ public void resizeVector(int newSize) {
}
}

/* AUTO MODE */
private void loopAlgorithms() {
new Thread(() -> {
Config.setAutoRunning(true);

for (String algorithm : SortingAlgorithms.SORTING_ALGORITHMS) {
int idealVectorSize = SortingAlgorithms.idealParameters.get(algorithm)[0];
int idealDelay = SortingAlgorithms.idealParameters.get(algorithm)[1];

Config.delayTime = idealDelay;
Config.arraySize = idealVectorSize;
resizeVector(Config.arraySize);

startInputType(InputType.SHUFFLE);
waitTillAlgorithmStops();

if (!Config.isAutoRunning()) {
break;
}

sleep(500);

startAlgorithm(algorithm);
waitTillAlgorithmStops();

if (!Config.isAutoRunning()) {
break;
}

sleep(500);
}

Config.setAutoRunning(false);
}).start();

}

/* USER INTERACTION */
public void setSidePanelEvents(SidePanel sidePanel) {
sidePanel.addControlListener((event, type) -> {
if (type == Controls.TYPE_BUTTON) {
onButtonPressed(event);
} else if (type == Controls.TYPE_INPUT_TYPE) {
}

if (Config.isAutoRunning()) {
return;
}

if (type == Controls.TYPE_INPUT_TYPE) {
onInputTypeSelected(event);
} else if (type == Controls.TYPE_SORTING) {
onSortingAlgorithmSelected(event);
Expand All @@ -93,17 +161,33 @@ public void setSidePanelEvents(SidePanel sidePanel) {

private void onInputTypeSelected(ControlEvent event) {
int index = (int) event.getValue();
String listItemName = InputType.INPUT_TYPES[index];
String inputType = InputType.INPUT_TYPES[index];

startInputType(inputType);
}

private void onSortingAlgorithmSelected(ControlEvent event) {
int index = (int) event.getValue();
String algorithm = SortingAlgorithms.SORTING_ALGORITHMS[index];

startAlgorithm(algorithm);
}

switch (listItemName) {
private void onButtonPressed(ControlEvent event) {
String buttonName = event.getName();

startButtonPressed(buttonName);
}

private void startInputType(String inputType) {
switch (inputType) {
case InputType.SHUFFLE:
if (sortingAlgorithm.isRunning()) {
return;
}

sortingAlgorithm = new ShuffleInput(pApplet, vector, color);
sortingAlgorithm.start();
System.out.println("Shuffle");
break;
case InputType.ASCENDING:
if (sortingAlgorithm.isRunning()) {
Expand Down Expand Up @@ -131,13 +215,11 @@ private void onInputTypeSelected(ControlEvent event) {
break;
}

sleep(10);
}

private void onSortingAlgorithmSelected(ControlEvent event) {
int index = (int) event.getValue();
String listItemName = SortingAlgorithms.SORTING_ALGORITHMS[index];

switch (listItemName) {
private void startAlgorithm(String algorithm) {
switch (algorithm) {
case SortingAlgorithms.BUBBLE_SORT:
if (sortingAlgorithm.isRunning()) {
return;
Expand Down Expand Up @@ -337,20 +419,19 @@ private void onSortingAlgorithmSelected(ControlEvent event) {
sortingAlgorithm = new IterativeMergeSort(pApplet, vector, color);
sortingAlgorithm.start();
break;
default:
break;
}
}

private void onButtonPressed(ControlEvent event) {
String buttonName = event.getName();
sleep(10);
}

private void startButtonPressed(String buttonName) {
switch (buttonName) {
case Controls.TOGGLE_SOUND:
Config.sound = !Config.sound;
break;
case Controls.STOP:
sortingAlgorithm.stop();
Config.setAutoRunning(false);
break;
case Controls.THEME:
Theme.currentTheme = Theme.currentTheme == Theme.LIGHT_THEME ?
Expand All @@ -360,6 +441,12 @@ private void onButtonPressed(ControlEvent event) {
break;
case Controls.PAUSE:
sortingAlgorithm.togglePause();
break;
case Controls.AUTO:
if (!Config.isAutoRunning()) {
loopAlgorithms();
}
break;
default:
break;
}
Expand All @@ -385,19 +472,18 @@ private void onSliderValueChanged(ControlEvent event) {
}
}


@Override
public void stop() {
super.stop();

sortingAlgorithm.stop();
/* UTIL */
private void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void mouseReleased() {
if (Config.arraySize != vector.length) {
resizeVector(Config.arraySize);
sortingAlgorithm = new Blank(pApplet, vector, color);
private void waitTillAlgorithmStops() {
while (sortingAlgorithm.isRunning()) {
sleep(200);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/window/main/sorting/SortingAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public SortingAlgorithm(PApplet pApplet, int[] vector, Color[] color, String alg

}

@Override
public abstract void run();

/* VECTOR CHANGING METHODS */
protected void swap(int i, int j) {
/* ANIMATION AND SOUND */
Expand Down Expand Up @@ -248,6 +251,10 @@ public void stop() {
stopSound();
}

public boolean isStopped() {
return stop;
}

public void togglePause() {
pause = !pause;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/window/side/SidePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,20 @@ private void initializeButtons() {
.setSize(buttonWidth, buttonHeight)
.setPosition(x + columnSize * 1 + padding + 10, y + rowSize * 12 + padding);

buttonControlIp5.addButton(Controls.AUTO)
.setSize(buttonWidth, buttonHeight)
.setPosition(x + columnSize * 0 + padding, y + rowSize * 14 + padding);

/* LISTS */
inputControlP5.addListBox("Input Type")
.setPosition(panelCenter - (int) (1.0 * listWidth / 2), y + rowSize * 15 + padding)
.setPosition(panelCenter - (int) (1.0 * listWidth / 2), y + rowSize * 16 + padding)
.setSize(listWidth, 4 * 15 + 5)
.setItemHeight(15)
.setBarHeight(20)
.addItems(InputType.INPUT_TYPES);

sortingControlIp5.addListBox("Sorting Algorithm")
.setPosition(panelCenter - (int) (1.0 * listWidth / 2), y + rowSize * 20 + padding)
.setPosition(panelCenter - (int) (1.0 * listWidth / 2), y + rowSize * 21 + padding)
.setSize(listWidth, 16 * 15 + 5)
.setItemHeight(15)
.setBarHeight(20)
Expand Down

0 comments on commit 0d29a22

Please sign in to comment.