Archiv der Kategorie: projekte

Die Geburt der Sonne


Processing 2.0

Hierbei handelt es sich um eine Kombination der Techniken Additive Blending und Flocking. Es werden Partikel erzeugt, die sich dann in der Mitte verdichten.

Der Sketch wäre vielleicht in Processing 2.x auch mit anderen Techniken zu realisieren. Ich habe ihn aber nur von der Version 1.5 portiert.

/** Copyright 2014 Thomas Koberger
 */
// based on a flocking algorithm by Daniel Shiffman
// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import processing.opengl.*;
import peasy.*;
import javax.media.opengl.*;
import javax.media.opengl.GL2;
import java.util.*;

// Flock
Flock flock;
Stars stars;

GL2 gl; 
PGraphicsOpenGL pgl;

PVector l[];
float str;

void setup() {
 size(1280, 720, OPENGL);

 // Create Flock
 flock = new Flock();
 stars = new Stars(400);

 for (int x = 0; x < 350; x+=1) {
 flock.addBoid(new Boid(new PVector(random(-500, 500), random(-500, 500)), random(5.0, 2), 0.5, 10000));
 Boid b = (Boid) flock.boids.get(flock.boids.size()-1);
 b.desiredseparation=random(3, 20);
 }

 for (int x = 0; x < 15; x+=1) {
 flock.addBoid(new Boid(new PVector(random(-500, 500), random(-500, 500)), random(29.0, 2), 0.5, 10000));
 Boid b = (Boid) flock.boids.get(flock.boids.size()-1);
 b.desiredseparation=random(20, 50);
 }
}

void draw() {
 hint(DISABLE_DEPTH_TEST);
 fill(0, 15);
 rect(-width, -height, width*2, height*2);
 translate(width/2, height/2, 300);

 pgl = (PGraphicsOpenGL) g; // g may change
 gl = ((PJOGL)beginPGL()).gl.getGL2();

 gl.glEnable( GL.GL_BLEND ); 
 gl.glEnable(GL.GL_LINE_SMOOTH); 

 // This fixes the overlap issue
 gl.glDisable(GL.GL_DEPTH_TEST);

 // Define the blend mode
 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);

 stars.drawStars(gl);
 flock.run(gl);

 gl.glDisable(GL.GL_BLEND);
 endPGL();
 if (frameCount%100==1) println("Rate: "+frameRate);
 //saveFrame("line-####.jpg");
}

void keyReleased() {
 //if (key == DELETE || key == BACKSPACE) background(360);
 if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.jpg");
} 

// timestamp
String timestamp() {
 Calendar now = Calendar.getInstance();
 println("Frame saved");
 return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}
// The Boid class

class Boid {

 PVector loc;
 PVector vel;
 PVector acc;

 float maxforce; // Maximum steering force
 float maxspeed; // Maximum speed
 float maxVertspeed; // Maximum speed vertical
 float maxVertforce; // Maximum speed vertical
 int lifeTime;

 float desiredseparation;// Distance to separate from neighbours
 float neighbordistAlgn; // Distance to align with neighbours
 float neighbordist; // Distance to stick to neighbours
 float desiredAvoidDist; // Distance to avoid Avoid Hunters

 float r, g, b, alpha;

 Boid(PVector l, float ms, float mf, int lt) {
 acc = new PVector(0, 0, 0 );
 vel = new PVector(0, 0, 0);
 loc = l.get();
 r = 2.0;
 maxspeed = ms;
 maxforce = mf;
 lifeTime= lt;
 maxVertspeed=ms*3;

 desiredseparation = 30.0;
 neighbordistAlgn = 15.0;
 neighbordist = 40.0;
 desiredAvoidDist = 300;
 }

 void run(ArrayList boids, GL2 gl) {
 flock(boids, gl);
 update();
 render(gl);
 lifeTime-=1;
 }

 // We accumulate a new acceleration each time based on three rules
 void flock(ArrayList boids, GL2 gl) {
 PVector sep = separate(boids); // Separation
 PVector ali = align(boids); // Alignment
 PVector coh = cohesion(boids, gl); // Cohesion
 PVector target = target(); // Food
 // Arbitrarily weight these forces
 sep.mult(2);
 ali.mult(0.5);
 coh.mult(1.0);
 target.mult(1.0);
 // Add the force vectors to acceleration
 acc.add(sep);
 acc.add(ali);
 acc.add(coh);
 acc.add(target);
 }

 // Method to update location
 void update() {
 // Update velocity
 vel.add(acc);
 // Limit speed
 vel.limit(maxspeed);
 loc.add(vel);
 // Reset accelertion to 0 each cycle
 acc.mult(0);
 }

