0

我得到了这个代码,它从跟踪手的运动传感器获取 x,y 位置。该应用程序在屏幕中间画一个圆圈,然后检测手是否在圆圈之外。在此处输入图像描述 当手在圆外时,一个函数会检查手到圆心的距离。我试图在手在链表中的圆圈之外时存储距离数据。

我需要获得前 5 个最大值和每次手在圆圈外的持续时间。

到目前为止,这是我的代码;为了简单起见,我省略了一堆设置运动传感器的代码,所以这是半伪代码。无论如何,我的主要问题是从列表中获取我需要的值。我也有圈课。我进行圆外计算以及在我的圆类内部计算多远。

请让我知道这是否有意义!运动传感器以 200 fps 的速度读取数据,因此效率是这里的因素。最重要的是,我只希望手来回移动,一次在圆圈外几秒钟。

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); 
  }

}

我也是 Processing 的新手,所以如果其中任何一个有效,请不要退缩。

4

1 回答 1

1

您可以Collections.sort(List);在此处使用,然后从列表中取出最后五个元素。

集合.排序()

于 2013-12-12T09:22:57.050 回答