changeset 32:b1a605eb721a

accept user inputs
author Dennis C. M. <dennis@denniscm.com>
date Thu, 29 Jun 2023 19:45:49 +0100
parents 61104b22a25d
children 576431310c8a
files README.md repo/1.png repo/2.png repo/3.png repo/Screencast from 06-29-2023 07:36:54 PM.webm repo/Screencast from 06-29-2023 07:38:33 PM.webm repo/Screencast from 06-29-2023 07:39:20 PM.webm repo/Screencast from 06-29-2023 07:41:04 PM.webm src/main.c
diffstat 9 files changed, 97 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Thu Jun 29 18:05:22 2023 +0100
+++ b/README.md	Thu Jun 29 19:45:49 2023 +0100
@@ -16,5 +16,55 @@
 cd build
 cmake ..
 make
-./algo_animator
+```
+
+**Currently I've only tested this project on Linux. However, I would like to prepare it so 
+that it can be used on Mac and Windows. However, my cross-platform experience is almost 
+non-existent. Maybe in the future I'll check how to do it**
+
+# Run
+
+To customize the program you can pass the following arguments. 
+
+```bash
+./algo_animator WINDOW_WIDTH WINDOW_HEIGHT RECTANGLE_WIDTH SPACE_BETWEEN_RECTANGLES
+```
+
+The default values are:
+
+```bash
+WINDOW_WIDTH = 1920
+WINDOW_HEIGHT = 1080
+RECTANGLE_WIDTH = 5
+SPACE_BETWEEN_RECTANGLES = 1
 ```
+
+Run the program in a window with width of 1920 pixels and height of 1080 pixels, 
+fit the screen with rectangles with width of 50 pixels, and add a space between
+rectangles of 3 pixels.
+
+```bash
+./algo_animator 1920 1080 50 3
+```
+
+![Image 3](repo/3.png)
+
+
+# Examples
+
+## Normal mode
+
+Just run the program and press `ENTER`
+
+## Slow motion
+
+Use `u` to increase the delay, then `ENTER` to run the algorithm   
+**The delay is applied after each iteration**
+
+## Pause mode
+
+Press `ENTER` then `p`
+
+## Sequential mode
+
+Press `q` then press `ENTER` to visualize the algorithm step by step.
Binary file repo/1.png has changed
Binary file repo/2.png has changed
Binary file repo/3.png has changed
Binary file repo/Screencast from 06-29-2023 07:36:54 PM.webm has changed
Binary file repo/Screencast from 06-29-2023 07:38:33 PM.webm has changed
Binary file repo/Screencast from 06-29-2023 07:39:20 PM.webm has changed
Binary file repo/Screencast from 06-29-2023 07:41:04 PM.webm has changed
--- a/src/main.c	Thu Jun 29 18:05:22 2023 +0100
+++ b/src/main.c	Thu Jun 29 19:45:49 2023 +0100
@@ -4,7 +4,7 @@
 int window_width = 1920;
 int window_height = 1080;
 int vpadding = 150;
-int rect_width = 30;
+int rect_width = 5;
 int space = 1;
 
 struct Algo algos[2];
@@ -248,7 +248,49 @@
 }
 
 
-int main(int argc, char** argv) {
+int main(int argc, char *argv[]) {
+	if (argc - 1 == 4) {
+		int args[4];
+
+		printf("Values provided: ");	
+		
+		for (int i = 1; i < argc; i++) {
+			printf("%s ", argv[i]);
+
+			char* end_ptr;
+			long value = strtol(argv[i], &end_ptr, 10);
+
+			if (end_ptr == argv[i]) {
+				printf("Invalid argument\n");
+				exit(1);
+			}
+
+			if ((value == LONG_MAX || value == LONG_MIN)) {
+				printf("Integer out of range\n");
+				exit(1);
+			}
+
+			args[i - 1] = (int)value;
+		}
+
+		printf("\n");
+
+		printf("Window width: %i\n", args[0]);
+		window_width = args[0];
+
+		printf("Window height: %i\n", args[1]);
+		window_height = args[1];
+
+		printf("Rectangle width: %i\n", args[2]);
+		rect_width = args[2];
+
+		printf("Space: %i\n", args[3]);
+		space = args[3];
+
+	} else {
+		printf("Using default values\n");
+	}
+
 	algo_args.arr_size = window_width / (rect_width + space);
 	algo_args.arr = malloc(algo_args.arr_size * sizeof(struct Element));
 	algo_args.comparisons = 0;
@@ -258,7 +300,7 @@
 
 	strcpy(algos[0].name, "Bubble sort");
 	algos[0].function = bubble_sort;
-	
+
 	strcpy(algos[1].name, "Selection sort");
 	algos[1].function = selection_sort;
 
@@ -270,7 +312,7 @@
 	glutInit(&argc, argv);
 	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
 	glutInitWindowSize(window_width, window_height);
-	glutCreateWindow("Algorithm animator");
+	glutCreateWindow("Visualization of sorting algorithms | Developed by Dennis CM");
 
 	setup_gl();
 	setup_freetype();