 void render(GL2 gl) {
 PVector modelOrientation = new PVector(0, 0, 1); 
 PVector heading=new PVector(vel.x, vel.y, vel.z);
 heading.mult(2);

 if (PVector.dist(loc, new PVector(0, 0, 0))>70) {
 int lines=5;
 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);

 for (int i=lines;i>0;i-=4) {
 gl.glLineWidth(i);
 gl.glColor4f(float(1-i/lines), 0.5-i/lines, 0.2+i/lines, 
 alpha/i);
 gl.glBegin(GL.GL_LINES); 
 gl.glVertex2f(loc.x-heading.x, loc.y-heading.y);
 gl.glVertex2f(loc.x, loc.y);
 gl.glEnd();
 }
 }
 }

 void seek(PVector target) {
 acc.add(steer(target, false));
 }

 void arrive(PVector target) {
 acc.add(steer(target, true));
 }

 // A method that calculates a steering vector towards a target
 // Takes a second argument, if true, it slows down as it approaches the target
 PVector steer(PVector target, boolean slowdown) {
 PVector steer; // The steering vector
 PVector desired = target.sub(target, loc); // A vector pointing from the location to the target
 float d = desired.mag(); // Distance from the target is the magnitude of the vector
 // If the distance is greater than 0, calc steering (otherwise return zero vector)
 if (d > 0) {
 // Normalize desired
 desired.normalize();
 // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
 if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
 else desired.mult(maxspeed);
 // Steering = Desired minus Velocity
 steer = target.sub(desired, vel);
 steer.limit(maxforce); // Limit to maximum steering force
 } 
 else {
 steer = new PVector(0, 0, 0);
 }
 return steer;
 }

 // Separation
 // Method checks for nearby boids and steers away
 PVector separate (ArrayList boids) {
 PVector steer = new PVector(0, 0, 0);
 int count = 0;
 // For every boid in the system, check if it's too close
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = PVector.dist(loc, other.loc);

 //verändert, damit der Scwarm besser zusammenhält
 // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
 if ((d > 0) && (d < desiredseparation/3)) {
 // Calculate vector pointing away from neighbor
 PVector diff = PVector.sub(loc, other.loc);
 diff.normalize();
 diff.div(d*10); // Weight by distance
 steer.add(diff);
 count++; // Keep track of how many
 }

 if ((d > 0) && (d < desiredseparation)) {
 // Calculate vector pointing away from neighbor
 PVector diff = PVector.sub(loc, other.loc);
 diff.normalize();
 diff.div(d/16); // Weight by distance
 steer.add(diff);
 count++; // Keep track of how many
 }
 }
 // Average -- divide by how many
 if (count > 0) {
 steer.div((float)count);
 }

 // As long as the vector is greater than 0
 if (steer.mag() > 0) {
 // Implement Reynolds: Steering = Desired - Velocity
 steer.normalize();
 steer.mult(maxspeed);
 steer.sub(vel);
 steer.limit(maxforce);
 }
 return steer;
 }

 // Alignment
 // For every nearby boid in the system, calculate the average velocity
 PVector align (ArrayList boids) {
 PVector steer = new PVector(0, 0, 0);
 int count = 0;
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = PVector.dist(loc, other.loc);
 if ((d > 0) && (d < neighbordistAlgn)) {
 steer.add(other.vel);
 count++;
 }
 }
 if (count > 0) {
 steer.div((float)count);
 }
 // As long as the vector is greater than 0
 if (steer.mag() > 0) {
 // Implement Reynolds: Steering = Desired - Velocity
 steer.normalize();
 steer.mult(maxspeed);
 steer.sub(vel);
 steer.limit(maxforce);
 }
 return steer;
 }

 // Cohesion
 // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
 PVector cohesion (ArrayList boids, GL2 gl) {
 PVector sum = new PVector(0, 0, 0); // Start with empty vector to accumulate all locations
 int count = 0;

 gl.glEnable( GL.GL_BLEND ); 
 gl.glColor4f(0.95, 0.3, 0.2, 0.007);
 gl.glLineWidth(6);
 gl.glBegin(GL2.GL_POLYGON); 
 gl.glVertex2f(loc.x, loc.y);
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = loc.dist(other.loc); 

 if ((d > 0) && (d < neighbordist)) {
 sum.add(other.loc); // Add location
 count++;

 gl.glVertex2f(other.loc.x, other.loc.y);
 }
 }
 gl.glDisable( GL.GL_BLEND );
 gl.glEnd();

 // für Farbverdichtungen
 alpha = map(count, 0, 50, 0, 0.9);

 if (count > 0) {
 sum.div((float)count);
 return steer(sum, true); // Steer towards the location
 }
 return sum;
 }

 // Move Towards Target
 PVector target () {
 PVector sum = new PVector(0, 0, 0); // Start with empty vector to accumulate all locations
 //sum.limit(maxforce);
 return steer(sum, true); // Steer towards the location
 }

 boolean alive() {
 if (lifeTime<0) return false;
 else return true;
 }
}

// The Flock (a list of Boid objects)

class Flock {
 ArrayList boids; // An arraylist for all the boids

 Flock() {
 boids = new ArrayList(); // Initialize the arraylist
 }

 void run(ArrayList hunters, GL gl) {
 for (int i = 0; i < boids.size(); i++) {
 Boid b = (Boid) boids.get(i);
 }
 }
 void run(GL2 gl) {

 for (int i = 0; i < boids.size(); i++) {
 Boid b = (Boid) boids.get(i); 

 b.run(boids, gl); // Passing the entire list of boids to each boid individually

 if (!b.alive()) boids.remove(i);
 }
 }


 void addBoid(Boid b) {
 boids.add(b);
 }
}

// The Boid class

class Stars {

 PVector [] stars; 

 Stars(int amt) {

 stars = new PVector [amt];
 
 for (int i=0; i < amt; i++) {
 stars[i] = new PVector (random(-width/2, width/2), random(-height/2, height/2), 0);
 }
 }

 void drawStars(GL2 gl) {
 for (int i=0; i<stars.length;i++) {
 if(frameCount%5==0){
 gl.glColor4f(1, 1, 1, random(0.02, 0.2));
 float w=random(0.3, 1.2);
 gl.glLineWidth(w*2);
 gl.glBegin(GL.GL_LINES); 
 gl.glVertex3f(stars[i].x-w/2, stars[i].y, stars[i].z);
 gl.glVertex3f(stars[i].x+w/2, stars[i].y, stars[i].z);
 gl.glVertex3f(stars[i].x, stars[i].y-w/2, stars[i].z);
 gl.glVertex3f(stars[i].x, stars[i].y+w/2, stars[i].z);
 gl.glEnd();
 }
 }
 }
}


 

Advertisements

Polarlichter


Processing 2.0

Hierbei handelt es sich um eine Kombination der Techniken Additive Blending und Flocking. Es werden an zufälligen Koordinaten Partikelschwärme erzeugt, deren Partikel dann mit Linien verbunden werden. Diese werden dann additiv übereinander geblendet und ergeben farbliche Verdichtungen, die mich an Polarlichter erinnern. Außerdem werden im Hintergrund Sterne gezeichnet.

Der Sketch wäre vielleicht in Processing 2.x auch mit anderen Techniken zu realisieren. Ich habe ihn aber nur von der Version 1.5 portiert.

/** Copyright 2014 Thomas Koberger
 */
// based on a flocking algorithm by Daniel Shiffman
// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import processing.opengl.*;
import javax.media.opengl.*;
import javax.media.opengl.GL2;
import java.util.*;

Flock flock;
Stars stars;

