How do you use a specific element from a returned array that comes from a function?

Hi, I've got a question. I have a custom defined function called Projection, that will return an array with two values. The first value is the X-coordinate and the second value is the Y-coordinate. I want to know how to be able to use the returned X-coordinate in a calculation. Thanks!

Answers

  • edited August 2018

    Show your code

    if the array is called a1 you want to use

    a1[0] + 300

    If you‘d return a PVector it would be pv.x + 300

  • There’s also a new forum

  • Without seeing your code my answer is a little generic

    Also see reference and tutorials on arrays

  • void setup() {
      final int x = randomXY()[0];
      println("x: " + x);
      exit();
    }
    
    int[] randomXY() {
      final int x = (int) random(width);
      final int y = (int) random(height);
      return new int[] { x, y };
    }
    
  • similar version to the above version

    void setup() {
      size(600, 600);
    }
    
    void draw() {
      int[] listXY = randomXY();
    
      println("x: " +
        listXY[0]);
    
      noLoop();
    }
    
    // --------------------------------------------------------------
    
    int[] randomXY() {
      // returns an array with x,y (better use type PVector though) 
      int x = (int) random(width);
      int y = (int) random(height);
    
      int[] listOfResult = new int[2];
    
      listOfResult[0] = x;
      listOfResult[1] = y;
    
      return listOfResult;
    }
    //
    
Sign In or Register to comment.