Return function from float[] array

Hi there,

I am stuck on a return function. I have a triangle, each corner is defined by pair of float [] arrays: float[] xArray = new float[3]; float[] yArray = new float[3] - all in the class to contstruct the triangle.
I need to be able to access each pair of arrays from another class or draw function - so I assume I need to return them in the class…how do I do that correctly?
Here is a code:

Triangle triangle;

void setup() {
  size(400, 400);
  triangle = new Triangle ( width/2, height/2, 40);
}

void draw() {
  background(88);     
  triangle.triangleDisplay();
}

// ---------------------------------------------

class Triangle {

  float x, y, w, h;
  float CSize;
  float[] xArray = new float[3];
  float[] yArray = new float[3];


  Triangle( float posx, float posy, float tW) {

    x = posx;
    y = posy;
    w = tW;
  }

  void triangleDisplay() {

    float currentAngle = -HALF_PI;  
    float wr = w * 0.5; 
    for (int i = 0; i < 3; i++) {
      xArray[i] = x + wr*cos(currentAngle);
      yArray[i] = y + wr*sin(currentAngle);

      //println(xArray[0], ",", yArray[0]);
      fill(255);
      currentAngle = currentAngle + radians (360 / 3);
    }
    triangle(xArray[0], yArray[0], xArray[1], yArray[1], xArray[2], yArray[2]);
  }
}

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

It’s good that you’ve considered the privacy of your class.

If xArray and yArray were private, you would need a public method to access them publically, say, something like

float[] getXArray(){
    return xArray;

}

In the bigger picture, you want the member variables to be readable, but not changeable.

Did this answer your question?

Hi Tony,

Indeed - as in I want to access each individual member (as in xArray[0]) from i.e. another class or draw function of main sketch. And indeed the variables are static…I will try with your method now :). THANK YOU!