简体   繁体   English

在Processing / Java中的链接列表中找到5个最大的值

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

I got this code that gets x,y positions from a motion sensor tracking a hand. 我得到了从跟踪手的运动传感器获取x,y位置的代码。 The app draws a circle in the middle of the screen and then detects whether the hand is outside the circle. 该应用程序在屏幕中间绘制一个圆圈,然后检测手是否在圆圈之外。 在此处输入图片说明 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. 每当手出圈时,我都需要获取前5个最大值和持续时间。

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. 运动传感器以200 fps的速度读取数据, 因此效率是决定因素。 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. 我也是处理技术的新手,所以如果有任何效果,请不要退缩。

You can use Collections.sort(List); 您可以使用Collections.sort(List); here, then take last five element from the list. 在这里,然后从列表中选择最后五个元素。

Collection.Sort() Collection.Sort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM