Agents coding work but need add on two variable data

image

i need on this coding to add two variable data which i have in csv file format. please help me.

Okay. So if your values are stored in a CSV file, you are going to need to load that file. You can use loadStrings() to do that.

Then you will need to parse the loaded data. You will need to loop over each line. Use a for loop.

I assume that each line will have two numbers on it, separated by a comma (as it is a CSV file). You can use split() to parse the line and get the two strings that represent the values. Then you can cast those values to numbers, using either int() or float().

Next, you will probably want to associate each pair of values with a Ball object. You could pass these values as parameters to the new Ball() constructor call. Then you would just need to modify the Ball class (which you have, I assume? You haven’t posted it…) to accept these two parameters.

You would then modify the Ball class to use the stored values… maybe as a size or color value when drawing a Ball? That’s up to you.

Look in the reference for each of the keywords and key functions I have mentioned to get a feel for how to use them. See how much of this process you can get done yourself, and post the code of your attempt for more help.

import toxi.geom.*;

//DECLARE - store all the balls/global variable 
ArrayList ballCollection;

// Setup the Processing Canvas
void setup() {
  size(600, 600);
  smooth();
  String [] data; //d


  //INITIALIZE
  ballCollection=new ArrayList();

  for (int i = 0; i < 100; i++) {
    Vec3D origin = new Vec3D (random(width), random(130), 0);
    Ball myBall = new Ball(origin);
    ballCollection.add(myBall);
  }
}

// Main draw loop
void draw() {
  background(0);


  //CALL FUNCTIONALITY

  for (int i = 0; i < ballCollection.size(); i++) {
    Ball mb = (Ball) ballCollection.get(i);
    mb.run();
  }
}


class Ball {
  // GLOBAL VARIABLES - LOCATION SPEED
  Vec3D loc = new Vec3D (0, 0, 0);
  Vec3D speed = new Vec3D(random(-2, 2), random(-2, 2), 0);
  Vec3D acc = new Vec3D(); 
  Vec3D grav = new Vec3D(0, 0.2, 0);

  //CONSTRUCTOR - HOW DO YOU BUILD THE CLASS - GIVE VARIABLES A VALUE
  Ball(Vec3D _loc) {
    loc = _loc;
  }

  //FUNCTIONS - BREAK DOWN COMPLEX BEHAVIOUR INTO DIFFERENT MODULES

  void run() {
    display();
    move();
    bounce();
    //gravity();


    //Create a line between the balls
    lineBetween();
    //flock = complex behaviour. Craig Reynolds Boids
    //flock();
  }


