How to access each item from the loop via return function

Hello there,

Another return function question. I have a for loop of distances between a set of xai and xbi positions in the class. I need to be able to access each distance individually. I wrote the function below which prints all distances, but now in main sketch I need to access each of them individually. how do I do that?


 float finddist() {
    
   for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 

      v1 = new PVector(newPosXa, posY-row);
      v2 = new PVector(newPosXb, posY-row); 
      allDist = v1.dist(v2);   
      println("distance",i,":", allDist);
    }
     return allDist;  
    }

Here is a whole code to explain better (basically I need to access individual length of each line (or a distance between the beginning and the end).

Middle middle;


void setup(){
  
 size(500, 500);
 background(0);
 middle = new Middle(width/2, 400, 40, 400);
   middle.makebase(); 
  middle.makelines(); 
  middle.makePoints(); 
  middle.finddist();
  
}

void draw(){
 

}

class Middle {

  float middlePos;  
  float posXa, posXb, posY;
  //float newPosXa, newPosXb;
  float distance;
  float d;
  float unit;
  PVector  v1, v2;
 float allDist ;
 
  Middle(float _middlePos, float _distance, float _unit, float _posY) {
    middlePos = _middlePos;
    distance = _distance;
    unit = _unit;
    posY = _posY;
  }

  void makebase() {

    posXa = middlePos - (distance/2);
    posXb = middlePos + (distance/2);


    stroke(244, 23, 230);
    strokeWeight(0.3);
    line(posXa, posY, posXb, posY);
  }

  void makelines() {

    for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 
      stroke(244, 23, 230);
      strokeWeight(0.3);
      line(newPosXa, posY-row, newPosXb, posY-row);
    }
  }

  float finddist() {
    
   for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 

      v1 = new PVector(newPosXa, posY-row);
      v2 = new PVector(newPosXb, posY-row); 
      allDist = v1.dist(v2);   
      println("distance",i,":", allDist);
    }
     return allDist;  
    }
  

  void makePoints() {
    
    
    
    for (int i = 0; i < distance/unit+1; i++ ) {

      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;


      int row = 20*i; 

      fill(255);
      noStroke();
      ellipse(newPosXa, posY-row, 2, 2);
    }
  }
}

The easiest way to this is to not do it. Instead, just have a global array of values, and store the values you want to save into that array.

// At the top level:
float[] allDistances = new float[10];

// Inside your function:
allDistances[i] = whatever;

// Outside that function, but elsewhere:
if( allDistance[i]  > whatever ){

Hi and thank you!
However i am still a bit lost, I have tried to somehow follow your instruction. So - do you mean:



// At the top level:
float[] allDistances = new float[10];

Shal I place it on the of the main sketch or class? Then the function - you mean I shall skip return all together? A bit more explanaition would be ever so helpful :). And very appreciated! Thank you in advance :slight_smile:


float array_of_values = new float[10];

void setup(){
  // ...
}

void draw(){
  // ...
  // You can use the values here.
  if( array_of_values[0] < 50 ){
    // ...etc
  } 
}

class YourClass {

  void yourClassFunction(){
    for( int i = 0; ... ){
      // Calculate distance here
      array_of_values[i] = the_ith_distance;
    }
    // No need to return anything.
  }
}