-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranches.pde
187 lines (170 loc) · 5.2 KB
/
Branches.pde
1
2
3
4
5
6
7
8
9
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// branches visualization by stefan wagner (andsynchrony)
// WIP!!!!!!111
class Branches implements Visualization
{
ArrayList branches;
float velX, velY;
PImage gradient;
Branches(int num, float size_x, float size_y)
{
setup(num, size_x, size_y);
}
void setup()
{
println("WARNING: set up with empty handler");
}
void setup(PApplet parent)
{
println("WARNING: set up with empty handler");
}
void setup(int num, float size_x, float size_y)
{
branches = new ArrayList();
for (int i = 0; i < num*2; i ++) {
randomSeed(i);
branches.add(new Branch2D(190+(i%4)*200, 80+(i/4)*200, random(0,360), random(0,180), i*10));
}
gradient = loadImage("gradient.png");
}
void draw(PGraphics canvas, float[] average)
{
canvas.beginDraw();
canvas.colorMode(HSB);
canvas.imageMode(CENTER);
canvas.fill((frameCount*8)%360, 360, 360, 3);
canvas.rect(0,0,width,height);
canvas.strokeCap(ROUND);
//canvas.blendMode(ADD);
for (int i = 0; i < branches.size (); i ++)
{
velX = (noise(8768, average[i%average.length])-0.5)*10;
velY = (noise(6875, average[i%average.length])-0.5)*10;
//velX = average[i]*0.12;
//velY = average[i]/0.12;
Branch2D branch = (Branch2D) branches.get(i);
branch.generate(canvas, velX, velY, gradient);
}
canvas.blendMode(NORMAL);
canvas.colorMode(RGB);
canvas.endDraw();
}
void draw(PGraphics canvas, float[] average, boolean beat) {
draw(canvas, average);
}
}
class Branch2D {
float gravity = 0.0;
float mass = 6.0;
int numsprings = 25;
ArrayList springs;
float velX, velY;
float posX, posY;
float dirX, dirY;
Branch2D(float posX, float posY, float dirX, float dirY, int randomVal) {
this.posX = posX;
this.posY = posY;
this.dirX = dirX;
this.dirY = dirY;
springs = new ArrayList();
for (int i = 0; i < numsprings; i += 5) {
randomSeed(randomVal+i);
if (i==0) {
springs.add(new Spring2D(posX, posY, mass, gravity, true));
} else {
springs.add(new Spring2D(random(-dirX, dirX), random(-dirY, dirY), mass, gravity, false));
}
for (int j=1; j<4; j++) {
springs.add(new Spring2D(random(-5, 5), random(-4, 6), mass, gravity, false));
}
}
}
void generate(PGraphics canvas, float velX, float velY, PImage img) {
Spring2D firstspring = (Spring2D) springs.get(0);
firstspring.update(posX, posY, velX, velY);
firstspring.display(canvas, mouseX, mouseY, velX, velY, img);
for (int i = 0; i < springs.size (); i += 5) {
randomSeed(i);
if (i == 0) {
} else {
Spring2D spring = (Spring2D) springs.get(i);
Spring2D backspring;
if (i == 5) {
backspring = (Spring2D) springs.get(i-5);
} else {
backspring = (Spring2D) springs.get(i-round(random(1, 5)));
}
spring.update(backspring.x, backspring.y, velX, velY);
spring.display(canvas, backspring.x, backspring.y, velX, velY, img);
canvas.stroke(0);
canvas.strokeWeight(50/i);
canvas.line(spring.x, spring.y, backspring.x, backspring.y);
for (int j = 1; j < 5; j++) {
Spring2D spring2 = (Spring2D) springs.get(i+j);
spring2.update(backspring.x, backspring.y, velX, velY);
canvas.stroke(0);
canvas.line(spring2.x, spring2.y, backspring.x, backspring.y);
spring2.display(canvas, backspring.x, backspring.y, velX, velY, img);
}
}
}
}
}
class Spring2D {
float vx, vy; // The x- and y-axis velocities
float x, y, _x, _y; // The x- and y-coordinates
float gravity;
float mass;
float radius = 5;
float stiffness = 0.5;
float damping = 0.9;
float velX;
float velY;
boolean isFixed = false;
Spring2D(float xpos, float ypos, float m, float g, boolean f) {
isFixed = f;
x = xpos;
y = ypos;
_x = xpos;
_y = ypos;
mass = m;
gravity = g;
}
void update(float targetX, float targetY, float velX, float velY) {
if (!isFixed)
{
float forceX = (targetX - x) * stiffness;
float ax = forceX / mass;
vx = damping * (vx + ax);
vx += velX;
x += vx;
x += _x;
float forceY = (targetY - y) * stiffness;
forceY += gravity;
float ay = forceY / mass;
vy = damping * (vy + ay);
vy += velY;
y += vy;
y += _y;
}
}
void display(PGraphics canvas, float nx, float ny, float velX, float velY, PImage img) {
//canvas.noStroke();
canvas.stroke(0, 40);
canvas.strokeWeight(1);
for (int i = 0; i < 1; i++) {
canvas.tint(95, random(300, 360), random(100, 360), vx * 100);
//canvas.rect(x+random(-20, 20), y+random(-20, 20), radius, radius);
//canvas.image(img, x+random(-20, 20), y+random(-20, 20), radius, radius);
canvas.line(x, y, x+velX*20, y+velY*20);
}
randomSeed(round(x*2/3));
/*
for (int i = 0; i < 10; i++) {
canvas.tint(0, 0, 360, vx * 100);
//canvas.rect(x+random(-10, 10), y+random(-10, 10), radius*2, radius*2);
//canvas.line(x+random(-10, 10), y+random(-10, 10),x+vx,x+vy);
canvas.image(img, x+random(-10, 10), y+random(-10, 10), radius*2, radius*2);
}
*/
}
}