//Über diese Objekte kann man auf OPENGL features zugreifen
GL2 gl;
PGraphicsOpenGL pgl;

PVector l[];
float str;

void setup() {
 size(1280, 720, OPENGL);

 //Sterne und Schwarm erzeugen
 flock = new Flock();
 stars = new Stars(350);
}

void draw() {
 //Verhindert, dass Objekte am Schirm, die von anderen verdeckt werden nicht gezeichnet werden
 hint(DISABLE_DEPTH_TEST);
 fill(0, 20);
 rect(-width, -height, width*2, height*2);
 translate (width/2, height/2, -200);

 // OpenGL Object Setup
 pgl = (PGraphicsOpenGL) g; // g may change
 gl = ((PJOGL)beginPGL()).gl.getGL2();
 gl.glEnable( GL.GL_BLEND );
 gl.glEnable(GL.GL_LINE_SMOOTH); 

 // This fixes the overlap issue
 gl.glDisable(GL.GL_DEPTH_TEST);

 // Define the blend mode
 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);

 //zeichne Sterne
 stars.drawStars(gl);

 if (frameCount%6==0) {
 //Add an initial set of boids into the system
 float x=random(-width/1.3, width/1.3);
 float y=random(-height/1.3, height/1.3);
 for (int i =(int) random(5,25); i > 0; i-=1) {
 flock.addBoid(new Boid(new PVector(x, y, 0), 10.0, 0.1, 300));
 }
 } 

 flock.run(gl);
 gl.glDisable(GL.GL_BLEND);
 endPGL();
 if (frameCount%100==1) println("Rate: "+frameRate);

 //einkommentieren, wenn man die Frames speichern will
 //saveFrame(timestamp()+"_##.jpg");
}

void mousePressed() {
 //fügt neue Boids hinzu
 if ( mouseButton==LEFT) {
 for (int x = 0; x < 30; x+=1) {
 flock.addBoid(new Boid(new PVector(mouseX-width*2/3, mouseY-height*2/3), 10.0, 0.1, 500));
 }
 }
}

void keyReleased() {
 //if (key == DELETE || key == BACKSPACE) background(360);
 if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.jpg");
} 

// timestamp
String timestamp() {
 Calendar now = Calendar.getInstance();
 println("Frame saved");
 return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

// The Boid class
// Original by Danial Shiffman modified by Thomas Koberger

class Boid {

 PVector loc;
 PVector vel;
 PVector acc;
 //float r;
 float maxforce; // Maximum steering force
 float maxspeed; // Maximum speed
 float maxVertspeed; // Maximum speed vertical
 float maxVertforce; // Maximum speed vertical
 int lifeTime;

 float desiredseparation;// Distance to separate from neighbours
 float neighbordistAlgn; // Distance to align with neighbours
 float neighbordist; // Distance to stick to neighbours
 float desiredAvoidDist; // Distance to avoid Avoid Hunters

 float r, g, b, alpha; 

 Boid(PVector l, float ms, float mf, int lt) {
 acc = new PVector(0, 0, 0 );
 vel = new PVector(0, 0, 0);
 loc = l.get();
 r = 2.0;
 maxspeed = ms;
 maxforce = mf;

 //definiert eine Lebensspanne für die einzelnen Boids
 lifeTime= lt;

 //definiert die Abstände, innerhalb derer sich die einzelnen Boids gegenseitig beeinflussen
 maxVertspeed=ms*3;
 desiredseparation = 8.0;
 neighbordistAlgn = 100.0;
 neighbordist = 60.0;
 }

 void run(ArrayList boids, GL2 gl) {
 flock(boids, gl);
 update();
 //borders();
 render(gl);
 lifeTime-=1;
 }

 // We accumulate a new acceleration each time based on three rules
 void flock(ArrayList boids, GL2 gl) {
 PVector sep = separate(boids); // Separation
 PVector ali = align(boids); // Alignment
 PVector coh = cohesion(boids, gl); // Cohesion
 PVector target = target(); // Food
 // Arbitrarily weight these forces
 sep.mult(2);
 ali.mult(1.0);
 coh.mult(0.1);
 target.mult(0.6);
 // Add the force vectors to acceleration
 acc.add(sep);
 acc.add(ali);
 acc.add(coh);
 acc.add(target);
 }

 // Method to update location
 void update() {
 // Update velocity
 vel.add(acc);
 // Limit speed
 vel.limit(maxspeed);
 loc.add(vel);
 // Reset accelertion to 0 each cycle
 acc.mult(0);
 }

 //Zeichne die Boids
 void render(GL2 gl) {
 PVector modelOrientation = new PVector(0, 0, 1);
 PVector heading=new PVector(vel.x, vel.y, vel.z);
 heading.mult(2);

 int lines=1;
 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE);
 for (int i=lines;i>0;i-=4) {
 gl.glLineWidth(i);
 gl.glColor4f(1-alpha*i/lines, 0.5-alpha*i/lines, alpha*i/lines,
 alpha/8);
 gl.glBegin(GL.GL_LINES);
 gl.glVertex2f(loc.x-heading.x, loc.y-heading.y);
 gl.glVertex2f(loc.x, loc.y);
 gl.glEnd();
 }
 }

 void seek(PVector target) {
 acc.add(steer(target, false));
 }

 void arrive(PVector target) {
 acc.add(steer(target, true));
 }

 // A method that calculates a steering vector towards a target
 // Takes a second argument, if true, it slows down as it approaches the target
 PVector steer(PVector target, boolean slowdown) {
 PVector steer; // The steering vector
 PVector desired = target.sub(target, loc); // A vector pointing from the location to the target
 float d = desired.mag(); // Distance from the target is the magnitude of the vector
 // If the distance is greater than 0, calc steering (otherwise return zero vector)
 if (d > 0) {
 // Normalize desired
 desired.normalize();
 // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
 if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
 else desired.mult(maxspeed);
 // Steering = Desired minus Velocity
 steer = target.sub(desired, vel);
 steer.limit(maxforce); // Limit to maximum steering force
 }
 else {
 steer = new PVector(0, 0, 0);
 }
 return steer;
 }

 // Separation
 // Method checks for nearby boids and steers away
 PVector separate (ArrayList boids) {
 PVector steer = new PVector(0, 0, 0);
 int count = 0;
 // For every boid in the system, check if it's too close
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = PVector.dist(loc, other.loc);

 //verändert, damit der Scwarm besser zusammenhält
 // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
 if ((d > 0) && (d < desiredseparation/3)) {
 // Calculate vector pointing away from neighbor
 PVector diff = PVector.sub(loc, other.loc);
 diff.normalize();
 diff.div(d*10); // Weight by distance
 steer.add(diff);
 count++; // Keep track of how many
 }

 if ((d > 0) && (d < desiredseparation)) {
 // Calculate vector pointing away from neighbor
 PVector diff = PVector.sub(loc, other.loc);
 diff.normalize();
 diff.div(d/16); // Weight by distance
 steer.add(diff);
 count++; // Keep track of how many
 }
 }
 // Average -- divide by how many
 if (count > 0) {
 steer.div((float)count);
 }

 // As long as the vector is greater than 0
 if (steer.mag() > 0) {
 // Implement Reynolds: Steering = Desired - Velocity
 steer.normalize();
 steer.mult(maxspeed);
 steer.sub(vel);
 steer.limit(maxforce);
 }
 return steer;
 }

 // Alignment
 // For every nearby boid in the system, calculate the average velocity
 PVector align (ArrayList boids) {
 PVector steer = new PVector(0, 0, 0);
 int count = 0;
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = PVector.dist(loc, other.loc);
 if ((d > 0) && (d < neighbordistAlgn)) {
 steer.add(other.vel);
 count++;
 }
 }
 if (count > 0) {
 steer.div((float)count);
 }
 //definiert den Rot Wert
 r=map(steer.mag(), 0, 10, 0, 0.9);
 // As long as the vector is greater than 0
 if (steer.mag() > 0) {
 // Implement Reynolds: Steering = Desired - Velocity
 steer.normalize();
 steer.mult(maxspeed);
 steer.sub(vel);
 steer.limit(maxforce);
 }
 return steer;
 }

 // Cohesion
 // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
 PVector cohesion (ArrayList boids, GL2 gl) {
 PVector sum = new PVector(0, 0, 0); // Start with empty vector to accumulate all locations
 int count = 0;

 // Define the blend mode
 gl.glEnable( GL.GL_BLEND );
 gl.glColor4f(r, g, b, 0.004);
 gl.glLineWidth(8);
 gl.glBegin(GL2.GL_POLYGON);
 gl.glVertex2f(loc.x, loc.y);
 for (int i = 0 ; i < boids.size(); i++) {
 Boid other = (Boid) boids.get(i);
 float d = loc.dist(other.loc);

 //definiert den Blau und Grün Wert
 b=map(d, 0, neighbordist*12, 0, 0.9);
 g=map(vel.mag(), 0, 10, 0, 0.9);

 if ((d > 0) && (d < neighbordist)) {
 sum.add(other.loc); // Add location
 count++;

 gl.glVertex2f(other.loc.x, other.loc.y);
 }
 }
 gl.glEnd();

 // für Farbverdichtungen
 alpha = map(count, 0, 50, 0, 1);

 if (count > 0) {
 sum.div((float)count);
 return steer(sum, true); // Steer towards the location
 }
 return sum;
 }

 // Move Towards Target
 PVector target () {
 PVector sum = new PVector(0, 0, 0); // Start with empty vector to accumulate all locations
 return steer(sum, true); // Steer towards the location
 }

 boolean alive() {
 if (lifeTime<0) return false;
 else return true;
 }
}