  /*
  void flock(){
   
   //3 functions of flock : based on vector maths
   separate(5);
   cohesion(0.001);
   align(1);
   
   }
   
   
   void align(float magnitude){
   Vec3D steer = new Vec3D();
   int count = 0;
   
   
   
   for(int i = 0; i < ballCollection.size();i++) {
   Ball other = (Ball) ballCollection.get(i);
   
   float distance = loc.distanceTo(other.loc);
   
   if(distance > 0 && distance < 40) {
   
   steer.addSelf(other.speed);
   count ++;
   
   
   
   
   }
   }
   
   if(count > 0){
   steer.scaleSelf(1.0/count);
   }  
   steer.scaleSelf(magnitude);  
   acc.addSelf(steer);
   
   
   
   }
   
   //cohesion =opposite of seperate - keep together - 
   
   void cohesion(float magnitude){  
   
   Vec3D sum = new Vec3D();
   int count = 0;
   
   
   
   for(int i = 0; i < ballCollection.size();i++) {
   Ball other = (Ball) ballCollection.get(i);
   
   float distance = loc.distanceTo(other.loc);
   
   if(distance > 0 && distance < 40) {
   
   sum.addSelf(other.loc);
   count++;  
   }
   }
   if (count > 0){
   sum.scaleSelf(1.0/count);
   }
   
   Vec3D steer = sum.sub(loc); 
   
   steer.scaleSelf(magnitude);
   
   
   acc.addSelf(steer);
   
   
   }
   
   
   void separate(float magnitude){
   
   Vec3D steer = new Vec3D();
   int count = 0;
   
   
   
   for(int i = 0; i < ballCollection.size();i++){
   
   Ball other = (Ball) ballCollection.get(i);  
   
   float distance = loc.distanceTo(other.loc);
   if(distance > 0 && distance <  30){
   
   //move away from another ball // calculate a vector of difference
   
   Vec3D diff = loc.sub(other.loc);
   //increases smoothness of the steer
   diff.normalizeTo(1.0/distance); 
   
   steer.addSelf(diff);
   count++;
   
   
   
   }
   
   } 
   if (count > 0){
   steer.scaleSelf(1.0/count);
   
   }
   steer.scaleSelf(magnitude);
   acc.addSelf(steer);
   
   }
   */
  void lineBetween() {
    //BallCollection
    for (int i = 0; i < ballCollection.size(); i++) {

      Ball other = (Ball) ballCollection.get(i);  

      float distance = loc.distanceTo(other.loc);
      if (distance > 0 && distance <  100) {
        stroke(255, 0, 0);
        strokeWeight(0.4);
        line(loc.x, loc.y, other.loc.x, other.loc.y);
      }
    }
  }

  void gravity() {
    speed.addSelf(grav);
  }

  void bounce () {
    if (loc.x > width) {
      speed.x = speed.x * -1;
    }
    if (loc.x < 0) {
      speed.x = speed.x * -1;
    }
    if (loc.y > height) {
      speed.y = speed.y * -1;
    }

    if (loc.y < 0) {
      speed.y = speed.y * -1;
    }
  } 

  void move() {
    //steering behaviours need accelartion :store the movements for the ball 

    speed.addSelf(acc);

    speed.limit(2);

    loc.addSelf(speed);

    acc.clear();
  }
  void display() {
    stroke(0);
    ellipse(loc.x, loc.y, 5, 5);
  }
}

sir this is full code. how to implement the function of coding. help me please.

Alright. It’s good to have code to start out with. We didn’t actually need to see the Ball class yet, as all of the data loading and parsing is probably going to happen in setup().

Since you have an array of Strings called data handy, we can use that to load the CSV file’s contents into.

data = loadStrings( "your_filename.csv" );

You can even look at the first line of the loaded data:

println( data[0] );

Try using split() on that line to get both of those values! Try it yourself! Post the code of YOUR ATTEMPT at this. Don’t think that you can get away with not trying to do it yourself! It’s not cunning or sneaky or even polite…

Attempt a for loop too if you are feeling bold…

void setup() {
  size(600, 600);
  String [] data; 

  data = loadStrings( "Spellman.csv" );
  println("there are " + data.length + " data");
  for (int i = 0; i < data.length; i++) {
    println(data[i]);
  }
}

Sir is this correct way i’m doing? Guide me sir. thank you lot previous guide.

Kindly help me above coding…

So far so good. Now, you probably see some output, right? Something like:

46.383,62.939,
39.73,6.828,
...

Not this exact output, of course… but the contents of your CSV file. A bunch of numbers, separated by commas.

So, as I have said, the next step is to use split(). If you took the time to find the Reference page for this function, you would already have all the information you need.

https://processing.org/reference/split_.html

Again, try to understand what the split() function does, and then attempt to use it in your own code yourself. The goal here is to get just ONE number per line!

void setup() {
  size(600, 600);
  String [] data; 

  data = loadStrings( "Spellman.csv" );
  println("there are " + data.length + " data");
  for (int i = 0; i < data.length; i++) {
    // Tell which line we are looking at now.
    println( "Now looking at line # " + i + ", which is " + data[i] );
    // Parse this line... somehow...
    float[] numbers_on_this_line = ???; // Try to fill in the rest of this line.
    // Show every number we found on this line.
    for( int j=0; j < numbers_on_this_line.length; j++){ println( "" + i + ", " + j + " => " + numbers_on_this_line[j] );}
  }
}
1 Like

