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

914 Views Asked by At

I got this code that gets x,y positions from a motion sensor tracking a hand. The app draws a circle in the middle of the screen and then detects whether the hand is outside the circle. enter image description here While the hand is outside the circle, a function 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 in a linked list.

I need to get both the top 5 largest values and the duration for each time the hand is outside the circle.

Here's 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-pseudo code. 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, going back and forth, to be outside of the circle for a few seconds at a time.

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.

1

There are 1 best solutions below

5
On

You can use Collections.sort(List); here, then take last five element from the list.

Collection.Sort()