01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Class which represents a single loop
class Loop {
// constructor
Loop(float cx, float cy, float r, int n) {
_hue = random(0,255);
_r = r;
_cx = cx;
_cy = cy;
_n = n;
_x = new float[_n];
_y = new float[_n];
for (int i=0; i<_n; ++i) {
float ang = 2. * 3.14159265358979323 * (float)i / _n;
_x[i] = _cx+ r*cos(ang);
_y[i] = _cy + r*sin(ang);
}
}
// move the loop
void move(float mx, float my) {
mx -= _cx;
my -= _cy;
float ml = sqrt(mx*mx+my*my);
if (ml > 0.) {
mx = mx/ml;
my = my/ml;
}
for (int i=0; i<_n; ++i) {
float vx = _x[i] - _cx;
float vy = _y[i] - _cy;
float l = sqrt(vx*vx+vy*vy);
if (l > 0.) {
vx = vx/l;
vy = vy/l;
float dot = vx * mx + vy * my;
dot = constrain(dot, 0., 1.);
l += 12. * (noise(_x[i], _y[i]) - .5);
l = dot*l + (1.-dot)*_r;
if (l < .0001) {
l = .0001;
}
_x[i] = _cx + vx * l;
_y[i] = _cy + vy * l;
}
}
_cx += random(-2.,2.);
_cy += random(-2.,2.);
}
// draw the loops
void draw(PGraphics2D pg) {
pg.stroke(_hue, 192, 128);
pg.noFill();
pg.beginShape();
for (int i=0; i<_n; ++i) {
pg.vertex(_x[i], _y[i]);
}
pg.vertex(_x[0], _y[0]);
pg.endShape();
}
int _n;
float _r;
float _cx;
float _cy;
float _x[];
float _y[];
float _hue;
};
Loop loops[];
PGraphics2D pg;
boolean firstFrame = true;
void setup() {
loops = new Loop[12];
for (int i=0; i<12; ++i) {
loops[i] = new Loop(random(100.,300.),random(100.,300.),
random(50.,150.), 300);
}
size(400,400);
pg = (PGraphics2D)createGraphics(400,400,P2D);
pg.colorMode(HSB, 1);
noiseDetail(8, .6);
}
void draw() {
pg.beginDraw();
if (firstFrame) {
pg.background(0,0,0);
firstFrame = false;
}
else {
pg.background(0,0,0,4);
}
for (int i=0; i<loops.length; ++i) {
loops[i].draw(pg);
}
for (int i=0; i<loops.length; ++i) {
loops[i].move(mouseX, mouseY);
}
pg.endDraw();
image(pg,0,0);
}