// The Flock (a list of Boid objects)

class Flock {
 ArrayList boids; // An arraylist for all the boids

 Flock() {
 boids = new ArrayList(); // Initialize the arraylist
 }

 void run(ArrayList hunters) {
 for (int i = 0; i < boids.size(); i++) {
 Boid b = (Boid) boids.get(i);
 }
 }
 void run(GL2 gl) {
 for (int i = 0; i < boids.size(); i++) {
 Boid b = (Boid) boids.get(i);
 b.run(boids, gl); // Passing the entire list of boids to each boid individually

 //Elimiert Boids, wenn sie den sichbaren bereich verlassen
 if (!b.alive() || b.loc.x>1200 || b.loc.x<-1200 || b.loc.y>1200 || b.loc.y<-1200) {
 boids.remove(i);
 }
 }
 }

 void addBoid(Boid b) {
 boids.add(b);
 }
}

// The Boid class

class Stars {

 PVector [] stars; 

 Stars(int amt) {

 stars = new PVector [amt];

 for (int i=0; i < amt; i++) {
 stars[i] = new PVector (random(-width, width), random(-height, height), 0);
 }
 }

 void drawStars(GL2 gl) {
 for (int i=0; i<stars.length;i++) {
 if(frameCount%5==0){
 gl.glColor4f(1, 1, 1, random(0.02, 0.2));
 float w=random(0.3, 1.2);
 gl.glLineWidth(w*2);
 gl.glBegin(GL.GL_LINES);
 gl.glVertex3f(stars[i].x-w/2, stars[i].y, stars[i].z);
 gl.glVertex3f(stars[i].x+w/2, stars[i].y, stars[i].z);
 gl.glVertex3f(stars[i].x, stars[i].y-w/2, stars[i].z);
 gl.glVertex3f(stars[i].x, stars[i].y+w/2, stars[i].z);
 gl.glEnd();
 }
 }
 }
}

 

Voronoi und die französiche Revolution


Processing 2.0

Hier nun 2 Anwendungsbeispiele für Voronoi Diagramme zum Thema „französische Revolution“.
Eine Erklärung, wie so ein Diagramm funktioniert findest du hier. Im ersten Beispiel wird eine Library verwendet, mit der man Schriftzüge in einzelne Punkte auflösen kann. Die Geomerative Library. Infos dazu gibts hier.

Beispiel 1: Hier werden die Schriftzüge der Werte Liberté  Égalité  Fraternité (Freiheit, Gleichheit, Brüderlichkeit) zerlegt, in Voronoi Bereiche aufgespalten und dann entsprechend eingefärbt. Blau für die Freiheit, Weiß für Gleichheit und Rot für Brüderlichkeit.

