Construct2
See /f0blog/useless-supercollider-class-no-2/
//
// Construct2
//
// Copyright (c) 2001 Fredrik Olofsson Musikproduktion. All rights reserved.
//
// 040920 ported to processing
// 200801 updated for processing3
//----------------------------------------------------------------------------------------
int c;
int step_X, step_Y;
int x= 10;
int y= 10;
int dx= 1;
int dy= 1;
int v= 1;
int m= 1;
int col= 4;
int row= 3;
int mov_X= 95;
int mov_Y= 95;
int max_X;
int max_Y;
void setup() {
size(640, 480);
frameRate(60);
max_X= width;
max_Y= height;
background(255);
nytt();
}
void mouseClicked() {
background(255);
nytt();
}
void keyTyped() {
if (key==' ') {
background(255);
nytt();
}
}
void draw() {
if (int(random(1881))==0) { //lite då och då
background(255); //rensa skärmen
nytt(); //nya värden
}
stroke(c, 55, 66);
drawFO(); //rita punkt(er)
x= x + (dx * step_X); //flytta position x
if (x < 1) //om vänster kant
dx= int(random(2)) % 2; //vänd eller stå still i x-led
else if (x > (max_X / col)) //om höger kant
dx= 0 - int(random(2) % 2); //vänd eller stå still i x-led
y= y + (dy * step_Y); //flytta position y
if (y < 1) //om övre kanten
dy= int(random(2) % 2); //vänd eller stå still i y-led
else if (y > (max_Y / row)) //om nedre kanten
dy= 0 - int(random(2) % 2); //vänd eller stå still i y-led
switch (v) {
case 1:
variant1();
break;
case 2:
variant2();
break;
case 3:
variant3();
break;
case 4:
variant4();
break;
case 5:
variant5();
break;
case 6:
variant6();
break;
}
}
//----------------------------------------------------------------------------------------
void variant1() { //1. slumpa riktningar individuellt
if (random(100) >= mov_X)
slumpaRiktningX();
if (random(100) >= mov_Y)
slumpaRiktningY();
}
void variant2() { //2. slumpa alltid riktningar tillsammans
slumpaRiktningXY();
}
void variant3() { //3. slumpa riktningar individuellt
if (random(100) >= mov_X) {
if (dx == 0 && x >= 1) //dra åt vänster
dx= -1;
else
dx= 0;
}
if (random(100) >= mov_Y) {
if (dy == 0 && y >= 1) //dra uppåt
dy= -1;
else
dy= 0;
}
}
void variant4() { //4. slumpa riktningar individuellt
if (random(100) >= mov_X) {
if (dx == 0 && x >= 1) //dra åt vänster
dx= -1;
else
dx= 0;
}
if (random(100) >= mov_Y) {
if (dy == 0 && y <= max_Y / row) //dra nedåt
dy= 1;
else
dy= 0;
}
}
void variant5() { //5. slumpa riktningar individuellt
if (random(100) >= mov_X) {
if (dx == 0 && x <= max_X / col) //dra åt höger
dx= 1;
else
dx= 0;
}
if (random(100) >= mov_Y) {
if (dy == 0 && y >= 1) //dra uppåt
dy= -1;
else
dy= 0;
}
}
void variant6() { //6. slumpa riktningar individuellt
if (random(100) >= mov_X) {
if (dx == 0 && x <= max_X / col) //dra åt höger
dx= 1;
else
dx= 0;
}
if (random(100) >= mov_Y) {
if (dy == 0 && y <= max_Y / row) //dra nedåt
dy= 1;
else
dy= 0;
}
}
//----------------------------------------------------------------------------------------
void nytt() {
v= int(random(6)) + 1;
m= int(random(16)) + 1;
col= int(random(12)) + 1;
row= int(random(7)) + 1;
step_X= 1;
//step_X= int(random(2)) + 1;
step_Y= 1;
//step_Y= int(random(2)) + 1;
slumpaRiktningXY();
c= int(random(255));
}
void slumpaRiktningXY() {
while ((dx==0) && (dy==0)) { //kolla att inte båda riktningar blir 0
dx= int(random(2));
dy= int(random(2));
}
}
void slumpaRiktningX() {
dx= int(random(2));
}
void slumpaRiktningY() {
dy= int(random(2));
}
//----------------------------------------------------------------------------------------
void drawFO() {
for (int i= 0; i < col; i++) {
int iCol= round((max_X / col) * i);
for (int j = 0; j < row; j++) {
int jRow= round((max_Y / row) * j);
switch (m) { //kolla mapping
case 1:
mapping1(i, j, iCol, jRow);
break;
case 2:
mapping2(i, j, iCol, jRow);
break;
case 3:
mapping3(i, j, iCol, jRow);
break;
case 4:
mapping4(i, j, iCol, jRow);
break;
case 5:
mapping5(i, j, iCol, jRow);
break;
case 6:
mapping6(i, j, iCol, jRow);
break;
case 7:
mapping7(i, j, iCol, jRow);
break;
case 8:
mapping8(i, j, iCol, jRow);
break;
case 9:
mapping9(i, j, iCol, jRow);
break;
case 10:
mapping10(i, j, iCol, jRow);
break;
case 11:
mapping11(i, j, iCol, jRow);
break;
case 12:
mapping12(i, j, iCol, jRow);
break;
case 13:
mapping13(i, j, iCol, jRow);
break;
case 14:
mapping14(i, j, iCol, jRow);
break;
case 15:
mapping15(i, j, iCol, jRow);
break;
case 16:
mapping16(i, j, iCol, jRow);
break;
}
}
}
}
//----------------------------------------------------------------------------------------
void mapping1(int i, int j, int iCol, int jRow) { //no flip
point(x + iCol, y + jRow);
}
void mapping2(int i, int j, int iCol, int jRow) { //flip all x
point(mirrorX(x) + iCol, y + jRow);
}
void mapping3(int i, int j, int iCol, int jRow) { //flip all y
point(x + iCol, mirrorY(y) + jRow);
}
void mapping4(int i, int j, int iCol, int jRow) { //flip all xy
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
}
//--
void mapping5(int i, int j, int iCol, int jRow) { //flip odd col x
if (i % 2 == 1)
point(mirrorX(x) + iCol, y + jRow);
else
point(x + iCol, y + jRow);
}
void mapping6(int i, int j, int iCol, int jRow) { //flip odd col y
if (i % 2 == 1)
point(x + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
void mapping7(int i, int j, int iCol, int jRow) { //flip odd col xy
if (i % 2 == 1)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
//--
void mapping8(int i, int j, int iCol, int jRow) { //flip odd row x
if (j % 2 == 1)
point(mirrorX(x) + iCol, y + jRow);
else
point(x + iCol, y + jRow);
}
void mapping9(int i, int j, int iCol, int jRow) { //flip odd row y
if (j % 2 == 1)
point(x + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
void mapping10(int i, int j, int iCol, int jRow) { //flip odd row xy
if (j % 2 == 1)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
//--
void mapping11(int i, int j, int iCol, int jRow) { //flip odd col & even row x, flip even col & odd row x
if (i % 2 == 1 && j % 2 == 0)
point(mirrorX(x) + iCol, y + jRow);
else if (i % 2 == 0 && j % 2 == 1)
point(mirrorX(x) + iCol, y + jRow);
else
point(x + iCol, y + jRow);
}
void mapping12(int i, int j, int iCol, int jRow) { //flip odd col & even row y, flip even col & odd row y
if (i % 2 == 1 && j % 2 == 0)
point(x + iCol, mirrorY(y) + jRow);
else if (i % 2 == 0 && j % 2 == 1)
point(x + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
void mapping13(int i, int j, int iCol, int jRow) { //flip odd col & even row xy, flip even col & odd row xy
if (i % 2 == 1 && j % 2 == 0)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else if (i % 2 == 0 && j % 2 == 1)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
//--
void mapping14(int i, int j, int iCol, int jRow) { //flip even col & even row x, flip odd col & odd row x
if (i % 2 == 0 && j % 2 == 0)
point(mirrorX(x) + iCol, y + jRow);
else if (i % 2 == 1 && j % 2 == 1)
point(mirrorX(x) + iCol, y + jRow);
else
point(x + iCol, y + jRow);
}
void mapping15(int i, int j, int iCol, int jRow) { //flip even col & even row y, flip odd col & odd row y
if (i % 2 == 0 && j % 2 == 0)
point(x + iCol, mirrorY(y) + jRow);
else if (i % 2 == 1 && j % 2 == 1)
point(x + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
void mapping16(int i, int j, int iCol, int jRow) { //flip even col & even row xy, flip odd col & odd row xy
if (i % 2 == 0 && j % 2 == 0)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else if (i % 2 == 1 && j % 2 == 1)
point(mirrorX(x) + iCol, mirrorY(y) + jRow);
else
point(x + iCol, y + jRow);
}
//----------------------------------------------------------------------------------------
int mirrorX(int ox) {
return round((max_X / col) - ox);
}
int mirrorY(int oy) {
return round((max_Y / row) - oy);
}