Functions Problem; Rows of squares

Hi does anyone know how to do rows of squares using functions?

This is what I have so far:

void setup()
{
   size(500,500);
   background(255);
}

void draw()
{
   background(255);
   stroke(5);

   int x = 10;
   int y = 10;
   int w = 10;
   int h = 10;

   for (int i = 0; i < 12; i++)
   {
      fill(distance(int(random(150, 255)), int(random(0, 30)), 
      int(random(0, 30)), 0));
      rect(x,y,w,h);
      x=x+w; 
   }
}

I will take a look, but please, format your code with </>

row
You want to do something like this but with squares and not lines right?
(just for the top row)

Yes, basically I am trying to do a pixel art. I just want to know how to do rows of squares in a function.

int width = 500;
int height = 500;
int square_widht = 25;

void setup(){
  size(500,500);
  //noStroke();
  strokeWeight(0.1);

}

void draw(){
for(int i = 0;i < width;i = i +  square_widht ){
rect(i,0,square_widht,square_widht);
}
}

I dont understand for what you need rand() and dist equations,
so I made this simple code that you can adapt to your code

1 Like

@Sky give you a good start,
but again, you use the word “pixel art”
and want make random fill color for each rectangle
( what leads to 60 times different color ( flickering ) per second

  • unless you do it in setup
  • or if do it in draw must switch to
    noLoop();
  • or make a array for all rectangles with a memory of the color.

)

add i wanted to stay more close to your code but
needed to

  • move the variable declaration to top
  • move constant settings into setup

also instead of one row you can easy do a complete grid with some math trick
https://processing.org/reference/modulo.html
https://processing.org/reference/floor_.html

// https://discourse.processing.org/t/functions-problem-rows-of-squares/7231
int x = 10;
int y = 10;
int w = 10;
int h = 10;

int many = 144;
int grid = 12;

void setup() {
  size(500, 500);
  stroke(0, 0, 200);                        // border line blue
  strokeWeight(0.2);                        // border line width
  noLoop();
}

void draw() {
  background(255);
  for (int i = 0; i < many; i++) {
    fill(random(150, 255), random(150, 255), random(150, 255), 255);
//    rect(x+i*w, y, w, h);                          // for one row only
    rect(x+(i%grid)*w, y+(floor(i/grid))*h, w, h);   // for grid
  }
}



if you want to test about the ALPHA
you could use a background image
( like from the basic example “load display image” )

1 Like