fixed graph rendering problems
This commit is contained in:
parent
d529a1c184
commit
759935c3c6
5 changed files with 71 additions and 19 deletions
|
@ -85,4 +85,38 @@ impl Algorithm{
|
|||
}
|
||||
}
|
||||
|
||||
pub fn cocktailShaker(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ impl GuiVec{
|
|||
let barHeightStep = (screen_height/((length) as f32));
|
||||
let mut list:Vec<Bar> = 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;
|
||||
}
|
||||
}
|
||||
|
|
17
src/State.rs
17
src/State.rs
|
@ -12,14 +12,13 @@ pub struct State{
|
|||
}
|
||||
|
||||
impl State{
|
||||
pub async fn runInsertSort(length:i32, mut generator:impl Generator<Yield=GuiVec, Return=()>+ std::marker::Unpin){
|
||||
pub async fn runInsertSort(timeout: f64, length:i32, mut generator:impl Generator<Yield=GuiVec, Return=()>+ 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
|
||||
}
|
||||
}
|
||||
|
|
29
src/main.rs
29
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue