Creating a list of data and accessing a value

henhen
edited October 2013 in How To...

Hi,

I am trying to track and object and determine the difference in angle by subtracting the previous angle from the current. Problem is it is too fast and I often get a value of zero. How can I put the variable 'previousAngle' into a list so that I can, for instance, subtract the previousAngle from 10 readings ago from the current angle.

I am very new to processing so please bear with me if I seem slow.

Many thanks.

Answers

  • edited October 2013

    You should start by posting your attempt here. If it's too big, make a model of the part you got problem with!
    By your description, a Queue would be the best tool. Java got an ArrayDeque class for it! =:)

  • Hi,

    You helped me identify the need to use array or float in a previous discussion: http://forum.processing.org/two/discussion/593/comparing-current-angle-of-tracked-pixel-to-previous-delay#Item_6 I am struggling to understand how to use either, I have read the reference entries for both and am none the wiser. I basically just need to slow the comparison between current and previous angles.

  • can Queue only retrieve the head of the queue?

  • edited October 2013

    Yup. a Queue adds to its tail and retrieves from its head! It's a FIFO structure, aka FCFS (1st come, 1st served). (~~)

    A kewl demo using it: :ar!

    /** 
     * Brownian Motion (v2.26)
     * by Ammon.Owed (2013/Aug)
     * mod GoToLoop
     * 
     * forum.processing.org/topic/
     * controlling-the-length-of-line-from-x-y-to-a-point-which-is-randomly-chosen
     */
    
    import java.util.Queue;
    import java.util.ArrayDeque;
    
    final static int DIM = 100, MAX = 03000, DETAIL = 1000, DEPTH = 2000;
    final static int HUE = 1<<10, FPS = 60, TOLERANCE = 40, ALIASING = 2;
    
    final static float BOLD = 1.5;
    final static boolean HAS_MAX_LIMIT = true;
    
    final Queue<PVector> points = new ArrayDeque(MAX);
    final PVector cam = new PVector(), lp = new PVector();
    
    float canvasRatio;
    
    void setup() {
      size(1000, 700, P3D);
      colorMode(HSB, HUE, 1, 1);
    
      frameRate(FPS);
      smooth(ALIASING);
    
      strokeWeight(BOLD);
      noFill();
    
      canvasRatio = (float) width/height;
      frameRate = TOLERANCE<<1;
    }
    
    void draw() {
      // auxiliary local variables:
      final int fc = frameCount, fr = round(frameRate);
      final PVector np = new PVector(); // new point.
    
      // interpolated rotating camera aimed at latest point (tail):
      cam.mult(.99);
      cam.add(PVector.mult(lp, .01, np));
    
      camera(cam.x + sin(fc*.01)*DETAIL, cam.y + cos(10 + fc*8e-3)*DETAIL, 
      cam.z - DEPTH, cam.x, cam.y, cam.z, 0, 1, 0);
    
      //perspective(THIRD_PI, canvasRatio, 1, 1e6);
    
      // draw colored curved lines:
      clear();
    
      int idx = 0;
      beginShape();
    
      for (PVector p: points) {
        stroke(fc + idx++ & HUE - 1, 1, 1);
        curveVertex(p.x, p.y, p.z);
      }
    
      endShape();
    
      // remove oldest point (head) if limit is reached:
      if (HAS_MAX_LIMIT) {
        if (fc > MAX)        points.remove();
      }
    
      else
        if (fr < TOLERANCE)  points.remove();
    
      // generate a new point at specified range:
      PVector.random3D(np, this).mult(DIM); // pick a new random direction.
      np.add(lp);                           // add that up from latest point.
      lp.set(np);                           // this is now the latest point too.
    
      points.add(np);                       // add new point to queue's tail.
    
      frame.setTitle("Brownian Motion\t\tFPS: " +fr+ "\t\tSize: " +points.size());
    }
    
Sign In or Register to comment.