From 759935c3c60c17f6eb71dd7bc4463c683d87e2cb Mon Sep 17 00:00:00 2001 From: polsevev Date: Tue, 8 Nov 2022 00:56:06 +0100 Subject: [PATCH] fixed graph rendering problems --- src/Algorithm.rs | 34 ++++++++++++++++++++++++++++++++++ src/BarPlugin.rs | 6 +----- src/GuiHookVec.rs | 4 ++-- src/State.rs | 17 ++++++++++++----- src/main.rs | 29 ++++++++++++++++++++++------- 5 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/Algorithm.rs b/src/Algorithm.rs index d4329aa..d053d9d 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -85,4 +85,38 @@ impl Algorithm{ } } + pub fn cocktailShaker(length:i32) -> impl Generator{ + let mut list = GuiVec::new(screen_width(), screen_height(), length); + list.randomize(); + move || { + let mut lowerBound = 0; + let mut upperBound = list.len()-1; + let mut swapped = true; + while swapped{ + swapped = false; + for i in lowerBound..upperBound { + if list.lessThan(i+1, i) { + yield list.swap(i+1, i); + swapped = true; + } + } + if !swapped{ + break; + } + swapped = false; + upperBound = upperBound-1; + for i in ((lowerBound)..(upperBound-1)).rev() { + if list.lessThan(i+1, i) { + yield list.swap(i+1, i); + swapped = true; + } + } + + lowerBound = lowerBound+1; + } + + } + + } + } \ No newline at end of file diff --git a/src/BarPlugin.rs b/src/BarPlugin.rs index 3d9bc63..cd7183e 100644 --- a/src/BarPlugin.rs +++ b/src/BarPlugin.rs @@ -4,8 +4,6 @@ use macroquad::rand; #[derive(Debug, Clone)] pub struct Bar { - pub width:f32, - pub height:f32, pub position:i32, pub color:Color } @@ -13,11 +11,9 @@ pub struct Bar { impl Bar{ - pub fn new(width:f32, height: f32,position:i32) -> Self{ + pub fn new(position:i32) -> Self{ Bar{ - width: width, - height: height, position, color:Color::from_rgba(rand::gen_range(0, 255),rand::gen_range(0, 254),rand::gen_range(0, 255),255), } diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index 07a09f8..fe1c6be 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -30,7 +30,7 @@ impl GuiVec{ let barHeightStep = (screen_height/((length) as f32)); let mut list:Vec = vec!(); for i in 1..length+1 { - list.push(Bar::new(barWidth, barHeightStep*(i as f32) ,i)); + list.push(Bar::new(i)); } GuiVec{list, initialSize:length as usize, lastTime: 0.0 , reads:0, writes:0, comps:0, screen_height, screen_width} } @@ -38,7 +38,7 @@ impl GuiVec{ pub fn draw(&self){ let mut count = 0; for bar in &self.list{ - draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)), (screen_height()-bar.height)-50., screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32 * 4., 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; } } diff --git a/src/State.rs b/src/State.rs index 3f9cb16..d022207 100644 --- a/src/State.rs +++ b/src/State.rs @@ -12,14 +12,13 @@ pub struct State{ } impl State{ - pub async fn runInsertSort(length:i32, mut generator:impl Generator+ std::marker::Unpin){ + pub async fn runInsertSort(timeout: f64, length:i32, mut generator:impl Generator+ std::marker::Unpin){ let mut finished = false; - let mut paused = false; - let timeout = 0.0; + let mut ret = false; let mut lasttime:f64 = 0.; let mut holder = GuiVec::new(screen_width(), screen_height(), length); - while !finished{ + loop{ clear_background(WHITE); if get_time()-lasttime > timeout && !finished && !paused{ match Pin::new(& mut generator).resume(()){ @@ -28,6 +27,7 @@ impl State{ }, GeneratorState::Complete(x) => { finished = true; + paused = true; } }; lasttime = get_time(); @@ -38,13 +38,20 @@ impl State{ draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.01, 60.0, 20.0, BLACK); draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.01, 80., 20.0, BLACK); - if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){ + if root_ui().button(Vec2::new(screen_width()*0.01, 90.), "Pause"){ if paused { paused = false; }else{ paused = true; } } + if root_ui().button(Vec2::new(screen_width()*0.01, 110.), "Return"){ + ret = true; + } + if (finished && ret) || ret { + break; + } + next_frame().await } } diff --git a/src/main.rs b/src/main.rs index a612516..b439940 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use crate::BarPlugin::Bar; use crate::GuiHookVec::GuiVec; use std::ops::{Deref, Generator, GeneratorState}; +use std::process::id; use macroquad::ui::root_ui; use crate::Algorithm::AlgoEnum; @@ -20,27 +21,41 @@ use crate::Algorithm::AlgoEnum; async fn main() { let mut length = 100; + let mut delay = 0.01; + loop{ clear_background(WHITE); - if root_ui().button(Vec2::new(screen_width()*0.2, 50.), "Increase"){ + draw_text(format!("Length: {}", length.to_string()).as_str(), screen_width()*0.83, 30., 20.0, BLACK); + if root_ui().button(Vec2::new(screen_width()*0.9, 50.), "+10"){ length += 10; } - if root_ui().button(Vec2::new(screen_width()*0.3, 50.), "Decrease"){ + if root_ui().button(Vec2::new(screen_width()*0.87, 50.), "+1"){ + length += 1; + } + if root_ui().button(Vec2::new(screen_width()*0.8, 50.), "-10"){ length -= 10; } - draw_text(format!("Length: {}", length.to_string()).as_str(), screen_width()*0.01, 50., 20.0, BLACK); + if root_ui().button(Vec2::new(screen_width()*0.84, 50.), "-1"){ + length -= 1; + } + + if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "InsertSort"){ - State::State::runInsertSort(length, Algorithm::Algorithm::insertSort(length)).await; + State::State::runInsertSort(delay,length, Algorithm::Algorithm::insertSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "BogoSort"){ - State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + State::State::runInsertSort(delay,length, Algorithm::Algorithm::bogoSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "BubbleSort"){ - State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + State::State::runInsertSort(delay, length, Algorithm::Algorithm::bubbleSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "StalinSort"){ - State::State::runInsertSort(length, Algorithm::Algorithm::bogoSort(length)).await; + State::State::runInsertSort(delay, length, Algorithm::Algorithm::stalinSort(length)).await; } + if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "CoctailShaker"){ + State::State::runInsertSort(delay, length, Algorithm::Algorithm::cocktailShaker(length)).await; + } + next_frame().await }