Initial state of vector (or array?), I do not understand

I have just discovered Processing and so far it is extremely addictive and fun!
I want to create two “droplets” of points, one near the top and one near the bottom of the visible space, but for some reason I do not understand, their position match.
Here is my code:

int num_points_dropplet = 3000;
int num_points_dropplet_2 = 100;
float[] x = new float[num_points_dropplet];
float[] y = new float[num_points_dropplet];
float[] x_2 = new float[num_points_dropplet_2];
float[] y_2 = new float[num_points_dropplet_2];
float[] dist_array = new float[num_points_dropplet];
float the_min = 1000;
int min_index = 0;
float distance_x;
float distance_y;


int box_x = 800;
int box_y = 800;
void settings() {
    size(600,800);
}

void setup() {
  frameRate(1000);
  background(0);
  noFill();

  for(int i = 0; i<num_points_dropplet; i++) {
    float radius = random(15);
    float a = random(TWO_PI);    
    x[i] = box_x + int(cos(a)*radius)+random(-radius/10.0,radius/10.0);
    y[i] = box_y + int(sin(a)*radius)+random(-radius/10.0,radius/10.0);
    stroke(255);
    strokeWeight(2);
  }
  
    for(int j = 0; j<num_points_dropplet_2; j++) {
    float radius = random(10);
    float a = random(TWO_PI);
    float x_offset_2 = box_x/2.0;
    float y_offset_2 = box_y/2.0;
    //x_2[j] = 0.0;//x_offset_2 + cos(a)*radius+random(-radius/10.0,radius/10.0);   //(**Why isn't this line doing anything?**)
    //y_2[j] = y_offset_2 + sin(a)*radius+random(-radius/10.0,radius/10.0); // **Why isn't this line doing anything?**
    stroke(255,0,0);
    strokeWeight(3);
  }
  
}
void draw() {
  background(0);
  translate(width/2, height/2);
  
    for(int i=0; i<num_points_dropplet; i++) {
      int radius = int(random(15));
      float a = random(TWO_PI);    
      x[i] += cos(a)*radius+random(-radius/10.0,radius/10.0);
      y[i] += sin(a)*radius+random(-radius/10.0,radius/10.0);
      
      if (x[i] <-box_x/2) {
      x[i] = -150;
      }
      if (x[i] >box_x/2) {
      x[i] = 150;
      }
      if (y[i] <-box_y/2) {
      y[i] = -150;
      }
      if (y[i] >box_y/2) {
      y[i] = 150;
      }
      stroke(255);
      strokeWeight(2);
      point(x[i], y[i]);
}

    for(int j=0; j<num_points_dropplet_2; j++) {
      the_min = 1000; // reset value, otherwise the dots all agglutinate.
      
        for(int i=0; i<num_points_dropplet; i++) {
          dist_array[i] = dist(x_2[j], y_2[j], x[i], y[i]);
          // return the index of the min of the distances
          if (dist_array[i] < the_min) {
           the_min = dist_array[i];
           min_index = i;
          }  

      float radius = random(1);
      float a = random(TWO_PI);
      
      // Move each point a little bit toward the other group's closest point.
      distance_x = x[min_index] - x_2[j];
      if (distance_x >0) {
        x_2[j] += cos(a)*radius + distance_x / 100.0;
        
      }else{
        x_2[j] += cos(a)*radius + distance_x / 100.0;
      }
      
      distance_y = y[min_index] - y_2[j];
      if (distance_y >0) {
        y_2[j] += sin(a)*radius + distance_y / 100.0;
        
      }else{
        y_2[j] += sin(a)*radius + distance_y / 100.0;
      }
           
      if (x_2[j] <-box_x/2) {
      x_2[j] = -box_x/2;
      }
      if (x_2[j] >box_x/2) {
      x_2[j] = box_x/2;
      }
      if (y_2[j] <-box_y/2) {
      y_2[j] = -box_y/2;
      }
      if (y_2[j] >box_y/2) {
      y_2[j] = box_y/2;
      }

        }
      
      

      stroke(255,0,0);
      strokeWeight(3);
            
      point(x_2[j], y_2[j]);
}


}

As you can see in the code, there are 2 commented out lines that have no effect when uncommented out. Why is it so? And how can I control where the red points are initially spawned?

The 2 lines refer to x_2

You show them with point in draw().

But I think the } } are a bit wrong and somehow the point command is outside the for loop.

You can always use ctrl-t in processing prior to posting to get auto-indents.

Setup() is run only once. Your box values are very high so the x,y points will be outside of the canvas?

Hmm I do not quite understand your answer. To me, the two lines refer to x_2 and y_2, not to x_2 alone, am I wrong?

I do not see anything wrong with the brackets, which ones do you think are wrong?

Ok about Setup being ran once, this is good because I specify there where I want the group of red points to spawn. For some reason, it has no effect whatsoever in the visualization when the code is ran.

I also do not really understand the comment about x and y points being outside the canvas. I have used conditions that if the point new coordinates would be outside the canvas, then the point would stay on the canvas limits (this part of the code works well), so there is no point outside the canvas. There is also no agglutination there, so I do not really understand your comment. Am I missing something?

My questions still remain.

1 Like

Referring to these lines.

I feel point is outside the for loop brackets}}

