A basic Random Composition • tim rodenbröker creative coding

A basic Random Composition

Courses / Random Compositions / A basic Random Composition

In this lesson you will learn how to program a composition of randomly arranged elements in Processing and p5.js. This allows you to generate an almost infinite range of motifs.

Processing

color bg = #f1f1f1;
color fg = #000000;

void setup() {
  size(900, 900);
  // frameRate(2);
  strokeCap(RECT);
}

void draw() {
  background(bg);

  // Circle

  float x = random(width);
  float y = random(height);
  float diameter = random(50, 300);

  noStroke();
  fill(fg);
  circle(x, y, diameter);

  // Line 1

  float x1 = random(width);
  float y1 = random(height);
  float x2 = random(width);
  float y2 = random(height);

  stroke(fg);
  strokeWeight(5);
  line(x1, y1, x2, y2);

  // Line 2

  float line2x1 = random(width);
  float line2y1 = random(height);
  float line2x2 = random(width);
  float line2y2 = random(height);

  stroke(fg);
  strokeWeight(150);
  line(line2x1, line2y1, line2x2, line2y2);
  
  saveFrame("out/####.png");
}

p5.js

let bg, fg;

function setup() {
  createCanvas(900, 900);
  // frameRate(2);
  strokeCap(SQUARE);
  
  bg = color('#f1f1f1');
  fg = color('#000000');
}

function draw() {
  background(bg);

  // Circle
  let x = random(width);
  let y = random(height);
  let diameter = random(50, 300);

  noStroke();
  fill(fg);
  circle(x, y, diameter);

  // Line 1
  let x1 = random(width);
  let y1 = random(height);
  let x2 = random(width);
  let y2 = random(height);

  stroke(fg);
  strokeWeight(5);
  line(x1, y1, x2, y2);

  // Line 2
  let line2x1 = random(width);
  let line2y1 = random(height);
  let line2x2 = random(width);
  let line2y2 = random(height);

  stroke(fg);
  strokeWeight(150);
  line(line2x1, line2y1, line2x2, line2y2);

  saveCanvas("out", "png");
}

Curated Output

It is worth investing time in curating the results. It is fascinating how concrete some of the motifs appear. Despite the strong abstraction, a closer look at the generated motifs reveals human bodies doing various activities.

Published by Tim on Wednesday September 6, 2023

Last modified on May 1st, 2024 at 7:07

  1. Recap: The random() Function
  2. A basic Random Composition
  3. Adding Space
  4. A simple way to animate Random Compositions
  5. Random Conditions / Probability
  6. Adding Color
  7. Positive-Negative (with Functional Programming)
  8. Constants and Variables
  9. Random Face Generator
  10. Adding Images
  11. From 2D to 3D
  12. Wrapping Up