Programming for Visual Artists

ARTS-A0701 – Digital Art 1 (2cr)

2024 - Lecture 5

Today

  • If-else
  • For-loop

Pearson, Matt (2011). Generative art: a practical guide using processing. Simon and Schuster. (p. 78, 81)

if-else

let x = 0;
let speed = 10;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  if (x < width) {
    x += speed;
  } else {
    x = 0;
  }
  ellipse (x, height/2, 50, 50);
}
let x = 0;
let speed = 10;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  if (x > width || x < 0) {
	speed *= -1;
  }
  x += speed;
  ellipse (x, height/2, 50, 50);
}
let x = 0;
let speed = 10;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  if (x < 0 || x > width) {
    speed = speed * -1;
  }
  x += speed;
  ellipse(x, height / 2, 50, 50);
}

for

let x = 0;
x+=2;
x+=2;
x+=2;
x+=2;
x+=2;
print(x);
let x = 0;

for (let i = 0; i < 5; i++) {
  x += 2;
}
print(x);
for (let i = 0; i < 10; i++) {
  // your code here
}

Initalization

Condition

Increment