140109_082800_01

/** Copyright 2013 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import java.util.Calendar;
import megamu.mesh.*;
import geomerative.*;

RShape shp, shp1, shp2;
RPoint[] pnts, pnts1;
PImage qr;

String BspText1 = "Liberté  Égalité  Fraternité";

ArrayList<Integer> voroPoints;
Voronoi myVoronoi;
float[][] points;

void setup() {
size(4000, 1000);
voroPoints = new ArrayList<Integer> ();

translate(width/2, height*2/12);

RG.init(this);
// 3 Shape - Objekte werden erzeugt.
// Die Schrift mit dem Namen "Ubuntu-R.ttf" muss im data Ordner platziert werden
shp1 = RG.getText(BspText1, "Ubuntu-R.ttf", width*8/120, CENTER);

// Punkte an der Schriftkontur finden
//Abstand der Punkte
RCommand.setSegmentLength (3);
//Modus
RCommand.setSegmentator(RCommand.UNIFORMLENGTH);
//RCommand.setSegmentator(RCommand.ADAPTATIVE);

// Die Shapes werdem gezeichnet und positioneiert
shp1.translate(0, height*7/12);
fill(180, 160);

// Finden der Konturpunkte
pnts = shp1.getPoints();

// Variation der einzelnen Punkte
for (int i=0;i<pnts.length; i+=1) {
stroke(255, 0, 0);
point(pnts[i].x, pnts[i].y);

int var=(int) random(-2, 2);
int x = (int) pnts[i].x+var;
var=(int) random(-2, 2);
int y = (int) pnts[i].y+var;

//Damit nicht 2 Punkte die gleichen Koordinaten haben
boolean coordAvailable=true;
for (int j=0; j<voroPoints.size(); j+=2) {
if (((int)voroPoints.get(j)==x && (int)voroPoints.get(j+1)==y)
|| x==0 || y==0) {
coordAvailable=false;
}
}

if (coordAvailable) {
voroPoints.add(x);
voroPoints.add(y);
}
else println("sameCoord");
}
println("NumPoints: "+voroPoints.size());
createVoronoi () ;
qr =loadImage("qrcode.png");
}

void draw() {
background(255);
translate(width/2, 0);

//get and draw VoroRegions
MPolygon[] myRegions = myVoronoi.getRegions();

for (int i=0; i<myRegions.length; i++) {
// an array of points
float[][] regionCoordinates = myRegions[i].getCoords();

int col= (int)random(0, 3);
if (col==0) fill(0, 0, 255);
else if (col==1) fill(255, 40);
else fill(255, 0, 0);
stroke(80, 50);
strokeWeight(1);
myRegions[i].draw(this); // draw this shape
}

//draw Points
strokeWeight(2);
stroke(80, 150);
for (int i=0; i<voroPoints.size(); i+=2) {
point((int)voroPoints.get(i), (int)voroPoints.get(i+1));
}
image(qr, width/2-height/5, height-height/5, height/10, height/10);
saveFrame(timestamp()+"_##.png");
}

void createVoronoi () {
points = new float[voroPoints.size()/2][2];
for (int i=0; i<voroPoints.size(); i+=2) {

//Hierbei muss die Art des Objekts in der ArrayList festgelegt werden.
points[i/2][0] =(int) voroPoints.get(i);
points[i/2][1] =(int) voroPoints.get(i+1);
}
myVoronoi = new Voronoi( points );
}

void keyReleased() {
if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
}

//timestamp
String timestamp() {
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

Beispiel 2: Visualisierung der Werte Liberté  Égalité  Fraternité (Freiheit, Gleichheit, Brüderlichkeit) in Form von  Voronoi Diagrammen.Fertig

/** Copyright 2013 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
*
* MOUSE
*
* KEYS
* Toggle Mode
* 0                   : Freiheit
* 1                   : Gleichheit
* 1                   : Brüderlichkeit
* s                   : save png
*/

import java.util.Calendar;
import megamu.mesh.*;

ArrayList<Integer> voroPoints;
Voronoi myVoronoi;
float[][] points;

//Freiheit = 0
//Gleichheit = 1
//Brüderlichkeit = 2
int mode=0;

void setup() {
size(800, 800);
voroPoints = new ArrayList<Integer> ();
createFreiheit();
noLoop();
}

void draw() {
createVoronoi();
if (mode==0) {
fill(0, 0, 190, 255);
stroke(200);
}
else if (mode==1) {
fill(255);
stroke(80);
}
else {
fill(220, 0, 0);
stroke(200);
}
if (voroPoints.size()>1) {

//getRegions
strokeWeight(1);
MPolygon[] myRegions = myVoronoi.getRegions();

for (int i=0; i<myRegions.length; i++)
{
// an array of points
float[][] regionCoordinates = myRegions[i].getCoords();
myRegions[i].draw(this); // draw this shape
}
}

// draw Points
strokeWeight(6);
for (int i=0; i<voroPoints.size(); i+=2) {
point((int)voroPoints.get(i), (int)voroPoints.get(i+1));
}
}

void createFreiheit() {
voroPoints = new ArrayList<Integer> ();

for (int i=160; i<width-80;i+=80) {
for (int j=160; j<height-80;j+=80) {

if (i==400 && j ==400) {
voroPoints.add(i+(int)random(-40, 40));
voroPoints.add(j+(int)random(-40, 40));
}
else {
voroPoints.add(i);
voroPoints.add(j);
}
}
}
}

void createGleichheit() {
voroPoints = new ArrayList<Integer> ();

for (int i=160; i<width-80;i+=80) {
for (int j=160; j<height-80;j+=80) {
voroPoints.add(i);
voroPoints.add(j);
}
}
}

void createBruederlichkeit() {
voroPoints = new ArrayList<Integer> ();

for (int i=160; i<width-80;i+=80) {
for (int j=160; j<height-80;j+=80) {
if (i==400 && j ==400) {
voroPoints.add(i);
voroPoints.add(j+20);
} else if (i==400 && j ==480) {
voroPoints.add(i);
voroPoints.add(j-20);
}else {
voroPoints.add(i);
voroPoints.add(j);
}
}
}
}

