How to constrain a for loop draw to a specific area

edited February 2015 in Questions about Code

I am trying to draw a bunch of objects in a constrained area, they are being translated so that they form interesting shapes together but I cannot figure out how to constrain these shapes to one area and not the entire screen.

Comments

  • Can you post an MCVE?

  • edited February 2015

    I want two sets of bubble one set constrained to the left side the other on the right. I was able to get that picture after a while but i want it to happen more often and less random

             void setup  {
          //size of canvas
          size(420, 520);
          //background is black
          //stroke color is white
          stroke(255);
          //stroke is smooth and not pixelated
          smooth();
        }
    
        void draw()
        {
          background(0);
          BubbleDraw();
        }
    
        void BubbleDraw()
        {
          //displacing the circles in a group in the middle
          translate(width/2, height/2);
          for (int i = 0; i < 30; i++)
          {
            for (int j = 0; j < 15; j++)
            {
              //circles have no fill (press picture before page turns white)
              fill(0);
              //the angles in which the circles rotate are from -90 to 90
              float angle = random (-180, 180);
              //changes degrees to radians
              rotate (radians (angle));
              //circles made at 0,0 progressively get larger
              ellipse(0, 0, i+j, i+j);
              //moves the circles individually 
              translate(0, 30);
            }
              noLoop();
          }
        }
    
  • go back, edit your post please

    select the entire code, hit ctrl-o

  • @chrisir what would be a way to constrain these circles in a specific area (like in a rectangle area or some other crazy shape)

  • edited February 2015
    size(420, 520);
    stroke(255);
    smooth();
    background(0);
    float x, y;
    for (int i=0; i<2; i++) {
      x = width/2; //i==0?0:width;
      y = height/2;
      fill(i==0?0:255,i==1?0:255,0);
      for (int j = 0; j < 100; j++) {
        float a = random(TWO_PI);
        x+=30*cos(a);
        y+=30*sin(a);
        x=constrain(x,0,width);
        y=constrain(y,0,height);
        if (i==0) {
          ellipse(map(x, 0, width, width/2, width), y, 5+j%20, 5+j%20);
        } else {
          ellipse(map(x, 0, width, 0, width/2), y, 5+j%20, 5+j%20);
        }
      }
    }
    

    It's not easy the way you were doing it because you surrendered control of the coordinate system to randomness. This approach is better.

Sign In or Register to comment.