Am I wrong? Or are all points from x_2 displayed?

I have no computer here so I can not test

I am also not in front of my computer, but this part looks OK to me. The line starting with point(… is inside the for j loop. All the points are displayed. What I do not understand is why they spawn at the same spot as the white points.

1 Like

The behavior of my code is actually extremely odd to me. The higher the number of white particles, the closer the red ones are going to appear to the white ones.

If I reduce the size of the box, e.g. to int box_x = 100;, int box_y = 100;, then the particles will be almost static… Very very odd to me.

Ok I spotted two bugs. One with the boundary box conditions, the other with a wrong loop (or bracket).
Working code:

int num_points_dropplet = 1000;
int num_points_dropplet_2 = 10;
float[] x = new float[num_points_dropplet];
float[] y = new float[num_points_dropplet];
float[] x_2 = new float[num_points_dropplet_2];
float[] y_2 = new float[num_points_dropplet_2];
float[] dist_array = new float[num_points_dropplet];
float the_min = 1000;
int min_index = 0;
float distance_x;
float distance_y;
int count = 0;

int box_x = 10;
int box_y = 1000;
void settings() {
  System.setProperty("jogl.disable.openglcore", "true");
  size(box_x, box_y, P2D);
}

void setup() {
  frameRate(2);
  background(0);
  noFill();

  for (int i = 0; i<num_points_dropplet; i++) {
    float radius = random(15);
    float a = random(TWO_PI);    
    x[i] = -box_x/8.0 + cos(a)*radius+random(-radius/10.0, radius/10.0);
    y[i] = -box_y/8.0 + sin(a)*radius+random(-radius/10.0, radius/10.0);
    stroke(255);
    strokeWeight(2);
  }

  for (int j = 0; j<num_points_dropplet_2; j++) {
    float radius = random(10);
    float a = random(TWO_PI);
    float x_offset_2 = box_x/2.0;
    float y_offset_2 = box_y/2.0;
    x_2[j] = x_offset_2 + cos(a)*radius+random(-radius/10.0, radius/10.0);
    y_2[j] = y_offset_2 + sin(a)*radius+random(-radius/10.0, radius/10.0);
    stroke(255, 0, 0);
    strokeWeight(3);
  }
}
void draw() {
  background(0);
  translate(width/2, height/2);
  for (int i=0; i<num_points_dropplet; i++) {
    float radius = random(15);
    float a = random(TWO_PI);    
    x[i] += cos(a)*radius+random(-radius/10.0, radius/10.0);
    y[i] += sin(a)*radius+random(-radius/10.0, radius/10.0);

    if (x[i] <-box_x/2) {
      x[i] = -box_x/2;
    }
    if (x[i] >box_x/2) {
      x[i] = box_x/2;
    }
    if (y[i] <-box_y/2) {
      y[i] = -box_y/2;
    }
    if (y[i] >box_y/2) {
      y[i] = box_y/2;
    }
    stroke(255);
    strokeWeight(2);
    point(x[i], y[i]);
  }

  for (int j=0; j<num_points_dropplet_2; j++) {
    the_min = 100000; // reset value, otherwise the dots all agglutinate.
    for (int i=0; i<num_points_dropplet; i++) {
      dist_array[i] = dist(x[i], y[i], x_2[j], y_2[j]);
      // return the index of the min of the distances
      if (dist_array[i] < the_min) {
        the_min = dist_array[i];
        min_index = i;
      }
    }  
    float radius = random(1);
    float a = random(TWO_PI);

    // Move each point a little bit toward the other group's closest point.
    distance_x = x[min_index] - x_2[j];
    if (distance_x >0) {
      x_2[j] += cos(a)*radius + distance_x / 100.0;
    } else {
      x_2[j] += cos(a)*radius + distance_x / 100.0;
    }

    distance_y = y[min_index] - y_2[j];
    if (distance_y >0) {
      y_2[j] += sin(a)*radius + distance_y / 100.0;
    } else {
      y_2[j] += sin(a)*radius + distance_y / 100.0;
    }

    if (x_2[j] <-box_x/2) {
      x_2[j] = -box_x/2;
    }
    if (x_2[j] >box_x/2) {
      x_2[j] = box_x/2;
    }
    if (y_2[j] <-box_y/2) {
      y_2[j] = -box_y/2;
    }
    if (y_2[j] >box_y/2) {
      y_2[j] = box_y/2;
    }
    stroke(255, 0, 0);
    strokeWeight(3);
    point(x_2[j], y_2[j]);
  }
}

1 Like

Hello,

Something else for you to take a look at:

    distance_x = x[min_index] - x_2[j];
    //if (distance_x >0) {
    //  x_2[j] += cos(a)*radius + distance_x / 100.0;
    //} else {
    //  x_2[j] += cos(a)*radius + distance_x / 100.0;
    //}
   //Above conditions are always:
    x_2[j] += cos(a)*radius + distance_x / 100.0;

    distance_y = y[min_index] - y_2[j];
    //if (distance_y >0) {
    //  y_2[j] += sin(a)*radius + distance_y / 100.0;
    //} else {
    //  y_2[j] += sin(a)*radius + distance_y / 100.0;
    //}
   //Above conditions are always:
    y_2[j] += sin(a)*radius + distance_y / 100.0;

:)

2 Likes