Having a problem with zero length arrays


I’ve formatted all my vectors to be 1 column and 4 rows, but apparently I’m putting a vector of length 0 into my function? Why might this happen?
Thanks,

The problem is that you are creating and initialising a 1D array (column) of 4 zero length arrays (rows).

If you try this code it demonstrates the problem

float a[][] = {{}, {}, {}, {}};
                       // OUTPUT
println(a.length);     // 4 
println(a[0].length);  // 0

a[0][0] = 3.142;       // ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
2 Likes

This code works

float a[][] = {{0}, {0}, {0}, {0}};
// OUTPUT
println(a.length);     // 4
println(a[0].length);  // 0

a[0][0] = 3.142;
println(a[0][0]);      // 3.142
3 Likes

Thank you for your response.

I am trying develop this code to render 3D objects. It is still not helping. Do you want to send you whole code and you look at it.

Here is the code.
int scr_width = 1200;
int scr_height = 800;
int x_coord = 0;
int y_coord = 0;
int z_coord = 0;
int cube = new int[8];
int cube_list = {{1, 2, 3, 4}, {1, 2, 6, 5}, {1, 4, 8, 5}, {2, 3, 7, 6}, {3, 4, 8, 7}, {5, 6, 7, 8}};
int cube_portray = new int[8][2];
int center = {50, 50, 50};
int cube_radius = 20;
float x_angle = 0;
float y_angle = 0;
float z_angle = 0;
float fov = PI/2;
float aspect = scr_width/scr_height;
float near = 0;
float far = 0;
float speed = 1;
float cube_corner(){
float c = new float[8][3][1];
int f = 0;
for(int i = -1; i < 2; i += 2){
for(int j = -1; j < 2; j += 2){
for(int k = -1; k < 2; k += 2){
c[f][0][0] = (float)center[0]+cube_radiusi;
c[f][1][0] = (float)center[1]+cube_radius
j;
c[f][2][0] = (float)center[2]+cube_radiusk;
f += 1;
}
}
}
return c;
}
float[][] matmul(float[][] a, float[][] b){
float c[][] = new float[a.length][b[0].length];
for (int i = 0; i < a.length; i++){
float d[] = new float[a.length];
for (int j = 0; j < b[0].length; j++){
float e = 0;
for (int k = 0; k < b.length; k++){
e += a[i][k]b[k][j];
}
d[j] = e;
}
c[i] = d;
}
return c;
}
float[][] projmul(float[][] a){
float projection[][] = {{1/(aspect
tan(fov/2)), 0, 0, 0}, {0, 1/(tan(fov/2)), 0, 0}, {}, {}};
float x_rot[][] = {{1, 0, 0, 0}, {0, cos(x_angle), -sin(x_angle), 0}, {0, sin(x_angle), cos(x_angle), 0}, {0, 0, 0, 1}};
float y_rot[][] = {{cos(y_angle), 0, sin(y_angle), 0}, {0, 1, 0, 0}, {-sin(y_angle), 0, cos(y_angle), 0}, {0, 0, 0, 1}};
float z_rot[][] = {{cos(z_angle), -sin(z_angle), 0, 0}, {sin(z_angle), cos(z_angle), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
float viewport[][] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
float view[][] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
a = matmul(viewport, a);
a = matmul(view, a);
a = matmul(x_rot, a);
a = matmul(y_rot, a);
a = matmul(z_rot, a);
a = matmul(projection, a);
return a;
}
void keyPressed(){
if(key == ‘w’){
x_coord += speed
cos(x_angle);
z_coord += speedsin(x_angle);
}
if(key == ‘a’){
x_coord -= speed
sin(x_angle);
z_coord += speedcos(x_angle);
}
if(key == ‘s’){
x_coord -= speed
cos(x_angle);
z_coord -= speedsin(x_angle);
}
if(key == ‘d’){
x_coord += speed
sin(x_angle);
z_coord -= speed*cos(x_angle);
}
}
void setup(){
size(1200, 800);
}
void draw(){
float coords_list = cube_corner();
float proj_list = new float[8][4][1];
for(int i = 0; i < coords_list.length; i++){
float vec = projmul(coords_list[i]);
for(int j = 0; j < 3; j++){
proj_list[i][j][0] = vec[j][0];
}
proj_list[i][4][0] = 1;
}
background(0);
for(int i : cube_list){
beginShape();
for(int j = 0; j < 4; j++){
vertex(proj_list[i[j]][0][0], proj_list[i[j]][1][0]);
}
endShape();
}
}

Welcome to the forum when you paste code it is best to format your code to make it easier for others to read. You can edit your last post to format the code rather than re-post.

I see in the code you have specified the details of a 3D cube so are you planning to render

  1. simple 3D geometric shapes e.g. cuboids, ellipsoids, cones, tubes etc, or
  2. complex 3D models e.g. those used in games etc.

If the first option do you

  1. want to create the source code from scratch, or
  2. are you prepared to use a contributed library to do the hard work.

If the later then I can recommend a truly excellent library, Shapes3D it comes with good examples and supporting website. It can even be installed in the PDE using the contributions manager :grin: Sorry about the plug but as the library creator I am a little biased. :innocent:

If the former then I suggest you use PVector and PMatrix3D to represent and manipulate 3D coordinates rather than 2D arrays directly.

Creating and manipulating 2D arrays in Java is mind bending stuff. In the code below two arrays are declared and defined , a and b , they look similar but produce orthogonal arrays

float a[][] = new float[1][4];
float b[][] = {{0}, {0}, {0}, {0}};

void setup() {
  a[0][3] = 3.142;
  b[3][0] = 2.728;
  a2dPrint("Array 'a'", a); 
  a2dPrint("Array 'b'", b); 
}

void a2dPrint(String title, float[][] array) {
  println("######", title, "#####");
  println("   Size", array.length, "x", array[0].length);
  for (int i = 0; i < array.length; i++) {
    print("    ");
    for (int j = 0; j < array[i].length; j++) {
      print(array[i][j], "  ");
    }
    println();
  }
  println("---------------------------------------");
}

Output:

###### Array 'a' #####
   Size 1 x 4
    0.0   0.0   0.0   3.142   
---------------------------------------
###### Array 'b' #####
   Size 4 x 1
    0.0   
    0.0   
    0.0   
    3.142   
---------------------------------------
2 Likes

The following technique will print the arrays in a similar manner:

float[][] a = new float[1][4];
float b[][] = {{0}, {0}, {0}, {0}};

void setup() {
  a[0][3] = 3.142;
  b[3][0] = 2.728;
  printArray(a);
  for (int i = 0; i < a.length; i++) {
    println(a[i]);
  }
  printArray(b);
  for (int i = 0; i < b.length; i++) {
    println(b[i]);
  }
}

Output:

I would like to see a screenshot of the posted code’s output. If there is a reference for the code I would like to see that also. To reiterate, the original post needs to be formatted because copy pasting leaves out some important characters.

True but the arrays are still orthogonal 1x4 versus 4x1 which is a problem if the user mixes and matches the two array definitions in the same sketch.

1 Like