ColorfulConnectingPoints/connectingDots.pde
2022-05-17 11:55:30 +02:00

91 lines
1.9 KiB
Plaintext

final int maxDistanz = 200;
final int punkteMenge = 100;
final int punktDurchmesser = 10;
final int verbindungsDicke = 4;
final float punktGeschwindigkeit = 3;
ArrayList<Punkt> punkte = new ArrayList<Punkt>();
void setup() {
fullScreen();
fill(255);
colorMode(HSB);
strokeWeight(verbindungsDicke);
for (int i = 0; i < punkteMenge; i++) {
punkte.add(new Punkt());
}
}
void draw() {
background(0);
if (mousePressed) {
PVector m = new PVector(mouseX, mouseY);
for (Punkt p : punkte) {
verbindePunkte(m, p.position);
}
}
for (int i = 0; i < punkte.size(); i++) {
punkte.get(i).update();
for (int j = 0; j < punkte.size(); j++) {
if (j != i) {
verbindePunkte(
punkte.get(i).position,
punkte.get(j).position
);
}
}
PVector p = punkte.get(i).position;
noStroke();
circle(p.x, p.y, punktDurchmesser);
}
}
void mousePressed() {
punkte.add(new Punkt(new PVector(mouseX, mouseY)));
}
void verbindePunkte(PVector a, PVector b) {
float c = dist(a.x,a.y,b.x,b.y);
float d = 255;
if (c <= maxDistanz) {
d = map(c, 0, maxDistanz, 255, 55);
float e = map(c, 0, maxDistanz, 255, 0);
stroke(e, 255, 255, d);
line(a.x, a.y, b.x, b.y);
}
}
class Punkt {
PVector position, velocity;
Punkt() {
velocity = PVector.random2D();
velocity.mult(punktGeschwindigkeit);
position = new PVector(random(width), random(height));
}
Punkt(PVector pos) {
velocity = PVector.random2D();
velocity.mult(punktGeschwindigkeit);
position = pos.copy();
}
void update() {
position.add(velocity);
if (position.x < 0) {
velocity.x *= -1;
position.x = 0;
} else if (position.x > width) {
velocity.x *= -1;
position.x = width;
}
if (position.y < 0) {
velocity.y *= -1;
position.y = 0;
} else if (position.y > height) {
velocity.y *= -1;
position.y = height;
}
}
}