1

I'm trying to create a 3D grid of points in a box using Processing with PeasyCam to visualize the box. To make the grid of points, I'm using a method that I've used successfully in 2D.

import peasy.*;
PeasyCam cam;

void setup()
{
  size(1000, 800, P3D);
  background(0);
  frameRate(60);
 
  cam = new PeasyCam(this, 1200);
  cam.lookAt(boxwidth/2, boxheight/2, boxdepth/2);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);
 
  //initialize grid of points of interest
  float count = 0;
  
  grid_spacingx = grid_width / (Nx-1); //one fewer spacings than points
  grid_spacingy = grid_height / (Ny-1);
  grid_spacingz = grid_depth / (Nz-1);
  
  for(int i = 0; i<N; i++){
    px[i] = (count*grid_spacingx);
    count++;
    if(count%Nx == 0){
      count = 0;
    }
  }
  for(int i = 0; i<N; i++){
    py[i] = (count*grid_spacingy);
    count++;
    if(count%Ny == 0){
      count = 0;
    }
  } 
  for(int i = 0; i<N; i++){
    pz[i] = (count*grid_spacingz);
    count++;
    if(count%Nz == 0){
      count = 0;
    }
  }  
}


int boxwidth = 1000;
int boxheight = 1000;
int boxdepth = 1000;

//size of arrays
int Nx = 26; //number of horizontal points (makes N-1 spacings)
int Ny = 26; //number of vertical points
int Nz = 26; //number of vertical points
int N = Nx * Ny; //total number of points

float grid_width = 1000;
float grid_height = 1000;
float grid_depth = 1000;

float grid_spacingx;
float grid_spacingy;
float grid_spacingz;

//position of point of interest
float[] px = new float[N];
float[] py = new float[N];
float[] pz = new float[N];

void draw() 
{
  background(0);

  colorMode(RGB, 255);

  fill(255);
  strokeWeight(2);
  drawBox();
  drawFieldArrows();
}

//******************************************************//

void drawFieldArrows() {
  fill(255);
  for(int i = 0; i<N; i++){
    pushMatrix();
    translate(px[i], py[i], pz[i]);
    point(0, 0, 0); 
    popMatrix(); 
  }
}

//******************************************************//

void drawBox() {
  stroke(255);
  noFill();

  line(0, 0, 0, boxwidth, 0, 0);
  line(0, 0, 0, 0, boxheight, 0);
  line(0, 0, 0, 0, 0, boxdepth);
  line(0, 0, boxdepth, boxwidth, 0, boxdepth);
  line(0, 0, boxdepth, 0, boxheight, boxdepth);
  
  line(boxwidth, boxheight, boxdepth, boxwidth, boxheight, 0);
  line(boxwidth, boxheight, boxdepth, 0, boxheight, boxdepth);  
  line(boxwidth, boxheight, boxdepth, boxwidth, 0, boxdepth);   

  line(boxwidth, 0, 0, boxwidth, boxheight, 0);   
  line(boxwidth, 0, 0, boxwidth, 0, boxdepth); 
  
  line(0, boxheight, 0, boxwidth, boxheight, 0);   
  line(0, boxheight, 0, 0, boxheight, boxdepth);     
  
  //fill(135,206,250, 100);
  //rect(0, 0, boxwidth, boxheight);
  //translate(0, 0, boxdepth);
  //rect(0, 0, boxwidth, boxheight);  
}

For some reason in 3D, it produces a single diagonal line of points instead of a complete grid. Any insight would be greatly appreciated!

I've copied code that worked perfectly in 2D (x, y only), and I've tried to add in the z dimension, to no avail.

1 Answer 1

1

The following source code uses a two dimensional grid of points placed inside of the box and then successively translated by the z axis in a 'for' loop to reach the other side of the box, creating a 3 dimensional grid of points. I used the following reference: https://processing.org/tutorials/p3d . It may be possible to simplify your code by using this technique.

import peasy.*;
PeasyCam cam;

int boxwidth = 1000;
int boxheight = 1000;
int boxdepth = 1000;

//size of arrays
int Nx = 26; //number of horizontal points (makes N-1 spacings)
int Ny = 26; //number of vertical points
int Nz = 26; //number of vertical points
int N = Nx * Ny * Nz; //total number of points

float grid_width = 1000;
float grid_height = 1000;
float grid_depth = 1000;

float grid_spacingx;
float grid_spacingy;
float grid_spacingz;

//position of point of interest
float[] px = new float[N];
float[] py = new float[N];
float[] pz = new float[N];