So far i get one issues on this. I’m stuck on this issue help me.

The file "Spellman.csv" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

What is the name of your CSV file? Is it in your sketch’s data folder?

The file name is Spellman.csv. In sketch’s data folder.

Okay, so it’s not a missing file then.
Is the folder accessible?
Did you add the file to your sketch?
Is the file readable?
I mean, these are things that the error message you got suggested you try…

That folder can accessible.
How to add the to my sketch? For double confirmation i have click sketch option and select show sketch folder its shown that place i have save.
that file is readable.

Alright. Try saving your sketch and restarting Processing.

i have restart but still same issues.

In the menus, click on “Sketch” > “Add File…”, and try adding it again to make sure your file has been added to your sketch properly.

Make sure you have the name and location right.
“Spellman.csv” is not the same as “SpellMan.csv” or “Spellman.CSV”!

Thank you sir. i get the result right now based on this coding.

void setup() {
  size(600, 600);
  String [] data; 

  data = loadStrings( "Spellman.csv" );
  println("there are " + data.length + " data");
  for (int i = 0; i < data.length; i++) {
    println(data[i]);
  }
}

The output result is

0.35887,0.1546
0.3588,0.13535
0.3588,0.18143
0.3588,0.04211
0.3588,0.24413
0.3588,0.08708
0.35864,0.18746
0.35849,0.13674
0.35785,0.1546
0.35768,0.09907
0.35768,0.13535
0.35768,0.24413
0.35768,0.08708
0.35768,0.02179
0.35768,0.08708
0.35768,0.18746
0.35768,0.11214
0.35768,0.06695
0.35768,0.06695
0.35768,0.2143
0.35768,0.11214
0.35768,0.13535
0.35768,0.08708
0.35768,0.12725
0.35768,0.08708
0.35768,0.15433
0.35768,0.08708
0.35768,0.09907
0.35768,0.09907
0.35722,0.09907
0.35711,0.09907
0.35711,0.17422
0.35711,0.08708
0.35711,0.10662
0.35711,0.17106
0.35711,0.06607
0.35694,0.09907
0.35694,0.04211
0.35694,0.08708
0.35694,0.13535
0.35694,0.09907
0.35694,0.08708
0.35678,0.10662
0.35662,0.07215
0.35662,0.13535
0.35623,0.09907

Based on this split() function. I’m confuse on it why i need split data.? Because i need implement two variable data into class ball coding.

Right now your data is still tied up in a String. That is, a line of your data is, for example, this:

"0.32768,0.11214"

This is not two numbers! It is one String. First, you want to separate that String into two Strings, each of which represents a number. This is what split does.

"0.32768"    and    "0.11214"

Then, as I have said before, you will want to turn these Strings into numbers. Since they are numbers with decimals, you will want to use floats, or floating point, numbers.

String line_with_numbers = "0.32768,0.11214";
String[] split_line = ???;
float a,b;
a = float( ??? );
b = float( ??? );
println( "" + a " " + b );

Go back to this post now that your CSV file loads… Agents coding work but need add on two variable data

I have make something split function code.

void setup() {
  size(600, 600);
  String [] data; 
  data = loadStrings( "Spellman.csv" );
 println("there are " + data.length + " data");
  for (int i = 0; i < data.length; i++) 
 {
    //data = int(split(stuff[0], ','));
    //println(data[i]);
println( "Now looking at line # " + i + ", which is " + data[i]); // Tell which line we are looking at now.
   
  
   String [] splitNums = split(data, " "  );
   String data = splitNums.length; 
   
   for (int j=0; j < splitNums.length; j++) {
     myNums.append(splitWords[j];  
  }
}

But i never get any output result on this coding. Please help me.