Find 5 Largest Values In A Linked List in Processing/Java

Hello Community,

So I got this code that is getting x,y positions from a motion sensor tracking a hand. The app draws a circle in the middle of the screen and then I have a function that will detect if the hand is outside of the circle. While the hand is outside of the circle, I have a function that checks the distance of the hand from the center of the circle. I'm attempting to store the distance data while the hand is outside of the circle into a linked list.

I need to get the top 5 largest values for each time the hand is outside the circle and also length of time.

Here my code thus far; I've left out a bunch of the code for setting up the motion sensor just for simplicity so this is semi pseudocode. In any case, my main issue is getting the values I need from the list. I have the circle class included as well. I do the outside of the circle calculation and how far outside of calculation inside of my circle class.

Please let me know if this makes sense! The motion sensor is reading in data at 200 fps so efficiency is factor here. On top of that, I am only expecting the hand to be outside of the circle for a few seconds at a time back and forth.

    import java.util.*;
    LinkedList<Integer> values;

    public void setup() 
    {
      size(800, 300);
      values = new LinkedList<Integer>();
      HandPosition = new PVector(0, 0); //This is getting x,y values from motion sensor
      aCircle = new Circle(); //my class just draws a circle to center of screen
      aCircle.draw();
    }

    public void draw() 
    { 

       if (aCircle.isOut(HandPosition)) /* detects if movement is outside of circle. Would it make more sense for this to be a while loop? I also need to start a timer as soon as this happens but that shouldn't be hard */
        {
        values.add(aCircle.GetDistance(HandPosition));  //gets how far the hand is from center of circle and adds it to linked list. Allegedly at least, I think this will work.
        /*So I need to get the 5 largest value from inside of my linked list here.
        I also need to start a timer*/
        }  
    }


    class Circle {

      PVector mCenter;
      int mRadius;

      Circle()
      {
        // initialize the center position vector
        mCenter = new PVector(0,0);
        mRadius = 150;
        mCenter.set((width/2),(height/2));
      }

      boolean isOut(PVector Position) //detects if hand position is outside of circle 
      {
        return  mCenter.dist(Position) <= mRadius;
      }

      float GetDistance(PVector Position) //detects how far the hand is from the center of circle 
      {
       return mCenter.dist(Position);
      }

      void draw() {
        ellipse(mCenter.x, mCenter.y, mRadius, mRadius); 
      }

    }

I'm new to Processing as well so don't hold back if any of this works.

tmore

Answers

  • Think about how you'd do this away from a computer. If somebody handed you a stack of index cards with numbers written on them, how would you pick the 5 largest?

    Pretend you have a really dumb friend. Write instructions that your friend could follow to accomplish your goal. Remember that your friend is really dumb, so break individual instructions down into smaller instructions. When you have a list of instructions your friend could follow to accomplish your goal, you'll have an algorithm that you can start thinking about converting to code.

  • What you need is a max priority queue. Check out http://stackoverflow.com/questions/3705881/changing-java-priorityqueue-to-a-max-pq

    You can create a Max PQ of size 5, and keep adding elements to it. Only the max 5 elements will be stored and rest will be discarded. The priority heap implementation is quite efficient too(logarithmic time complexity).

Sign In or Register to comment.