void createVoronoi () {
points = new float[voroPoints.size()/2][2];
for (int i=0; i<voroPoints.size(); i+=2) {

points[i/2][0] =(int) voroPoints.get(i);
points[i/2][1] =(int) voroPoints.get(i+1);
}
myVoronoi = new Voronoi( points );
}

void keyReleased() {
if (key == DELETE || key == BACKSPACE) background(360);
if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
if (key == '0') {
mode=0;
createFreiheit();
}
if (key == '1') {
mode=1;
createGleichheit();
}
if (key == '2') {
mode=2;
createBruederlichkeit();
}
loop();
}

//timestamp
String timestamp() {
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

Wahlergebnis


Processing 2.0

Unser Ziel war es die Wahlergebnisse 2013 möglichst einfach, informativ und ohne prozentuelle Zahlenwerte, visuell darzustellen.

Am Anfang hatten wir mehrere Lösungsvorschläge gesammelt, aber haben uns schließlich für eine Darstellung mithilfe von Kreisen entschlossen. Ein Kreis in der Mitte wird von mehreren Kreisen umschlossen, wobei in vier Ebenen (Österreich, Bundesländer, Bezirke, Gemeinden) die gesamte Verteilung der Stimmen, auf die bei der Wahl angetretenen Parteien abgebildet wird.

Um einen guten visuellen Effekt zu erzeugen haben wir die Radien der Kreise immer so vergrößert dass diese den gleichen Flächeninhalt wie der Kreis in der Mitte haben. Diesen Effekt erhält man indem man den Radius des ersten Kreises (in der Mitte) mit dem Faktor multipliziert, so erhält man den Radius des zweiten Kreises. Den Radius des dritten Kreises erhält man dann indem man den Radius des ersten Kreises mit multipliziert usw…

Die Kreise haben wir dann dem Prozentsatz nach, den die Parteien bei den Wahlen erzielt haben in Sektoren unterteilt und eingefärbt. Ausgegangen sind wir hier von den gültigen Stimmen.

wahl13klein wahlklein

Nationalratswahl 2008.pdf

Nationalratswahl 2013.pdf

// WahlVisualisierung Nationalratswahl 2008
//
// Copyright 2013 Thomas Koberger
//
// http://www.lernprocessing.wordpress.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//Damit im Pdf Mode gerendert werden kann
//Nötig, um die geforderte Auflösung zu erreichen
import processing.pdf.*;

//Array zum Einlesen der csv Datei
String [][] csv;
PImage fo;
//Array enthält die Parteifarben
color[] parteiFarbe = {
color(255, 0, 0), //SPÖ
color(0, 0, 0), //ÖVP
color(0, 0, 255), //FPÖ
color(255, 180, 0), //BZÖ
color(0, 255, 0), //Grüne
color(255, 255, 0), //Frank
color(255, 0, 100), //Neos
color(190, 0, 0), //KPÖ
color(255, 0, 255), //Pirat
color(0, 100, 255), //CPÖ
color(0, 100, 255), //Wandl
color(0, 100, 255), //M
color(0, 100, 255), //EUAUS
color(0, 100, 255), //SLP
color(0, 100, 255)
};

//Array enthält die Parteinamen
String [] parteien= {
"SPÖ",
"ÖVP",
"FPÖ",
"BZÖ",
"Grüne",
"Frank",
"Neos",
"KPÖ",
"Pirat",
"CPÖ",
"Wandl",
"M",
"EUAUS",
"SLP"
};

//Variable bestimmt später die Größe der Grafik
float weight;

float angle;

void setup() {
size(8000, 8000, PDF, "Wahl13.pdf");

//Definitionen
strokeWeight(1);
stroke(100);
ellipseMode(CENTER);
textFont(createFont("Tahoma", 96));
textAlign(CENTER, CENTER);
imageMode(CENTER);

//folgender Code ist für den Import der Daten aus
//aus einer .svg zuständig und stammt im Original von:
//for importing csv files into a 2d array
String lines[] = loadStrings("NRW13.csv");
int csvWidth=0;
//Berechnet die Anzahl der Spalten in der csv Datei
for (int i=0; i < lines.length; i++) {
//println(i+". "+lines[i]);
String [] chars=split(lines[i], ';');
if (chars.length>csvWidth) {
csvWidth=chars.length;
}
}
//Erstellt ein Array basierend auf die Anzahl der Zeilen und Spalten der csv Datei
csv = new String [lines.length][csvWidth];

//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], ';');
for (int j=0; j < temp.length; j++) {
csv[i][j]=temp[j];
}
}
//Berechnet einen Wert, der die Größe der Grafik bestimmt,
//und aus der Anzahl der Stimmberechtigten ermittelt wird
weight = sqrt(parseFloat(csv[0][6])/PI)*2;
angle=0;
}

void draw () {
background(255);
pushMatrix();
translate(width/2, height/2);
drawBeschriftung();
drawLaender();
drawAustria();
popMatrix();
textSize(50);
textAlign(CENTER, CENTER);
//text("Quelle: http://www.bmi.gv.at/cms/BMI_wahlen/nationalrat/2008/files/Ergebnis_end.zip", width/2, height-height/50);
// Exit the program
fo =loadImage("qrcode.png");
image(fo,width/1.08,height/1.08,640,640);
println("Finished.");
exit();
}

