-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchPackingSpheresAroundCenter.java
executable file
·74 lines (59 loc) · 2.15 KB
/
SketchPackingSpheresAroundCenter.java
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
package de.hfkbremen.algorithmiccliches.examples;
import processing.core.PApplet;
import processing.core.PVector;
import teilchen.util.Packing;
import java.util.ArrayList;
public class SketchPackingSpheresAroundCenter extends PApplet {
private final PVector mCenter = new PVector();
private ArrayList<Packing.PackingEntity> mEntities;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
mEntities = createRandomEntities(50);
mCenter.set(width / 2.0f, height / 2.0f, 0);
}
public void draw() {
background(255);
stroke(0);
noFill();
for (Packing.PackingEntity mEntity : mEntities) {
ellipse(mEntity.position().x, mEntity.position().y, mEntity.radius() * 2, mEntity.radius() * 2);
}
final int ITERATIONS = 50;
for (int i = 1; i < ITERATIONS; i++) {
attachToMouse();
Packing.pack(mEntities, mCenter, 1.0f / frameRate * 0.0251f);
}
}
public void mousePressed() {
mCenter.set(mouseX, mouseY, 0);
}
public void keyPressed() {
mEntities = createRandomEntities(50);
}
private ArrayList<Packing.PackingEntity> createRandomEntities(int pNumberOfShapes) {
ArrayList<Packing.PackingEntity> mRandomEntities = new ArrayList<>();
for (int i = 0; i < pNumberOfShapes; i++) {
Packing.PackingEntity c = new Packing.PackingEntity();
c.position().set(random(width), random(height), 0);
c.radius(random(pNumberOfShapes) + 10);
mRandomEntities.add(c);
}
return mRandomEntities;
}
private boolean contains(Packing.PackingEntity c, PVector pPosition) {
float d = PVector.dist(c.position(), pPosition);
return d <= c.radius();
}
private void attachToMouse() {
for (Packing.PackingEntity c : mEntities) {
if (contains(c, new PVector(mouseX, mouseY))) {
c.position().set(mouseX, mouseY, 0);
}
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchPackingSpheresAroundCenter.class.getName()});
}
}