QScript: Fibonacci

edited October 2015 in Library Questions

Hello,

is somebody very bored and willing to write a Fibonacci generator in QScript....?

Thanks!

Chrisir !

Tagged:

Answers

  • The QScriptIDE example that comes with the library will do that for you ;;)

  • damn...

    I am overworked....

    and underpayed

    sorry....

  • Answer ✓

    No Problem.

  • ##Description 
    This sketch displays the first few terms in the Fibonacci series. Change the value in the first line to set the number of terms to display.
    ##EndDescription 
    ##Code
    nbrTerms = 12
    n1 = 1; n2 = 1 ; count = 2;
    print(n1 + ' ' + n2 + ' ')
    WHILE(nbrTerms > 2)
      next = n1 + n2
      print(next + ' ') 
      count = count + 1
      IF(count % 10 == 0) 
        println('')
      ENDIF
      n1 = n2; n2 = next
      nbrTerms = nbrTerms - 1
    WEND
    println(''); END(next)
    ##EndCode
    
  • edited October 2015

    I couldn't run the QScript IDE because processing 2.2.1 crashed with some awt error.

    Maybe because I already installed processing 3.0 and they interfere?

    Is QScript for processing 3.0 out already...?

    ;-)

  • Answer ✓

    QScriptIDE uses G4P and there are 2 versions for PS2 and PS3 so that is the conflict. I suggest you have separate sketch folders for PS2 and PS3 then the right versions of the libraries will be installed.

  • edited November 2015

    Dear @quark

    I am still on it.

    I now try to run your QScript Fibonacci-Code (see above).

    To do so I tried to make a basic small sketch to run the QScript Fibonacci-Code. I extracted the relevant lines from your exampel sketch QScript IDE. But it doesn't show results (see below).

    I have to say that it seems to me that QScript promises more than it can keep:

    • It promises that you can run any QScript within a hosting processing sketch

    • but in reality you have to make a lot of adjustements in the hosting processing sketch to make the QScript code really run

    How would a hosting processing sketch look like that could run any QScript code ?

    Best, Chrisir ;-)

    // test for Fibonacci 
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    import java.util.*;
    
    // ---------------------------------------------
    
    boolean firstTime = true;  
    
    Script script;
    
    String[] algor; 
    
    String algorText;
    // boolean showAlgor = false;
    
    final int OFFSET_TOP = 20;
    final int OFFSET_LEFT = 50;
    
    // Selection box data
    boolean selecting = false;
    
    int x0, y0, x1, y1;
    boolean validSelection;
    
    final int nbrColors = 32;
    final int resolution = 1;
    int[] colors = new int[nbrColors +1];
    
    LinkedList<ScriptEvent> event_queue = new LinkedList<ScriptEvent>();
    
    /// -------------------------------------------------------
    
    public void setup() {
      size(500, 550);
    
      algor = loadStrings("fibonacci.txt");
      algorText = PApplet.join(algor, "\n");
    
      printArray(algor);
    
      script = new Script(algor);
      script.setCode(algor);
    
      textAlign(LEFT, TOP);
    
      // Scripting events are to be sent to this object
      script.addListener(this);
      println("end of setup()  ----------------------------------------");
    }
    
    public void draw() {
      background(48);
      noStroke();
      fill(255); 
      text("we are in draw()", 14, 14); 
    
      if (firstTime) { 
        // Solver$.evaluate(script);
        firstTime=false;
      } // if 
    
      Solver$.evaluate(script);
      // script.parse();
    } // func 
    
    // =============================================================
    
    /**
     * Since events are fired asynchronously this method might be called whilst G4P
     * is drawing its controls causing the program to crash. To avoid this we will
     * add the event to a queue and process it during Processing's event loop.
     */
    @EventHandler
      public void onScriptEvent(ScriptEvent event) {
      if (event instanceof HaltExecutionEvent && event.etype == ErrorType.STOPPED_BY_USER)
        event_queue.addFirst(event);
      else
        event_queue.addLast(event);
    }
    
    /**
     * This method has been registered with Processing so will be called just 
     * before the draw method. It will process a maximum of 20 events in the FIFO
     * queue, then allow the draw method to execute.
     * Since the script can generate hundreds of events per frame we have to
     * cap the number processed if we want the GUI to be responsive.
     */
    public void pre() {
      int count = 0;
      while (!event_queue.isEmpty () && count++ < 20)
        performEventAction(event_queue.removeFirst());
    }
    
    /**
     * This method performs the actions needed by any particular type of script 
     * event. 
     */
    public void performEventAction(ScriptEvent event) {
      if (event instanceof TraceEvent && false) {
        //
      } else if (event instanceof SyntaxErrorEvent || event instanceof EvaluationErrorEvent) {
        //
      } else if (event instanceof HaltExecutionEvent) {
        //
      } else if (event instanceof ScriptFinishedEvent) {
        event_queue.clear();
      } else if (event instanceof OutputEvent) {
        println(event.extra[0]);
      } else if (event instanceof StoreUpdateEvent) {
        Variable var = (Variable) event.extra[0];
        StringBuilder s = new StringBuilder();
      } else if (event instanceof WaitEvent) {
        Argument arg = (Argument) event.extra[0];
        int time = arg.toInteger();
      }
    } // func 
    //
    
  • To do so I tried to make a basic small sketch to run the QScript Fibonacci-Code. I extracted the relevant lines from your example sketch QScript IDE.

    and that was one of your mistakes because you hacked away code that was still needed. :(

    The next mistake was assuming that the file "fibonacci.txt" only contained the code. In fact the file includes the code and a description When loaded by 'QScript IDE' they are separated, both the code and the description are displayed but ONLY the code is used in the evaluation. You are trying to evaluate the description :D

    It promises that you can run any QScript within a hosting processing sketch

    Despite what you think this is true, but I didn't say it is was easy. Using the Solver$.evaluate(script); means that it is evaluated in a separate thread so special care has to be taken to avoid concurrent access exceptions. This naturally leads to more complex code.

  • Ok

    Sorry, I didn't mean to be rude

    Which parts are missing then?

    I don't want to run it in a separate thread

  • No apology needed, no offense taken :)

    The library is large and complicated so I strongly recommend that you read the library guides on my website here

    Try this code first.

    // test for Fibonacci 
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    import java.util.*;
    
    // ---------------------------------------------
    
    Script script;
    
    String[] algor = {
      "nbrTerms = 12", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "print(n1 + ' ' + n2 + ' ')", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  print(next + ' ') ", 
      "  count = count + 1", 
      "  IF(count % 10 == 0) ", 
      "    println('')", 
      "  ENDIF", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "println(''); END(next)"
      };
    
      // Int list to store fibseries
      IntList fibseries;
    
    LinkedList<ScriptEvent> event_queue = new LinkedList<ScriptEvent>();
    
    /// -------------------------------------------------------
    
    public void setup() {
      size(500, 550);
      // Initialize the fib series list
      fibseries =  new IntList();
      fibseries.append(1);
      fibseries.append(1);
    
      // Create the script to evaluate
      script = new Script(algor);
      // Allow a maximum 20 seconds to execute the script
      script.setTimeLimit(20);
      // Scripting events are to be sent to this object
      script.addListener(this);
      // Register the pre method for processing the events
      registerMethod("pre", this);
      // Evaluate the script
      Solver$.evaluate(algor);
    }
    
    public void draw() {
      background(48);
      noStroke();
      fill(255);
      int c = 0, px = 20, py = 30;
      for (Integer i : fibseries) {
        text(i, px, py);
        c++;
        px += 40;
        if (c % 10 == 0) {
          px = 20;
          py += 20;
        }
      }
    } // func 
    
    // ======================================
    
    /**
     * Since events are fired asynchronously this method might be called whilst G4P
     * is drawing its controls causing the program to crash. To avoid this we will
     * add the event to a queue and process it during Processing's event loop.
     */
    @EventHandler
      public void onScriptEvent(ScriptEvent event) {
      println("SCRIPT EVENT " + event);
      if (event instanceof HaltExecutionEvent && event.etype == ErrorType.STOPPED_BY_USER)
        event_queue.addFirst(event);
      else
        event_queue.addLast(event);
    }
    
    /**
     * This method has been registered with Processing so will be called just 
     * before the draw method. It will process a maximum of 20 events in the FIFO
     * queue, then allow the draw method to execute.
     * Since the script can generate hundreds of events per frame we have to
     * cap the number processed if we want the GUI to be responsive.
     */
    public void pre() {
      int count = 0;
      while (!event_queue.isEmpty () && count++ < 20)
        performEventAction(event_queue.removeFirst());
    }
    
    /**
     * This method performs the actions needed by any particular type of script 
     * event. 
     */
    public void performEventAction(ScriptEvent event) {
      println("EVENT  " + event);
      if (event instanceof TraceEvent) {
        println("Trace event");
      } else if (event instanceof HaltExecutionEvent) {
      } else if (event instanceof ScriptFinishedEvent) {
      } else if (event instanceof OutputEvent) {
      } else if (event instanceof StoreUpdateEvent) {
        println("Storage event");
        Variable var = (Variable) event.extra[0];
        if (var.getIdentifier().equals("next")) {
          int n = var.toInteger();
          println(n);
          fibseries.append(n);
        }
      } else if (event instanceof WaitEvent) {
      } else if (event instanceof SyntaxErrorEvent) {
        //Hopefully this won't be needed
      } else if (event instanceof EvaluationErrorEvent) {
        //Hopefully this won't be needed
      }
    } // func 
    //
    
  • If you only want to display the output in the console then this would do it :)

    // test for Fibonacci 
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    // ---------------------------------------------
    
    Script script;
    
    String[] algor = {
      "nbrTerms = 12", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "print(n1 + ' ' + n2 + ' ')", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  print(next + ' ') ", 
      "  count = count + 1", 
      "  IF(count % 10 == 0) ", 
      "    println('')", 
      "  ENDIF", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "println(''); END(next)"
      };
    
    /// -------------------------------------------------------
    
    public void setup() {
      // Create the script to evaluate
      script = new Script(algor);
      Solver.evaluate(algor);
    }
    
  • @quark:

    what is the right command instead of Solver$.evaluate(script); ?

    I didn't find script.run(); or script.start();

  • edited November 2015

    To evaluate the script in the main thread (synchronous) then use

    Solver.evaluate(script);

    The only difference is removing the $.

    The last example used synchronous evaluation and displays the first 12 terms of the series.

    This example (also synchronous) shows how you can get a particular term in the series.

    // test for Fibonacci 
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    // ---------------------------------------------
    
    Script script;
    int fib;
    
    String[] algor = {
      "IF(nbrTerms < 2)", 
      "  END(1)", 
      "ENDIF", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  count = count + 1", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "END(next)", 
    
    };
    
    /// -------------------------------------------------------
    
    public void setup() {
      size(500, 550);
      // Create the script to evaluate
      script = new Script(algor);
      fib = getFibTerm(4);
      println(fib);
    }
    
    public int getFibTerm(int n) {
      int fibn = 0;
      script.storeVariable("nbrTerms", n);
      Result r1 = Solver.evaluate(script);
      if (r1.isValid()) {
        fibn = r1.answer.toInteger();
      }
      return fibn;
    }
    

    Note

    Term  Value
    1          1
    2          1
    3          2
    4          3
    5          5
    6          8
    
  • edited November 2015

    ok, this is the minimalistic version

    // test for Fibonacci 
    
    import org.qscript.*;
    
    Script script;
    String[] algor; 
    
    // ------------------------------------
    
    public void setup() {
      size(500, 550);
    
      algor = loadStrings("fibonacci.txt");
      printArray(algor);
    
      script = new Script(algor);
      script.parse();
      println ("is parsed : "
        + script.isParsed() ) ; 
    
      script.evaluate();    // Basic mode
    
      println("end of setup()  --------");
    }
    
    public void draw() {
    } // func
    
    // -------------------------------
    

    the QScript script is

    nbrTerms = 12
    n1 = 1; n2 = 1 ; count = 2;
    print(n1 + ' ' + n2 + ' ')
    WHILE(nbrTerms > 2)
      next = n1 + n2
      print(next + ' ') 
      count = count + 1
      IF(count % 10 == 0) 
        println('')
      ENDIF
      n1 = n2; n2 = next
      nbrTerms = nbrTerms - 1
    WEND
    println(''); END(next)
    

    result

    is parsed : true
    1 1 2 3 5 8 13 21 34 55 
    89 144 
    end of setup()  --------
    
  • ah, sorry, I haven't seen your posts since I was working...

    ;-)

  • No problem, I hope they helped :)

  • thanks a lot......

    ;-)

  • edited February 2016

    journey tomorrow for a few days...

    but then....

    it will be part of a much bigger project though..............

    ;-)

  • Dear @quark,

    I now try to bring the Fibonacci QScript into the classical graphical representation of Fibonacci.

    but something is terrible wrong....

    Could you please take a look?

    Thank you!

    Best, Chrisir

    ;-)

    // test for Fibonacci 
    
    // http : // forum.processing.org/two/discussion/13282/qscript-fibonacci#latest
    // http : // www.openprocessing.org/sketch/135053
    
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    // ---------------------------------------------
    
    Script script;
    
    int fib = 5;
    int fibprev; 
    
    int n1  = 5;
    
    int[] quadrantOrder = new int [] {
      1, 4, 3, 2
    };
    
    final float quarterCircle = 0; // 1.0 * TWO_PI/4.0; 
    
    String[] algor = {
      "IF(nbrTerms < 1)", 
      "  END(1)", 
      "ENDIF", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  count = count + 1", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "END(next)", 
    
    };
    
    /// -------------------------------------------
    
    public void setup() {
      size(1200, 750);
      background(0);
    
      // Create the script to evaluate
      script = new Script(algor);
    
      fib = getFibTerm(n1);
      fibprev = fib;
    
      frameRate(2);
    }
    
    void draw() {
      if (n1<22) {
        n1++;
    
        fibprev = fib;
        fib = getFibTerm(n1);
    
        drawArc(quadrantOrder[(n1)%4]);
        println(fib);
      }
    
      // text box: 
      fill(0);
      rect(0, 0, 299, 24);
      fill(255); 
      text("Press any key often", 14, 14);
      // end of text box
    } // func 
    
    // ----------------------------------------
    
    void drawArc(int quadrant) {
    
      //draws an arc in specific quadrant
      // with current Fibonacci value 
    
      final int q1 = 2; 
      float x, y;
    
      x = width*0.5;
      y = height*0.6;
    
      fill((int)(fib%140) + 100);
    
      int textSize = (int)(fib * 0.3);
      int textValue = (int)fib /10;
    
      println ("quadrant "+quadrant); 
    
      switch(quadrant) {
      case 1:
        y += (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI+HALF_PI+quarterCircle, TWO_PI+quarterCircle, 
          PIE);
        fill(255);
        //   text(textValue, x+(fib*0.05), y-(fib*0.07));   
        break;
      case 2:
        x += (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI+quarterCircle, PI+HALF_PI+quarterCircle, 
          PIE);
        fill(255);
        //  text(textValue, x-((textSize*0.7)*(digits(textValue))), y-(fib*0.05));
        break;
      case 3:
        y -= (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI-HALF_PI+quarterCircle, PI+quarterCircle, PIE); 
        fill(255);
        //   text(textValue, x-((textSize*0.7)*(digits(textValue))), y+((textSize*0.9)));
        break;
      case 4:
        x -= (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          0+quarterCircle, PI-HALF_PI+quarterCircle, PIE);
        fill(255);
        //   text(textValue, x+(fib*0.05), y+((textSize)));
        break;
      } // switch 
      //
    } // func 
    
    public int getFibTerm(int n) {
    
      // contact to QScript Fibonacci script 
    
      int fibn = 0;
      script.storeVariable("nbrTerms", n);
      Result r1 = Solver.evaluate(script);
      if (r1.isValid()) {
        fibn = r1.answer.toInteger();
      }
      return fibn;
    } // func 
    // end of sketch
    
  • So the problem is not QScript but positioning and rotating the quadrants. I have been messing about with this for a couple of hours but have had no luck. Sorry :)

  • weird....

    did you check the 1st openprocessing sketch?

  • edited November 2015

    oh, I solved it

    issue was that we must call

      x = width  * 0.5;
      y = height * 0.6;
    

    only once and not throughout (as I had it first).

    It is called once in setup() and then x,y are changed throughout.

    I had the mistake that I had these 2 lines within the function drawArc().

    Btw. the code stems of course from openprocessing, see below.

    Thank you!

    Chrisir ;-)

    // test for Fibonacci 
    
    // http : // forum.processing.org/two/discussion/13282/qscript-fibonacci#latest
    // http : // www.openprocessing.org/sketch/135053
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    import org.qscript.editor.*;
    import org.qscript.*;
    import org.qscript.operator.*;
    import org.qscript.errors.*;
    
    // ---------------------------------------------
    
    Script script;
    
    int fib;
    int fibprev; 
    
    float x, y;
    
    
    int n1  = 1;
    
    int[] quadrantOrder = new int [] {
      1, 4, 3, 2
    };
    
    final float quarterCircle = 0; // 1.0 * TWO_PI/4.0; 
    
    String[] algor = {
      "IF(nbrTerms < 1)", 
      "  END(1)", 
      "ENDIF", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  count = count + 1", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "END(next)", 
    
    };
    
    /// -------------------------------------------
    
    public void setup() {
      size(1200, 750);
    
      x = width  * 0.5;
      y = height * 0.6;
    
      background(0);
    
      // Create the script to evaluate
      script = new Script(algor);
    
      fib = getFibTerm(n1);
      fibprev = fib;
    
      frameRate(222);
    }
    
    void draw() {
    
      // text box: 
      fill(0);
      rect(0, 0, 299, 24);
      fill(255); 
      text("Press any key often. Iteration is " 
        + n1
        + " ("
        + fib
        + ").", 14, 14);
      // end of text box
    } // func 
    
    // ----------------------------------------
    
    void keyPressed() {
      n1++;
      if (n1<22) {
    
        fibprev = fib;
        fib = getFibTerm(n1);
    
        drawArc(quadrantOrder[(n1)%4]);
        println("Iteration is " 
          + n1
          + " (with Fibonacci value "
          + fib
          + ").");
      }
    }
    
    void drawArc(int quadrant) {
    
      //draws an arc in specific quadrant
      // with current Fibonacci value 
    
      final int q1 = 2; 
    
    
      fill((int)(fib%140) + 100);
      noStroke();
    
      int textSize = (int)(fib * 0.3);
      int textValue = (int)fib /10;
    
      println ("quadrant "+quadrant); 
    
      switch(quadrant) {
      case 1:
        y += (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI+HALF_PI+quarterCircle, TWO_PI+quarterCircle, 
          PIE);
        fill(255);
        // text(textValue, x+(fib*0.05), y-(fib*0.07));   
        break;
      case 2:
        x += (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI+quarterCircle, PI+HALF_PI+quarterCircle, 
          PIE);
        fill(255);
        // text(textValue, x-((textSize*0.7)*(digits(textValue))), y-(fib*0.05));
        break;
      case 3:
        y -= (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          PI-HALF_PI+quarterCircle, PI+quarterCircle, PIE); 
        fill(255);
        // text(textValue, x-((textSize*0.7)*(digits(textValue))), y+((textSize*0.9)));
        break;
      case 4:
        x -= (fib - fibprev)/q1;
        arc(x, y, fib, fib, 
          0+quarterCircle, PI-HALF_PI+quarterCircle, PIE);
        fill(255);
        // text(textValue, x+(fib*0.05), y+((textSize)));
        break;
      } // switch 
      //
    } // func 
    
    public int getFibTerm(int n) {
    
      // contact to QScript Fibonacci script 
    
      int fibn = 0;
      script.storeVariable("nbrTerms", n);
      Result r1 = Solver.evaluate(script);
      if (r1.isValid()) {
        fibn = r1.answer.toInteger();
      }
      return fibn;
    } // func 
    
    //int digits (int value) {
    //  if (value < 10) return 1;
    //  else if (value < 100) return 2;
    //  else if (value < 1000) return 3;
    //  else return 4;
    //} // func  
    
    // end of sketch
    
  • @quark : in this new version I replaced the arcs with mere rects (little simpler)

    thanks again!

    Chrisir ;-)

    // test for Fibonacci
    
    // this version: with keyPressed to move on
    //               replacing the arcs with mere rects 
    
    // http : // forum.processing.org/two/discussion/13282/qscript-fibonacci#latest
    // http : // www.openprocessing.org/sketch/135053
    
    import org.qscript.eventsonfire.*;
    import org.qscript.events.*;
    // import org.qscript.editor.*;
    import org.qscript.*;
    // import org.qscript.operator.*;
    // import org.qscript.errors.*;
    
    // ---------------------------------------------
    
    Script script; // script
    
    int fib;      // result for the iteration 
    int fibprev;  // previous result 
    
    float x, y;   // position (init once, change throughout) 
    
    int n1  = 1; // which iteration do we have 
    
    int[] quadrantOrder = new int [] {
      1, 4, 3, 2
    };
    
    String[] algor = {
      "IF(nbrTerms < 1)", 
      "  END(1)", 
      "ENDIF", 
      "n1 = 1; n2 = 1 ; count = 2;", 
      "WHILE(nbrTerms > 2)", 
      "  next = n1 + n2", 
      "  count = count + 1", 
      "  n1 = n2; n2 = next", 
      "  nbrTerms = nbrTerms - 1", 
      "WEND", 
      "END(next)", 
    
    };
    
    /// -------------------------------------------
    
    public void setup() {
      size(1200, 750);
    
      x = width  * 0.5;
      y = height * 0.6;
    
      background(0);
    
      // Create the script to evaluate
      script = new Script(algor);
    
      fib = getFibTerm(n1);
      fibprev = fib;
    }
    
    void draw() {
      // text box: 
      //  rectMode(CORNER); 
      fill(0);
      rect(0, 0, 299, 24);
      fill(255); 
      text("Press any key often. Iteration is " 
        + n1
        + " ("
        + fib
        + ").", 14, 14);
      // end of text box
    } // func 
    
    // ----------------------------------------
    
    void keyPressed() {
      n1++;
      if (n1<22) {
    
        fibprev = fib;
        fib = getFibTerm(n1);
    
        drawRect(quadrantOrder[(n1)%4]);
        println("Iteration is " 
          + n1
          + " (with Fibonacci value "
          + fib
          + ").");
      }
    }
    
    void drawRect(int quadrant) {
    
      // draws an arc in specific quadrant
      // with current Fibonacci value 
    
      final int q1 = 2; 
    
      fill((int)(fib%140) + 100);
      noStroke();
    
      println ("quadrant "+quadrant); 
    
      float fib2 = fib / 2;
      float fibprev2 = fibprev / 2;
    
    
      switch(quadrant) {
      case 1:
        fill(255, 2, 2);  // upper right corner 
        y += (fib - fibprev)/q1;
        rect(x, y, fib2, -fib2); 
        fill(255);
        break;
      case 2:
        fill(2, 255, 2); // upper LEFT corner 
        x += (fib - fibprev)/q1;
        rect(x, y, -fib2, -fib2);  
        fill(255);
        break;
      case 3:
        fill(2, 0, 255); // LOWER LEFT corner
        y -= (fib - fibprev)/q1;
        rect(x, y, -fib2, fib2);  
        fill(255);
        break;
      case 4:
        fill(255, 0, 255); // LOWER right corner
        x -= (fib - fibprev)/q1;
        rect(x, y, fib2, fib2);  
        fill(255);
        break;
      } // switch 
      //
    } // func 
    
    public int getFibTerm(int n) {
    
      // contact to QScript Fibonacci script 
    
      int fibn = 0;
      script.storeVariable("nbrTerms", n);
      Result r1 = Solver.evaluate(script);
      if (r1.isValid()) {
        fibn = r1.answer.toInteger();
      }
      return fibn;
    } // func 
    
    // end of sketch
    
  • Dear @quark,

    after I made the first of the 3 example QScript scripts that come with your nice QScript IDE example into a visual (Fibonacci):

    Are there any special ideas how to show the other 2 examples graphical?

    I was just thinking of a bar diagram...

    Some visual way to convey there meaning as we did with the Fibonacci values?

    Do you have any other ideas for visuals?

    thank you!

    Best, Chrisir ;-)

  • There is already a graphical example of the prime number sieve (comes with the library).

    I can't see any interesting graphical representation of the 'Solve Quadratic Equation' example .

Sign In or Register to comment.