LucasPilla / Sorting-Algorithms-Visualizer

Program made with Python and Pygame module for visualizing sorting algorithms
MIT License
418 stars 153 forks source link

Adding GIF output option #157

Closed thestar19 closed 1 year ago

thestar19 commented 1 year ago

This adds a feature for creating a GIF of the displayed sorting action. It adds a “checkbox” feature for outputting GIFs and uses a subfolder called “pictures” to dump a bunch of pictures which will later be input for GIF creation. If pictures exists, no folder will be created. Else, a new folder with the name "pictures" will be created in main folder.

This can be optimized by not writing any pictures and instead storing them in memory only as byte-objects. This may require either smaller sample pictures or fewer sample pictures. Currently, for sorting arrays with size less then or equal to 100, the GIF will be very accurate to what is displayed on screen. For any larger arrays, the GIF will not contain all displayed sorting actions. Because of this, the rendering of the GIF is quite slow. This could be much faster with fewer pictures, but this would lose accuracy. Changing the ratio on lines 174 to 177 will affect this speed. The folder pictures will become large if array size is large (and gif output is active), but never more than 300MB. As these files will be deleted shortly afterwards, this is not considered a problem.

The checkbox will move if the Size window increases. A bad aspect of the current implementation is that it will print any warning statements to the terminal. A better version would be some kind of “Working – please wait” indication on the pygame screen. I'm not a great artist, but two checkbox pictures are added. They are bad but can be improved by simply replacing the images in the folder.

Also, a new library in the form of “imageio” has been added to requirements.txt. It is fairly actively maintained so this should not be a problem, and makes it possible to add further functionality in the future. Implementing the GIF creating feature without some kind of library is deemed infeasible. In theory, the GIF could be created while the sort is rendered using imageio, but despite many attempts this was not successful. Any files created during running will be deleted afterwards, except sorting.gif. A good addition would be the option the place the file using a file dialog and changing the name.

A test of outputting GIF has been done with size 100, 200, 500 and 1000.