void drawLaender() {
pushMatrix();
float weightBl=weight*sqrt(3);

for (int i=0; i < csv.length; i++) {

//draw Bundesländer
String Gkz=csv[i][0];
Gkz=Gkz.substring(1, 6);
//println(parseInt(Gkz)%10000);
if (parseInt(Gkz)%10000==0 && parseInt(Gkz)/10000!=0) {
float percentage= parseFloat(csv[i][6])/parseFloat(csv[0][6])*100;
float angleBl=radians(percentage*3.6);
int gkzBl = parseInt(Gkz)/10000;
int partyBl=getStrongestParty(i);
println("Bl: "+gkzBl);

//draw Bezirke
float weightBz=weight*sqrt(5);
pushMatrix();
for (int k=0; k < csv.length; k++) {
Gkz=csv[k][0];
Gkz=Gkz.substring(1, 6);
//println(Gkz+" 11: "+parseInt(Gkz));

if (parseInt(Gkz.substring(0, 1))==gkzBl && (parseInt(Gkz)-gkzBl*10000)%100 ==0 && (parseInt(Gkz)-gkzBl*10000) >0 ) {
float percentageBz= parseFloat(csv[k][6])/parseFloat(csv[0][6])*100;
float angleBz=radians(percentageBz*3.6);
int gkzBz = parseInt(Gkz.substring(1,3));
println(gkzBz+" Bz: " + csv[k][1]+ " gkz: "+csv[k][0]);
int partyBz=getStrongestParty(k);

//draw Gemeinden
float weightGmd=weight*sqrt(7);
pushMatrix();
for (int l=0; l < csv.length; l++) {
Gkz=csv[l][0];
Gkz=Gkz.substring(1, 6);

if (parseInt(Gkz.substring(0,1))==gkzBl && parseInt(Gkz.substring(1,3))==gkzBz && parseInt(Gkz.substring(3,5))!=0) {
println(gkzBl+" gkzBz: "+gkzBz+" Gmd: "+Gkz.substring(3,5)+" "+csv[l][1]+ " gkz: "+csv[l][0]);
float percentageGmd= parseFloat(csv[l][6])/parseFloat(csv[0][6])*100;
//println(parseFloat(csv[k][2])+" percBZ"+percentageBz+" perc"+percentage);
float angleGmd=radians(percentageGmd*3.6);
int gkzGmd = parseInt(csv[l][0]);
int partyGmd=getStrongestParty(l);
//println(gkzGmd);

//Zeichnet die Stimmverteilung der Gemeinden
drawParty( angleGmd, l, 7);
//Zeichnet die Beschriftung der Gemeinden
angle+=angleGmd/2;
rotate(angleGmd/2);
fill(100);
pushMatrix();
translate(weightGmd/2+10, 0);
if (degrees(angle)>90 && degrees(angle)<270) {
rotate(PI);
textAlign(RIGHT, CENTER);
}
else {
textAlign(LEFT, CENTER);
}
textSize(map(min(parseFloat(csv[l][6]),10000),100,15000,5,16));
fill(parteiFarbe[partyGmd]);
text(csv[l][1], 0, 0);
popMatrix();
rotate(angleGmd/2);
angle+=angleGmd/2;
//println(csv[k][0]+" : "+angle);
}
}
popMatrix();
angle-=angleBz;
//Zeichnet die Stimmverteilung der Bezirke
drawParty( angleBz, k, 5);
//Zeichnet die Beschriftung der Bezirke
angle+=angleBz/2;
rotate(angleBz/2);
fill(100);
pushMatrix();
translate(weightBz/2+10, 0);
if (degrees(angle)>90 && degrees(angle)<270) {
rotate(PI);
textAlign(RIGHT, CENTER);
}
else {
textAlign(LEFT, CENTER);
}

textSize(weight/160);
fill(parteiFarbe[partyBz]);
text(csv[k][1], 0, 0);
popMatrix();
angle+=angleBz/2;
rotate(angleBz/2);
}
}
popMatrix();
angle-=angleBl;
//Zeichnet die Stimmverteilung der Bundesländer
println(gkzBl+" angle: "+angleBl);
drawParty( angleBl, i, 3);
//Zeichnet die Beschriftung der Bezirke
angle+=angleBl/2;
rotate(angleBl/2);
fill(100);
pushMatrix();
translate(weightBl/2+weight/50, 0);
rotate(PI/2);
textSize(weight/45);
textAlign(CENTER, CENTER);
fill(parteiFarbe[partyBl]);
text(csv[i][1], 0, 0);
popMatrix();
rotate(angleBl/2);
angle+=angleBl/2;
}
}

popMatrix();
fill(255);
ellipse(0, 0, weight*sqrt(2), weight*sqrt(2));
}

void drawAustria() {
//Zeichnet den Inneren Kreis mit dem Ergebnis für ganz Österreich
pushMatrix();
angle=0;
//Liest für jede Partei die Stimmen in % aus
for (int i=0;i<26; i+=2) {
//Stimmen für die jeweilige Partei in %
float percentage=parseFloat(csv[0][i+8]);
//Winkel des Kreissektors jeder Partei
float angleAt=radians(percentage*3.6);
//Zeichnet den Kreissektor in der Farbe der Partei
stroke(200);
fill(255);
arc(0, 0, weight*sqrt(2), weight*sqrt(2), 0, angleAt, PIE);
fill(parteiFarbe[i/2]);
//Zeichnet den Kreissektor für die Beschriftung
noStroke();
arc(0, 0, weight, weight, 0, angleAt, PIE);
//Beschriftung der Kreissektoren
rotate(angleAt/2);
angle+=angleAt/2;
fill(100);
pushMatrix();
translate(weight/2+weight/100, 0);
if (degrees(angle)>90 && degrees(angle)<270) {
rotate(PI);
textAlign(RIGHT, CENTER);
}
else {
textAlign(LEFT, CENTER);
}
textSize(weight/40);
//if (percentage>0.5) text(parteien[i/2]+" "+csv[0][i+8]+"%", 0, 0);
if (percentage>0.5) text(parteien[i/2], 0, 0);
popMatrix();
angle+=angleAt/2;
rotate(angleAt/2);
}
popMatrix();
}

//Zeichnet die einzelnen Kreissektoren der Parteien für jeden Wahlkreis
void drawParty(float winkel, int i, int ebene) {
stroke(200);
fill(255);
arc(0, 0, weight*sqrt(ebene+1), weight*sqrt(ebene+1), 0, winkel, PIE);
noStroke();
float offset=0;
for (int j=0;j<26; j+=2) {
float breite=parseFloat(csv[i][j+8]);
fill(parteiFarbe[j/2]);
arc(0, 0, weight*sqrt(ebene-offset), weight*sqrt(ebene-offset), 0, winkel, PIE);
offset+=parseFloat(csv[i][j+8])/100;
}
}

