-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectedCircles.pde
More file actions
123 lines (110 loc) · 2.76 KB
/
Copy pathConnectedCircles.pde
File metadata and controls
123 lines (110 loc) · 2.76 KB
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
int gridsize = 10;
int count = 0;
Orb[] orbs;
ArrayList<Vanishing_Line> lines = new ArrayList<Vanishing_Line>();
void setup() {
orbs = new Orb[(gridsize - 1) * (gridsize - 1)];
size(512, 512);
background(255);
count = 0;
for (int i = 1; i < gridsize; i++) {
for (int j = 1; j < gridsize; j++) {
float x = width*i/gridsize;
float y = height*j/gridsize;
Orb orb = new Orb(x,y);
orbs[count] = orb;
orbs[count].display();
count += 1;
}
}
}
void draw() {
background(255);
for (int i = 0; i < count; i++) {
orbs[i].move();
}
collision();
for (int i = lines.size() - 1; i >= 0; i--){
Vanishing_Line line = lines.get(i);
if (!line.finished) {
line.display();
} else {
lines.remove(i);
}
}
}
void collision(){
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++){
float loc_dist = distance(orbs[i].xpos, orbs[i].ypos, orbs[j].xpos, orbs[j].ypos);
float collision_dist = orbs[i].orbsize/2 + orbs[j].orbsize/2;
if (loc_dist <= collision_dist){
Vanishing_Line line = new Vanishing_Line(orbs[i].xpos, orbs[i].ypos, orbs[j].xpos, orbs[j].ypos);
lines.add(line);
}
}
}
}
class Orb {
float orbsize;
float x_speed;
float y_speed;
float xpos;
float ypos;
float index;
public Orb(float x, float y){
orbsize = random(1,3) * height/prop(gridsize * 2);
x_speed = prop(random(-1, 1));
y_speed = prop(random(-1, 1));
xpos = x;
ypos = y;
}
void display(){
noStroke();
fill(0, 0, 0, 0);
ellipse(xpos, ypos, orbsize, orbsize);
}
void move(){
if (xpos + x_speed + orbsize/2 > width || xpos + x_speed - orbsize/2 < 0){
x_speed *= -1;
}
if (ypos + y_speed + orbsize/2 > height || ypos + y_speed - orbsize/2 < 0){
y_speed *= -1;
}
xpos = xpos + x_speed;
ypos = ypos + y_speed;
}
}
class Vanishing_Line {
float x0;
float y0;
float x1;
float y1;
float frames_alive;
float decay;
boolean finished = false;
public Vanishing_Line(float x0pos, float y0pos, float x1pos, float y1pos) {
x0 = x0pos;
y0 = y0pos;
x1 = x1pos;
y1 = y1pos;
frames_alive = 90;
decay = frames_alive;
}
void display(){
if (decay >= 0){
float alpha = decay / frames_alive;
stroke(0, 0, 0, alpha * 255);
line(x0, y0, x1, y1);
decay -= 1;
} else {
finished = true;
}
}
}
float prop(float value){
return value * width / 512;
}
float distance(float x1, float y1, float x2, float y2){
return (float)sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}