如何 return 来自 class 的值,从 Processing 中的数组派生

How to return value from a class, derived from an array in Processing

我是 Processing 的新手。我想学习如何绘制我正在做的一些模型,所以我正在使用 gwoptics 来做。他们有一个名为 RollingGraph 的示例。这基本上可以沿着时间相关的滚动 x 轴绘制出您想要的任何内容。

问题是我不太明白我是如何得到它来绘制我想要的东西的。

我有一个数组,假设在 canvas 上随机绘制椭圆,并且这些椭圆每帧随机旋转。我怎样才能得到滚动图来绘制所有旋转的总和,这可能是 circle.rot?

到目前为止,我拥有我能得到的最好的 MCVE....

import org.gwoptics.graphics.graph2D.Graph2D;
import org.gwoptics.graphics.graph2D.traces.ILine2DEquation;
import org.gwoptics.graphics.graph2D.traces.RollingLine2DTrace;



  class eq implements ILine2DEquation{
      public double computePoint(double x,int pos) {
       return mouseX;    //////HOW DO I GET THIS TO RETURN THE SUM OF circle.rot?????? 
      }
    }

    class eq2 implements ILine2DEquation{
      public double computePoint(double x,int pos) {
        return mouseY;
      }    
    }

    class eq3 implements ILine2DEquation{
      public double computePoint(double x,int pos) {
        if(mousePressed)
          return 400;
        else
          return 0;
      }    
    }

    RollingLine2DTrace roll,roll2,roll3;
    Graph2D g;


class Circle{
   public float x1; 
   public float y1;
   public float x2;
   public float y2;
   public color cB;
   public float rot;
   public float authority;
   public float fert = 1;
   public float r = x1; //radius


   public Circle(float x1, float y1, float x2, float y2, color tempcB, float rot, float authority, float fert){
          this.x1 = x1;
          this.y1 = y1;
          this.x2 = x2;
          this.y2 = y2;
          this.cB = tempcB;
          this.authority = random(255);
          this.fert = random(1);
          this.rot= random(360);
       }
    }

    public ArrayList<Circle> circles = new ArrayList<Circle>();

    void setup(){
      size(1000, 1000);
      frameRate(6);
      rectMode(CENTER);
      ellipseMode(CENTER);

      int sec = second();

      roll  = new RollingLine2DTrace(new eq() ,100,0.01f);
      roll.setTraceColour(0, 255, 0);


      roll2 = new RollingLine2DTrace(new eq2(),100,0.01f);
      roll2.setTraceColour(255, 0, 0);

      roll3 = new RollingLine2DTrace(new eq3(),100,0.05f);
      roll3.setTraceColour(255, 255, 255);

      g = new Graph2D(this, 400, 200, false);
      g.setYAxisMax(600);
      g.addTrace(roll);
      g.addTrace(roll2);
      g.addTrace(roll3);
      g.position.y = 50;
      g.position.x = 100;
      g.setYAxisTickSpacing(100);
      g.setXAxisMax(5f);
      smooth();
      background(204);

      noStroke();
      fill(255, 204,100);
           for(int i = 1; i < 48; i++){
           float r = random(100,height-200);
           float s = random(100,width-200);
           float t = 20;
           float u = 20;
           circles.add(new Circle(r,s,t,u,color(100,14,14),random(360),color(100,14,14),random(10)));
       }
    }

    void draw() {
      background(204);
      g.draw();

    for(Circle circle : circles){
      pushMatrix();
            translate(circle.x1, circle.y1);
            rotate(random(360));
            translate(-circle.x1, -circle.y1);
            fill(circle.authority);
            strokeWeight(0);
            stroke(100,0,0);
            rect(circle.x1, circle.y1, 24, 36,0, 0, 12, 18);
            popMatrix();
      }
    }

如果我理解你的问题,你所要做的就是遍历实例并计算总数。

首先,您需要一个包含所有实例的数据结构。您已经说过您正在使用数组,所以听起来您已经涵盖了所有内容。您必须确保此数组在 范围内 以用于下一步,因此这可能意味着在草图级别声明它(而不是在函数或 class 内)。

其次,您将需要一个 for 循环来遍历数据结构中的实例。您可以使用变量来累加总数。像这样:

float total = 0;
for(Circle c : yourCircleArray){
   total += c.rot;
}

您可以将其放入一个函数中,以便您可以随时调用它。

编辑: 更仔细地查看您的代码,您实际上有一个 ArrayList,而不是数组。看起来它已经在草图级别进行了初始化,所以您需要做的就是:

 public double computePoint() {
    float total = 0;
    for(Circle c : circles){
       total += c.rot;
    }
    return total;
 }

如果您无法使其正常工作,请尝试创建一个 MCVE,方法是消除对您在草图顶部导入的任何库的依赖。请记住,MCVE 应该将其缩小到一个特定问题(如何从数组中获取总数),而不是完成您的整个最终目标。