使用课程

时间:2017-06-24 19:44:28

标签: class csv processing

*根据以下建议编辑。继续努力使代码更少。 * 我试图转向在我的代码中使用类。我在一段更大的代码中运行了这个功能,并且我正在尝试使用类重新创建步进操作。

 if (i< table.getRowCount()) {
    row = table.getRow(i);
    k=row.getInt("y");
    x=row.getInt("x");
  }

  //draw white ellipse visual
  ellipse(x*diam, y, diam, diam);

  // step next circle up
  y+=Step;

  // -- if off the set upper limit of the line,
  // swing right 


  if (frameCount%(k/Step)==0) {
    x+=Step;

    y=starty;
    // increment row counter
    i=i+1;
  }

我正在尝试解决为什么我无法单步执行变量i作为csv表中的行。

我希望k能够在csv文件中打印出名为“y”的列中的数据。 我希望每次变量y达到k值时,我打印出一个递增1的数字。

从打印出我的变量i,k,x

k成功从名为“y”

的csv文件列中获取数据

x成功从名为“x”的csv文件列中获取数据

y成功递增

我没有递增

y未注册已达到k。

我现在的想法是,实际上变量k是y停止递增的标记,但也许不应该是构造函数中Dot参数的一部分。

// An Array of Dot objects

Dot[] dots;
// A Table object
Table table;
float diam=10.0;
int i;

void setup() {  
  size(560, 420);
  loadData();
  background(0);
  // set the animation speed
  frameRate(10);
}

void draw() {
  // Display all dots
  for (Dot d : dots) {
    d.display();
    d.move();
  }

}

void loadData() {
  // Load CSV file into a Table object
  // "header" option indicates the file has a header row
  table = loadTable("data.csv", "header");

  // The size of the array of Dot objects is determined by the total number of rows in the CSV
  dots = new Dot[table.getRowCount()]; 

  // You can access iterate over all the rows in a table
  int rowCount = 0;
  for (TableRow row : table.rows()) {
    // You can access the fields via their column name (or index)
    if (i< table.getRowCount()) {
      row = table.getRow(i);
      float x = row.getFloat("x");
      float k = row.getFloat("y");
      float d = row.getFloat("diameter");


      //String n = row.getString("name");
      // Make a fot object out of the data read
      dots[rowCount] = new Dot(i,k, x, d);

      rowCount++;
    }
  }

  //  check to see if at end of data
  if (i == table.getRowCount()) {
    i = 0;//if so, loop back to first row
  }
}

class Dot {
  float x =10.0;
  float y=10.0;
  float diam=10.0;
  float k;
  int step=10;
  int startx =10;
  int starty=10;

  float i=0;

  Dot(float i_,float k_, float x_,float diam_) {
    k=k_;
    x=x_;
    i=i_;
    diam=diam_;
  }




  void display() {
    noStroke();
    fill(255);
    // draw a circle
    ellipse(x*diam, y, diam, diam);
  }

  void move() {
    // move next circle to the right
    y+=step;

    // -- if off the right edge of the line,
    // swing down and all the way to the left
    if (frameCount%(k/step)==0) {
      x+=step;
      y=starty;
      // increment row counter
      i=i+1;
    }

    // if off the bottom of the page, stop
    if (x>=width) {
      fill(0);
      text("done", width/2, height/2);
      noLoop();
    }
  }
}

0 个答案:

没有答案