void setup() {
  size(1000, 800, P3D);
  background(0);
  frameRate(60);

  cam = new PeasyCam(this, 1200);
  cam.lookAt(boxwidth/2, boxheight/2, boxdepth/2);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);

  //initialize grid of points of interest
  float count = 0;

  grid_spacingx = grid_width / (Nx-1); //one fewer spacings than points
  grid_spacingy = grid_height / (Ny-1);
  grid_spacingz = grid_depth / (Nz-1);

  for (int i = 0; i<N; i++) {
    px[i] = (count*grid_spacingx);
    count++;
    if (count%Nx == 0) {
      count = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    py[i] = (count*grid_spacingy);
    count++;
    if (count%Ny == 0) {
      count = 0;
    }
  }

  for (int i = 0; i<N; i++) {
    pz[i] = (count*grid_spacingz);
    count++;
    if (count%Nz == 0) {
      count = 0;
    }
  }
}

void draw() {
  background(0);
  colorMode(RGB, 255);
  fill(255);
  strokeWeight(3);
  drawBox();
  drawFieldArrows();
}

//******************************************************//

void pointGrid(int colGap, int rowGap) {
  int _numRows = 26;
  int _numCols = 26;
  for (int k = 0; k < _numRows; k++) {
    for (int j = 0; j < _numCols; j++) {
      int x = j*(colGap);
      int y = k*(rowGap);
      stroke(0, 0, 255);
      strokeWeight(3);
      point(x, y);
    }
  }
}

void drawFieldArrows() {
  fill(255);

  for (int i = 0; i < 40; i++) {
    pushMatrix();
    translate(0, 0, 26*i);
    pointGrid(40, 40);
    popMatrix();
  }
}

//******************************************************//

void drawBox() {
  stroke(255);
  noFill();

  line(0, 0, 0, boxwidth, 0, 0);
  line(0, 0, 0, 0, boxheight, 0);
  line(0, 0, 0, 0, 0, boxdepth);
  line(0, 0, boxdepth, boxwidth, 0, boxdepth);
  line(0, 0, boxdepth, 0, boxheight, boxdepth);

  line(boxwidth, boxheight, boxdepth, boxwidth, boxheight, 0);
  line(boxwidth, boxheight, boxdepth, 0, boxheight, boxdepth);
  line(boxwidth, boxheight, boxdepth, boxwidth, 0, boxdepth);

  line(boxwidth, 0, 0, boxwidth, boxheight, 0);
  line(boxwidth, 0, 0, boxwidth, 0, boxdepth);

  line(0, boxheight, 0, boxwidth, boxheight, 0);
  line(0, boxheight, 0, 0, boxheight, boxdepth);

}

Addendum:

Simplified version follows:

import peasy.*;

PeasyCam cam;

int boxwidth = 500;
int boxheight = 500;
int boxdepth = 500;

void pointGrid(int colGap, int rowGap) {
  int _numRows = 13;
  int _numCols = 13;
  for (int k = 0; k < _numRows; k++) {
    for (int j = 0; j < _numCols; j++) {
      int x = j*(colGap);
      int y = k*(rowGap);
      stroke(0, 0, 255);
      strokeWeight(3);
      point(x, y);
    }
  }
}

void drawBox() {
  stroke(255);
  strokeWeight(3);
  
  line(0, 0, 0, boxwidth, 0, 0);
  line(0, 0, 0, 0, boxheight, 0);
  line(0, 0, 0, 0, 0, boxdepth);
  line(0, 0, boxdepth, boxwidth, 0, boxdepth);
  line(0, 0, boxdepth, 0, boxheight, boxdepth);

  line(boxwidth, boxheight, boxdepth, boxwidth, boxheight, 0);
  line(boxwidth, boxheight, boxdepth, 0, boxheight, boxdepth);
  line(boxwidth, boxheight, boxdepth, boxwidth, 0, boxdepth);

  line(boxwidth, 0, 0, boxwidth, boxheight, 0);
  line(boxwidth, 0, 0, boxwidth, 0, boxdepth);

  line(0, boxheight, 0, boxwidth, boxheight, 0);
  line(0, boxheight, 0, 0, boxheight, boxdepth);
}

void setup() {
  size(1000, 800, P3D);
  cam = new PeasyCam(this, 1200);
  cam.lookAt(boxwidth/2, boxheight/2, boxdepth/2);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(2000);
}

void draw() {
  background(0);
  drawBox();
  for (int i = 0; i < 20; i++) {
    pushMatrix();
    translate(0, 0, 26*i); // grid spacing = 26
    pointGrid(40, 40); // col,row spacing = 40,40
    popMatrix();
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.