added an exit button, cleaned up the algorithm choice
This commit is contained in:
parent
41adfc2fa9
commit
d59ef79b96
4 changed files with 94 additions and 41 deletions
11
index.html
11
index.html
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>TITLE</title>
|
<title>BeepSort!</title>
|
||||||
<style>
|
<style>
|
||||||
html,
|
html,
|
||||||
body,
|
body,
|
||||||
|
@ -23,7 +23,16 @@
|
||||||
<canvas id="glcanvas" tabindex='1'></canvas>
|
<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 -->
|
<!-- 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 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 -->
|
<script>load("./BeepSortMacroQuad.wasm");</script> <!-- Your compiled wasm file -->
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
use crate::BarPlugin::Bar;
|
use crate::BarPlugin::Bar;
|
||||||
use crate::GuiHookVec::GuiVec;
|
use crate::GuiHookVec::GuiVec;
|
||||||
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::thread::yield_now;
|
|
||||||
use macroquad::prelude::screen_width;
|
use macroquad::prelude::screen_width;
|
||||||
use macroquad::window::screen_height;
|
use macroquad::window::screen_height;
|
||||||
use std::collections::BinaryHeap;
|
use std::collections::BinaryHeap;
|
||||||
|
@ -17,13 +15,31 @@ pub struct Algorithm{
|
||||||
|
|
||||||
impl 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);
|
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
|
||||||
list.randomize();
|
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(){
|
for index in 0..list.len(){
|
||||||
let mut j = index;
|
let mut j = index;
|
||||||
while j>0 && list.lessThan(j, j-1){
|
while j>0 && list.lessThan(j, j-1){
|
||||||
list.swap(j, j - 1).await;
|
if list.swap(j, j - 1).await {return};
|
||||||
j -= 1;
|
j -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,25 +65,22 @@ impl Algorithm{
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub async fn bubbleSort(length:i32, delay:f32){
|
pub async fn bubbleSort(list:&mut GuiVec){
|
||||||
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
|
|
||||||
list.randomize();
|
|
||||||
let n = list.len();
|
let n = list.len();
|
||||||
for i in 0..n {
|
for i in 0..n {
|
||||||
for j in 0..(n - i - 1) {
|
for j in 0..(n - i - 1) {
|
||||||
if list.lessThan(j + 1, j) {
|
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){
|
pub async fn bogoSort(list:&mut GuiVec){
|
||||||
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
|
|
||||||
list.randomize();
|
|
||||||
loop{
|
loop{
|
||||||
list.draw().await;
|
if list.swap(0,0).await {return};
|
||||||
if list.isSorted() {
|
if list.isSorted() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -75,9 +88,7 @@ impl Algorithm{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cocktailShaker(length:i32, delay:f32){
|
pub async fn cocktailShaker(list:&mut GuiVec){
|
||||||
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
|
|
||||||
list.randomize();
|
|
||||||
let mut lowerBound = 0;
|
let mut lowerBound = 0;
|
||||||
let mut upperBound = list.len()-1;
|
let mut upperBound = list.len()-1;
|
||||||
let mut swapped = true;
|
let mut swapped = true;
|
||||||
|
@ -85,7 +96,7 @@ impl Algorithm{
|
||||||
swapped = false;
|
swapped = false;
|
||||||
for i in lowerBound..upperBound {
|
for i in lowerBound..upperBound {
|
||||||
if list.lessThan(i+1, i) {
|
if list.lessThan(i+1, i) {
|
||||||
list.swap(i+1, i).await;
|
if list.swap(i+1, i).await {return};
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,7 +107,7 @@ impl Algorithm{
|
||||||
upperBound = upperBound-1;
|
upperBound = upperBound-1;
|
||||||
for i in ((lowerBound)..(upperBound-1)).rev() {
|
for i in ((lowerBound)..(upperBound-1)).rev() {
|
||||||
if list.lessThan(i+1, i) {
|
if list.lessThan(i+1, i) {
|
||||||
list.swap(i+1, i).await;
|
if list.swap(i+1, i).await {return};
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,11 +119,11 @@ impl Algorithm{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn binaryHeap(length:i32, delay:f32){
|
pub async fn binaryHeap(list:&mut GuiVec){
|
||||||
let mut list = GuiVec::new(screen_width(), screen_height(), length, delay);
|
|
||||||
let mut indexMap:HashMap<i32, usize> = HashMap::new();
|
let mut indexMap:HashMap<i32, usize> = HashMap::new();
|
||||||
let mut binHeap:BinaryHeap<i32> = BinaryHeap::new();
|
let mut binHeap:BinaryHeap<i32> = BinaryHeap::new();
|
||||||
list.randomize();
|
|
||||||
let mut ind = 0;
|
let mut ind = 0;
|
||||||
for bar in list.elements(){
|
for bar in list.elements(){
|
||||||
binHeap.push(bar.position);
|
binHeap.push(bar.position);
|
||||||
|
@ -122,7 +133,7 @@ impl Algorithm{
|
||||||
for i in (0..list.len()).rev(){
|
for i in (0..list.len()).rev(){
|
||||||
let bar = binHeap.pop().unwrap();
|
let bar = binHeap.pop().unwrap();
|
||||||
let barIndex = *indexMap.get(&bar).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;
|
let temp = list.get(barIndex).position;
|
||||||
indexMap.insert(temp, barIndex);
|
indexMap.insert(temp, barIndex);
|
||||||
|
|
||||||
|
|
|
@ -26,18 +26,32 @@ pub struct GuiVec{
|
||||||
screen_height:f32,
|
screen_height:f32,
|
||||||
screen_width:f32,
|
screen_width:f32,
|
||||||
isPaused:bool,
|
isPaused:bool,
|
||||||
delay:f32
|
delay:f32,
|
||||||
|
pub done:bool
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GuiVec{
|
impl GuiVec{
|
||||||
|
|
||||||
pub fn new(screen_width:f32, screen_height:f32,length:i32, delay:f32) -> Self {
|
pub fn new(screen_width:f32, screen_height:f32,length:i32, delay:f32) -> Self {
|
||||||
let colorStep = 360./length as f32;
|
let colorStep = 360./length as f32;
|
||||||
let mut list:Vec<Bar> = vec!();
|
let mut list:Vec<Bar> = vec!();
|
||||||
for i in 1..length+1 {
|
for i in 1..length+1 {
|
||||||
list.push(Bar::new(i, (colorStep*i as f32)/360.));
|
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){
|
pub async fn draw(&mut self){
|
||||||
|
@ -45,6 +59,7 @@ impl GuiVec{
|
||||||
let mut delayText = self.delay.to_string();
|
let mut delayText = self.delay.to_string();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
self.checkPaused();
|
self.checkPaused();
|
||||||
if self.isPaused{
|
if self.isPaused{
|
||||||
|
@ -54,15 +69,23 @@ impl GuiVec{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
clear_background(WHITE);
|
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);
|
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|{
|
root_ui().window(hash!(), Vec2::new(screen_width()*0.2, 5.), Vec2::new(200., 25.), |ui|{
|
||||||
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
|
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>(){
|
self.delay = match delayText.parse::<f32>(){
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(_)=> {f32::MAX}
|
Err(_)=> {f32::MAX}
|
||||||
|
@ -71,7 +94,7 @@ impl GuiVec{
|
||||||
|
|
||||||
next_frame().await;
|
next_frame().await;
|
||||||
|
|
||||||
if frames >= self.delay/10000.0{
|
if frames >= self.delay/10000.0 && !self.done{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
frames += get_frame_time();
|
frames += get_frame_time();
|
||||||
|
@ -110,11 +133,12 @@ impl GuiVec{
|
||||||
self.initialSize -= 1;
|
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.writes += 2;
|
||||||
self.reads += 2;
|
self.reads += 2;
|
||||||
self.list.swap(index1, index2);
|
self.list.swap(index1, index2);
|
||||||
self.draw().await;
|
self.draw().await;
|
||||||
|
self.done
|
||||||
}
|
}
|
||||||
pub fn randomize(&mut self){
|
pub fn randomize(&mut self){
|
||||||
self.list.shuffle();
|
self.list.shuffle();
|
||||||
|
@ -144,5 +168,15 @@ impl GuiVec{
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
pub async fn show(&mut self){
|
||||||
|
loop{
|
||||||
|
if !self.done{
|
||||||
|
self.draw().await
|
||||||
|
}else{
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -37,26 +37,25 @@ async fn main() {
|
||||||
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
|
ui.input_text(hash!(), "Delay (ms)", &mut delayText);
|
||||||
ui.input_text(hash!(), "Length Of Array!", &mut lengthString);
|
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!"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "Run InsertSort!"){
|
||||||
//State::State::runInsertSort(delay,length).await;
|
algo = "insertSort";
|
||||||
Algorithm::Algorithm::insertSort(length, 1.0).await;
|
|
||||||
}
|
}
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "Run BubbleSort!"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "Run BubbleSort!"){
|
||||||
//State::State::runInsertSort(delay,length).await;
|
algo = "bubbleSort";
|
||||||
Algorithm::Algorithm::bubbleSort(length, 1.0).await;
|
|
||||||
}
|
}
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "Run BinaryHeapSort!"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "Run BinaryHeapSort!"){
|
||||||
//State::State::runInsertSort(delay,length).await;
|
algo = "binaryHeap";
|
||||||
Algorithm::Algorithm::binaryHeap(length, 1.0).await;
|
|
||||||
}
|
}
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "Run CoctailShakerSort!"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "Run CoctailShakerSort!"){
|
||||||
//State::State::runInsertSort(delay,length).await;
|
algo = "cocktailShaker";
|
||||||
Algorithm::Algorithm::cocktailShaker(length, 1.0).await;
|
|
||||||
}
|
}
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "Run BogoSort!"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "Run BogoSort!"){
|
||||||
//State::State::runInsertSort(delay,length).await;
|
algo = "bogoSort";
|
||||||
Algorithm::Algorithm::bogoSort(length, 1.0).await;
|
}
|
||||||
|
|
||||||
|
if algo != ""{
|
||||||
|
Algorithm::Algorithm::run(length, 1.0, algo.to_string()).await;
|
||||||
}
|
}
|
||||||
next_frame().await
|
next_frame().await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue