Trying to use coordinates from an array to draw an ellipse gives "The method ellipse(float, float, float, float) in the type PApplet is not applicable for the arguments (float[], float[], float, float)"

So at school we are learning to program now using processing, and one of the homework assignments is to draw several ellipses using a preformatted piece of code:

int[][] circles = { {10, 15}, {100, 130}, {77, 43}, {30, 145}, {185, 17}, {99, 76} }; 
final int DIAMETER = 20; 
final int YELLOW = #FFFF00; 
final int RED = #FF0000; 

void setup() {   
  size(200, 200);   
  ellipseMode(CORNER);
} 

void draw() {   
  background(#000000);
  drawCircles(circles);
} 

We are not allowed to change anything in it but we need to draw all the ellipses by writing a drawCircles() function. Now I have tried to get mine to work for days and asked my fellow students for help as well but the solution eludes meā€¦ hereā€™s what I have so far:

void drawCircles(int[][] array) {
  for (int i =0; i <= array.length; i++) {
    for (int j =0; j <= array.length; j++) {
      fill(YELLOW);
      float[] x = float(array[i]);
      float[] y = float(array[j]);
      ellipse( x, y, (float)DIAMETER, (float)DIAMETER);
    }
  }
}

Whatever I try it keeps giving me errors; mostly about not entering floats into the ellipse() function therefore I have been trying to convert them using many different methodsā€¦ Could someone perhaps point me in the right direction?

2 Likes

Just taking a fast look, it seems that you are passing an array, not a float

x is a float array, same with y

If you want to give the coordinates x y from the array, you donā€™t need the second for:

void drawCircles(int[][] array) {
  for (int i =0; i < array.length; i++) {
      fill(YELLOW);
      float x = float(array[i][0]);
      float y = float(array[i][1]);
      ellipse( x, y, (float)DIAMETER, (float)DIAMETER);
  }
}

With float(array[i]) you are acceding to { {x, y},.......}, not the x value or the y, so the ellipse method is receiving something like this ellipse( {10,15}, {100,130} ,(float)DIAMETER,(float)DIAMETER); , instead of ellipse(10, 15 ,(float)DIAMETER,(float)DIAMETER);

Hope this helps you :smiley:

3 Likes

Thereā€™s no need to coerce int values to float b/c Java automatically does that for us. :wink:

Instead what you actually have to do is extract 1D int[] arrays outta your 2D int[][] 1.

The easiest way to pull that out is use an ā€œenhancedā€ for ( : ) loop type:

Letā€™s say your drawCircles()'s 2D int[][] parameter is named arr2d:
void drawCircles(final int[]... arr2d) {}

So you have to loop over that arr2d in such a way that in each iteration of it we get a 1D int[] array from it.

Letā€™s name the iterator variable as arr1d and declare it as datatype int[]:
for (final int[] arr1d : arr2d) {}

Given parameter arr2d actually points to the passed argument circles:
drawCircles(circles);

You can be sure that in each iteration the iterator arr1d is a 1D int[] array w/ length = 2.

Then youā€™ve got exactly indices [0] & [1] from arr1d.

So you can finally access those 2 indices like this: circle(arr1d[0], arr1d[1], DIAMETER);

Thatā€™s it! If you didnā€™t understand some point of it, just ask again. :innocent:

5 Likes

Thank you Waboqueox! That explains it!

1 Like

Wowā€¦thanks GoTo! This concept is still a bit hard to grasp for me but I think Iā€™m beginning to understand what you are talking about here. Iā€™ll have to study it some more before Iā€™ll know for sure though, but a definite ā€˜thank youā€™ for taking the time to address this!

2 Likes

here is a version that gets the 1D arrays from the 2D array and prints them



int[][] circles = { 
  {10, 15}, 
  {100, 130}, 
  {77, 43}, 
  {30, 145}, 
  {185, 17}, 
  {99, 76} 
}; 
final int DIAMETER = 20; 
final int YELLOW = #FFFF00; 
final int RED = #FF0000; 

void setup() {   
  size(200, 200);   
  ellipseMode(CORNER);
} 

void draw() {   
  background(#000000);
  for (int[] arr1d : circles) {
    printArray(arr1d);
  }
  exit();
}

And circles

 




int[][] circles = { 
  {10, 15}, 
  {100, 130}, 
  {77, 43}, 
  {30, 145}, 
  {185, 17}, 
  {99, 76} 
}; 
final int DIAMETER = 20; 
final int YELLOW = #FFFF00; 
final int RED = #FF0000; 

void setup() {   
  size(200, 200);   
  ellipseMode(CORNER);
} 

void draw() {   
  background(#000000);
  for (int[] arr1d : circles) {
    ellipse(arr1d[0], arr1d[1], DIAMETER, DIAMETER);
  }
}
2 Likes