int getStrongestParty(int index) {
int party=0;
float strongestParty=0;
for (int m=0;m<26; m+=2) {
//println(csv[i][1]+" :"+parseFloat(csv[i][m+8])+" strong:"+strongestParty+" bl: "+partyBl);
if (strongestParty<parseFloat(csv[index][m+8])) {
strongestParty=parseFloat(csv[index][m+8]);
party=m/2;
}
}
return(party);
//println(csv[i][1]+" :"+partyBl);
}

void drawBeschriftung() {
pushMatrix();
String ueberschrift= "Nationalratswahl 2013";
println(weight*sqrt(9)-weight*sqrt(8));
//textFont(createFont("Tahoma", (int)(weight*sqrt(9)-weight*sqrt(8))));

textSize((weight*sqrt(9)-weight*sqrt(8))/2);
textAlign(CENTER);
//translate(width/2,height/2);

fill(255, 0, 0);
ellipse(0, 0, weight*sqrt(9), weight*sqrt(9));
fill(255);
ellipse(0, 0, weight*sqrt(9)-(weight*sqrt(9)-weight*sqrt(8))/3, weight*sqrt(9)-(weight*sqrt(9)-weight*sqrt(8))/3);
fill(255, 0, 0);
ellipse(0, 0, weight*sqrt(9)-(weight*sqrt(9)-weight*sqrt(8))*2/3, weight*sqrt(9)-(weight*sqrt(9)-weight*sqrt(8))*2/3);
rotate(PI*7/6);
fill(0);
arc(0, 0, weight*sqrt(9), weight*sqrt(9), 0, PI*2/3, PIE);
fill(250);
ellipse(0, 0, weight*sqrt(8), weight*sqrt(8));

for (int i=0;i<ueberschrift.length();i++) {
rotate(PI*2/3/(ueberschrift.length())/2);
pushMatrix();
translate(weight*sqrt(8)/2+weight/100, 0);
rotate(PI/2);
fill(255);
text(ueberschrift.charAt(i), 0, 0);
popMatrix();
rotate(PI*2/3/(ueberschrift.length())/2);
}
popMatrix();
}

Rotations III


Eine weitere Variation der Rotations.

/** Copyright 2012 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

int rand=(int) random(0, 50);

void setup() {
size(1280, 1280);
background(0);
stroke(50, 100, 200, 5);
frameRate(50);
colorMode(HSB);
}

void draw() {
if (frameCount<=500) {
translate(width/2, height/2);
strokeWeight(frameCount/2);
float winkel=radians(frameCount*80);
rotate(winkel*rand);
stroke(map(radians(winkel)%(2*PI), 0, 2*PI, 0, 255), 255, 255, 5);
translate(cos(winkel)*100, sin(winkel)*100);
line(100, 0, 300, 0);
if (frameCount%50 == 0) saveFrame("rotations-"+rand+"-#####.png");
}
}


Rotations II


Processing 2.0

Beispiel: Das Auge

Das Programm von Rotations I wird nun um eine zufällig generierte Farbpalette erweitert. Diese Zufallsfarben weisen eine Gauss’sche Normalverteilung auf. Siehe Zufall

Außerdem ändert sich während der Laufzeit die Strichlänge, Farbe und Position.

/** Copyright 2012 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Random generator;
int str=(int) random(7, 7);

int col[] = new int [str];
int baseCol= (int) random(0, 255);

void setup() {
size(2700, 2700);
background(0);
frameRate(120);
colorMode(HSB);
generator = new Random();
for (int i =0; i<col.length; i++) {
float gauss = (float) generator.nextGaussian();
col[i] = round( 40 * gauss + baseCol);
}
println(col);
}

void draw() {
if (frameCount<30000) {
translate(width/2, height/2);
//strokeWeight(frameCount/128);
strokeWeight(random(1, width/40));
rotate(radians(frameCount/2*(360/str)));
// println(str);
stroke(col[frameCount%str]-frameCount/200, 255, 255, 1);
line(width/4.5-frameCount/50, 0, random(width/8, width*4/10)-frameCount/50, 0);
if (frameCount%3000==0) saveFrame("rotate"+str+baseCol+"#####.png");
}
}

Rotations I


Processing 2.0

Ausgehend von einem Beispiel aus dem Artikel Transformationen hat mich interessiert, was mit einfachen Rotationen alles möglich ist.

Beispiel: Erst mal einfärbig und mit nur einer Rotation

/** Copyright 2012 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

void setup() {
size(900,900);
background(0);
stroke(50,100,200,5);
frameRate(25);
}

void draw() {
if(frameCount<500) {
translate(width/2,height/2);
strokeWeight(frameCount/4);
rotate(radians(frameCount*20));
line(50,0,350,0);
}
if(frameCount==500) saveFrame("rotate.png");
}

Beispiel: Etwas mehr Farbe

/** Copyright 2012 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

void setup() {
size(900,900);
background(0);
stroke(50,100,200,5);
frameRate(25);
colorMode(HSB);
}

void draw() {
if(frameCount<500) {
translate(width/2,height/2);
strokeWeight(frameCount/4);
println(radians(frameCount*20));
rotate(radians(frameCount*20));
stroke(map(radians(frameCount*20)%(2*PI),0,2*PI,0,255),255,255,5);
line(50,0,350,0);
}
if(frameCount==500) saveFrame("rotate.png");
}

Beispiel: Variation mit etwas schmäleren Strichen und mehr Wiederholungen

/** Copyright 2012 Thomas Koberger
*/

// https://lernprocessing.wordpress.com/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

void setup() {
size(900, 900);
background(255);
stroke(50, 100, 200, 5);
frameRate(120);
colorMode(HSB);
}

void draw() {
if (frameCount<50000) {
translate(width/2, height/2);
strokeWeight(frameCount/64);
rotate(radians(frameCount*20));
stroke(map(radians(frameCount*20)%(2*PI), 0, 2*PI, 0, 255), 255, 255, 5);
line(200, 0, 220, 0);
if (frameCount%500 == 0) saveFrame("background-######.png");
}
}