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