why this doesn't draw??!

edited February 2016 in Questions about Code

This supose to draw a graf of how the buble sort arrange the bars but sometimes only shows the finsh The file "list.txt" is a list of 200 unorder numbers

BufferedReader reader;
int numbers[] = new int[200];
int nof_bars;
boolean start = true;
long lastTime = 0;
void setup()
{
  size(900,700);
  create_list();
  for (int i=0; i <  numbers.length;i++)
    println(numbers[i]);
  nof_bars = numbers.length;
}
void draw()
{
  if (start)
  {
    for(int i=0;i<numbers.length;i++)
     for(int j=i;j<numbers.length;j++)
     {
       int memo =0;
       if (numbers[i]<numbers[j])
       {
         memo = numbers[j];
         numbers[j] = numbers[i];
         numbers[i] = memo;
         background(0);
         graf();
       }
     }
    start = !start;
  }
}
void graf()
{
  for(int i =0; i < numbers.length; i++)
    rect(((width)/nof_bars)*i,height,(width)/nof_bars,-1*numbers[i]);
}
void create_list()
{
  String[] list = loadStrings("list.txt"); 
  for (int i=0; i < list.length;i++)
    numbers[i] = int(list[i]);
}

Answers

  • You'll get a better response if you format your code. Here's how:

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • The display only updates when draw () finishes.

  • This is by design.

  • for all kind of recursions it would be cool to be able to update the screen throughout somehow.

    question: would that be possible using eclipse or JAVA or another workaround?

  • Just a thought, not sure if it works but would it be a good idea to do the bubble sort in a separate method you launch in setup() and use noloop() and redraw() in each step?

  • edited February 2016

    No, redraw() also just lets draw() update the screen at the end - I guess

    The naive approach is :

    • call sorting from setup()

    • copy the full list in an ArrayList at each step

    • in draw() replay the ArrayList to show each step throughout

    but this is ugly....

  • edited February 2016

    I don't get why the "need" to implement our own sorting algorithm while Java already got sort() for both regular arrays & Collection containers: :O)

    1. http://docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-T:A-java.util.Comparator-
    2. http://docs.Oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-

    It needs a Comparator parameter in case the elements to sort() aren't numbers, strings or don't implement its own Comparable:
    http://docs.Oracle.com/javase/8/docs/api/java/lang/Comparable.html

    But for something as simple as an int[] array, Processing offers sort() too: *-:)
    https://Processing.org/reference/sort_.html

    Also Processing's own containers like IntList, FloatDict, etc., features sorting methods too! :-bd

  • A bit ugly...but seems to work.
    Draw histogram step by step pressing a key.

    BufferedReader reader;
    int numbers[] = new int[200];
    int nof_bars;
    boolean start = true;
    long lastTime = 0;
    
    boolean ii, jj;
    int i, j;
    boolean run;
    
    void setup()
    {
      size(400,400);
      //create_list();
      nof_bars = 10;
      for (int i=0; i < nof_bars; i++)
        numbers[i] = (int) random(10,100);
      i = -1;
      j = 0;
      run = true;
      jj = false;
    }
    void draw()
    {
      if (run&&keyPressed)
      {
        run = false;
        if (start)
        {
          if (i < nof_bars)
          {
            if (jj == false) j = i;
            println("i=" + i + " j=" + j + " jj=" + jj);        
            if (j < nof_bars)
            {
              jj = true;
              int memo = 0;
              if (numbers[i]<numbers[j])
              {
                memo = numbers[j];
                numbers[j] = numbers[i];
                numbers[i] = memo;
                //background(0);
                //graf();
              }
            }
            else
            {
              jj = false;
            }
          }
        }
        if (i == nof_bars) start = !start;
        background(0);
        graf();      
      }
    }
    
    void graf()
    {
      for(int k = 0; k < nof_bars; k++)
      {
        fill(255);
        if (k == i) fill(255,0,0);
        if (k == j) fill(0,255,0);
        rect(((width)/nof_bars)*k,height,(width)/nof_bars,-1*numbers[k]);
      }  
    }
    
    void create_list()
    {
      String[] list = loadStrings("list.txt"); 
      for (int i=0; i < list.length;i++)
        numbers[i] = int(list[i]);  
    }
    
    void keyPressed()
    {
      if (jj == false)
      {
        i = i + 1;
      }
      else
      {
        j = j + 1;
      }
      println("K --> i=" + i + " j=" + j + " jj=" + jj);
      run = true;  
    }  
    
  • Yes, my noLoop() idea won't work as setup() will wait until the sort method is finished before moving on to draw.

    Here's my solution:

    BufferedReader reader;
    int numbers[] = new int[200];
    int nof_bars;
    boolean start = true;
    long lastTime = 0;
    int i = 0;
    void setup()
    {
      size(900, 700);
      create_list();
      //for (int i=0; i <  numbers.length;i++)
      // println(numbers[i]);
      nof_bars = numbers.length;
    }
    void draw()
    {
      if (start && i < numbers.length)
      {
        //Where did the i for loop go? It's gone!
        //I replaced it with the draw loop: now each draw() iteration has the same effect as one iteration in the old i for loop
    
        for (int j=i; j<numbers.length; j++)
        {
          int memo =0;
          if (numbers[i]<numbers[j])
          {
            memo = numbers[j];
            numbers[j] = numbers[i];
            numbers[i] = memo;
          }
        }
        background(0);
        graf();
        i++;
      }
    }
    void graf()
    {
      for (int i =0; i < numbers.length; i++)
        rect(((width)/nof_bars)*i, height, (width)/nof_bars, -1*numbers[i]);
    }
    void create_list()
    {
      //I don't have your text file so I just fill numbers[] with random values
      for (int i=0; i < numbers.length; i++)
        numbers[i] = (int) random(height);
    }
    
  • question: would that be possible using eclipse or JAVA or another workaround?

  • I mean these solutions won't work with recursions anyway

  • I don't get why the "need" to implement our own sorting algorithm while Java already got sort()

    learning exercise.

  • edited February 2016

    @Chrisir: with recursions is impossible ( almost for me :) ).
    Your solution with ArrayList should work.

Sign In or Register to comment.