added an exit button, cleaned up the algorithm choice

This commit is contained in:
Rolf Martin Glomsrud 2023-02-26 23:25:41 +01:00
parent 41adfc2fa9
commit d59ef79b96
4 changed files with 94 additions and 41 deletions

View file

@ -2,7 +2,7 @@
<head>
<meta charset="utf-8">
<title>TITLE</title>
<title>BeepSort!</title>
<style>
html,
body,
@ -23,7 +23,16 @@
<canvas id="glcanvas" tabindex='1'></canvas>
<!-- Minified and statically hosted version of https://github.com/not-fl3/macroquad/blob/master/js/mq_js_bundle.js -->
<script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
<script>
register_plugin = function (importObject) {
importObject.env.hi_from_wasm = function (js_object) {
console.log("hi")
}
}
miniquad_add_plugin({register_plugin});
</script>
<script>load("./BeepSortMacroQuad.wasm");</script> <!-- Your compiled wasm file -->
</body>
</html>

View file

@ -2,9 +2,7 @@
use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec;
use std::pin::Pin;
use std::rc::Rc;
use std::thread::yield_now;
use macroquad::prelude::screen_width;
use macroquad::window::screen_height;
use std::collections::BinaryHeap;
@ -17,13 +15,31 @@ pub struct Algorithm{
impl Algorithm{
pub async fn insertSort(length:i32, delay:f32){
pub async fn run(length:i32, delay:f32, functionName:String){
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
list.randomize();
match functionName.as_str() {
"insertSort" => Algorithm::insertSort(&mut list).await,
"bubbleSort" => Algorithm::bubbleSort(&mut list).await,
"bogoSort" => Algorithm::bogoSort(&mut list).await,
"cocktailShaker" => Algorithm::cocktailShaker(&mut list).await,
"binaryHeap" => Algorithm::binaryHeap(&mut list).await,
_ => panic!("No algorithm with that name implemented!")
}
if !list.done{
list.show().await
}
}
pub async fn insertSort(list:&mut GuiVec){
for index in 0..list.len(){
let mut j = index;
while j>0 && list.lessThan(j, j-1){
list.swap(j, j - 1).await;
if list.swap(j, j - 1).await {return};
j -= 1;
}
}
@ -49,25 +65,22 @@ impl Algorithm{
}
*/
pub async fn bubbleSort(length:i32, delay:f32){
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
list.randomize();
pub async fn bubbleSort(list:&mut GuiVec){
let n = list.len();
for i in 0..n {
for j in 0..(n - i - 1) {
if list.lessThan(j + 1, j) {
list.swap(j, j + 1).await;
if list.swap(j, j + 1).await {return};
}
}
}
}
pub async fn bogoSort(length:i32, delay:f32){
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
list.randomize();
pub async fn bogoSort(list:&mut GuiVec){
loop{
list.draw().await;
if list.swap(0,0).await {return};
if list.isSorted() {
break;
}
@ -75,9 +88,7 @@ impl Algorithm{
}
}
pub async fn cocktailShaker(length:i32, delay:f32){
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
list.randomize();
pub async fn cocktailShaker(list:&mut GuiVec){
let mut lowerBound = 0;
let mut upperBound = list.len()-1;
let mut swapped = true;
@ -85,7 +96,7 @@ impl Algorithm{
swapped = false;
for i in lowerBound..upperBound {
if list.lessThan(i+1, i) {
list.swap(i+1, i).await;
if list.swap(i+1, i).await {return};
swapped = true;
}
}
@ -96,7 +107,7 @@ impl Algorithm{
upperBound = upperBound-1;
for i in ((lowerBound)..(upperBound-1)).rev() {
if list.lessThan(i+1, i) {
list.swap(i+1, i).await;
if list.swap(i+1, i).await {return};
swapped = true;
}
}
@ -108,11 +119,11 @@ impl Algorithm{
}
pub async fn binaryHeap(length:i32, delay:f32){
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
pub async fn binaryHeap(list:&mut GuiVec){
let mut indexMap:HashMap<i32, usize> = HashMap::new();
let mut binHeap:BinaryHeap<i32> = BinaryHeap::new();
list.randomize();
let mut ind = 0;
for bar in list.elements(){
binHeap.push(bar.position);
@ -122,7 +133,7 @@ impl Algorithm{
for i in (0..list.len()).rev(){
let bar = binHeap.pop().unwrap();
let barIndex = *indexMap.get(&bar).unwrap();
list.swap(i, barIndex).await;
if list.swap(i, barIndex).await {return};
let temp = list.get(barIndex).position;
indexMap.insert(temp, barIndex);

View file

@ -26,18 +26,32 @@ pub struct GuiVec{
screen_height:f32,
screen_width:f32,
isPaused:bool,
delay:f32
delay:f32,
pub done:bool
}
impl GuiVec{
pub fn new(screen_width:f32, screen_height:f32,length:i32, delay:f32) -> Self {
let colorStep = 360./length as f32;
let mut list:Vec<Bar> = vec!();
for i in 1..length+1 {
list.push(Bar::new(i, (colorStep*i as f32)/360.));
}
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , reads:0, writes:0, comps:0, screen_height, screen_width, isPaused:false, delay}
GuiVec{
list,
initialSize:length as usize,
lastTime: 0.0 ,
reads:0,
writes:0,
comps:0,
screen_height,
screen_width,
isPaused:false,
delay,
done:false
}
}
pub async fn draw(&mut self){
@ -45,6 +59,7 @@ impl GuiVec{
let mut delayText = self.delay.to_string();
loop {
/*
self.checkPaused();
if self.isPaused{
@ -54,15 +69,23 @@ impl GuiVec{
*/
clear_background(WHITE);
let mut count = 0;
for bar in &self.list{
for (count,bar) in self.list.iter().enumerate(){
draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)),screen_height() - (screen_height()/((self.len()) as f32))*bar.position as f32 , screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32, bar.color);
count += 1;
}
root_ui().window(hash!(), Vec2::new(screen_width()*0.2, 5.), Vec2::new(200., 25.), |ui|{
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
});
if root_ui().button(Vec2::new(screen_width()*0.2, 25.), "Exit"){
self.done = true;
break;
}
self.delay = match delayText.parse::<f32>(){
Ok(a) => a,
Err(_)=> {f32::MAX}
@ -71,7 +94,7 @@ impl GuiVec{
next_frame().await;
if frames >= self.delay/10000.0{
if frames >= self.delay/10000.0 && !self.done{
break;
}
frames += get_frame_time();
@ -110,11 +133,12 @@ impl GuiVec{
self.initialSize -= 1;
}
pub async fn swap(&mut self, index1:usize, index2:usize){
pub async fn swap(&mut self, index1:usize, index2:usize) -> bool{
self.writes += 2;
self.reads += 2;
self.list.swap(index1, index2);
self.draw().await;
self.done
}
pub fn randomize(&mut self){
self.list.shuffle();
@ -144,5 +168,15 @@ impl GuiVec{
}
true
}
pub async fn show(&mut self){
loop{
if !self.done{
self.draw().await
}else{
break
}
}
}
}

View file

@ -37,26 +37,25 @@ async fn main() {
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
ui.input_text(hash!(), "Length Of Array!", &mut lengthString);
});
let mut algo = "";
if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "Run InsertSort!"){
//State::State::runInsertSort(delay,length).await;
Algorithm::Algorithm::insertSort(length, 1.0).await;
algo = "insertSort";
}
if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "Run BubbleSort!"){
//State::State::runInsertSort(delay,length).await;
Algorithm::Algorithm::bubbleSort(length, 1.0).await;
algo = "bubbleSort";
}
if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "Run BinaryHeapSort!"){
//State::State::runInsertSort(delay,length).await;
Algorithm::Algorithm::binaryHeap(length, 1.0).await;
algo = "binaryHeap";
}
if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "Run CoctailShakerSort!"){
//State::State::runInsertSort(delay,length).await;
Algorithm::Algorithm::cocktailShaker(length, 1.0).await;
algo = "cocktailShaker";
}
if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "Run BogoSort!"){
//State::State::runInsertSort(delay,length).await;
Algorithm::Algorithm::bogoSort(length, 1.0).await;
algo = "bogoSort";
}
if algo != ""{
Algorithm::Algorithm::run(length, 1.0, algo.to_string()).await;
}
next_frame().await
}