StatCounter

Saturday, April 12, 2014

I  thought I wanted to share with you a really nice piece of code I recently created in Processing (processing.org) that makes a very beautiful drawing. If you don't know what this is, refer to my older post. It isn't hard to program, and only has a few lines of code.

Here's the code:

float random1x,random1y,random2x,random2y,random3x,random3y;
int shapes = 100; 

  size(400,400);
  noStroke();
  for(int i = 0; i < shapes; i++) {
    random1x = random(0,width);
    random1y = random(0,height);
    random2x = random(0,width);
    random2y = random(0,height);
    random3x = random(0,width);
    random3y = random(0,height);
    fill(random(255),random(255),random(255),random(255));
    triangle(random1x,random1y,random2x,random2y,random3x,random3y);
  }



 Here's my results after three different tries:



The code works like this: First, the  variables randomx1,randomy1, randomy2...... define my random x,y values for the triangle coordinates. The next line, shapes = 100,shows how many shapes to draw. We are drawing 100 triangles in this example. The size(x,y) adjusts the screen to x pixels wide, y pixels long, which in my case is 400 pixels wide, 400 pixels long. The noStroke() sets the triangles to not have an annoying black border.  Next, we encounter a for loop, which iterates the variable i over the number of shapes (shapes) we'll draw. Then comes the random() statement,  which makes a random number in the range of two numbers set in the parentheses. I set the minimum to 1, and I set x,y max is respectively set to width, height, because the width is on x axis, and the height is on the y axis. The fill(r,g,b,alpha) takes an 8-bit red, green, blue, and an alpha value. The alpha value is the amount of transparency of an object. In case you're wondering, if the random() function takes one input, as in random(255), the assigned number(255) is the maximum, and it assumes that the minimum is 0. It then assigns a random 8-bit color (0-255) for r,g,b, and alpha, to the fill() statement for the triangles. The last statement, triangle(),  assigns the three random x and y's we created to make a triangle. After doing this i  number of times, we reach the end of code, and the pictures remain still.

No comments:

Post a Comment