Generating random x AND y point values

edited March 2014 in Questions about Code

Hi,

I am new to processing and am trying to generate random points in 2D with nested for loops, without using arrays (it's a requirement). With the code I'm using, depending on the order of the for loops, I get either random x values, with the same y value, and conversely. My goal is to have a couple of random x and y values for each point. Ultimately, it should be in 3D, but for the moment, I'll be content if I can manage that in 2D. Here is the code (I don't think I got how to properly copy paste it from the forum's instructions, sorry). I commented parts of the code that I couldn't use but they might be part of the solution.

size (640, 360);
background(0);
noStroke();

// a = 0;
//float b = 0;

int numpoints = 10;
// random incremental value for x and y 
//for (int i=0; i <numpoints; i++){
//a = random (0, 40);

//println (a);
//}
//for (int j=0; j <numpoints; j++){
//b = random (0, 40);

//println (b);
//}


float firstvalx = random (1, 10);
//float lastvalx = random (20, 30);
//float incremx = a;
float firstvaluey = random (1, 10);
//float lastvaluey = random (20, 30);
//float incrementy = b;

for (float x=firstvalx; x<600; x+= random(0, 100)){
  for (float y=firstvaluey; y<300; y+= random (0, 100)){
    strokeWeight (8);
    stroke (255);
    point (x,y);
  }
Tagged:

Answers

  • This does not use a double for loop, but I don't see why you would need to either. The idea is to just have a loop that runs some number of times (given my numpoints) and picks a random (x, y) each time. I used ellipse() because it is easier to see than point():

    int numpoints = 10;
    
    void setup() {
      size(640, 360);
    
      background(255);
    
      for (int i = 0; i < numpoints; i++) {
        ellipse(random(width), random(height), 3, 3);
      }
    }
    
  • edited March 2014

    The simplest solutions ae the best, but not easy to figure sometimes, and your solution is probably the most logical. However, my idea is to avoid repetitions in the random generation. I had tried your solution before, but the risk is to get ellipses or points superimposed on each other, don't you think?

  • Yes, but that is because it is random. In order to avoid that you would need structured randomness or a condition to avoid placing new points if they are too close to another one.

  • Yes, of course. And this is precisely what I cannot figure out. All the examples I have seen so far are based on arrays.

  • edited March 2014

    Arrays (or ArrayLists) are (probably) used to know where the previously placed ones are so that you don't place another there. Why can't you use arrays?

    Edit: I guess my point in asking is that you cannot avoid placing new random points on previous points without knowing / storing the location of previous points.

  • edited April 2014

    when you just draw the points, you can of course use get() to check whether the area you want to place the point in is free

    since your ellipse has a diameter of 3 you need to check 3x3 points

  • here...

    int numpoints = 1000;
    
    void setup() {
      size(640, 360);
    
      background(255);
    
      fill (0);
      int i=0;
      // for (int i = 0; i < numpoints; i++) {
      while ( i < numpoints ) {
        int x=int(random(width-3));
        int y=int(random(height-3));
        if (get((x), (y)) == color(255) && 
          get((x+1), (y+1)) == color(255) &&
          get((x), (y+1)) == color(255) &&
          get((x+2), (y+1)) == color(255) &&
          get((x+2), (y+2)) == color(255)&&
          get((x+2), (y+3)) == color(255)&&
          get((x+3), (y+3)) == color(255)
          )
        {
          ellipse(x, y, 3, 3);
        }
        else {
          println ("No");
        }
        i++;
      } // for / while
    }
    
    void draw() {
      //
    }
    
  • @Chrisir, get() accesses pixels[] which is an array

  • Shhh.. don't tell

    I think the solution would count with his teacher...

    ;-)

  • ... would be accepted I mean...

Sign In or Register to comment.