xxxxxxxxxx
PImage bg;
PImage transp;
import processing.pdf.*;
import java.awt.event.KeyEvent;
int minDistToDraw = 500;
//color BGColor = color(160, 189, 255);
color BGColor = color(6, 0, 32);
//color BGColor = color(255);
//color MolecoleColor = color(255,121,31);
color StarColor = color(255, 254, 245);
//color MolecoleColor = color(0);
int numStarsBG = 500;
int numStars = 10;
Constellation SkyBG = new Constellation();
Constellation SkyMove = new Constellation(true);
void setup() {
fullScreen();
//size(1920, 1080);
bg = loadImage("typo.jpg");
transp = loadImage("transp.png");
SkyBG.addStars(numStarsBG);
SkyMove.addStars(numStars, random(width), random(width), 5, 255, 255, 1);
smooth();
//noCursor();
/*
background( 255 );
smooth();
strokeWeight( strk );
fill( 150, 50 );
*/
}
void draw() {
background(16, 16, 16);
//image(bg,0,0);
if (!pause) {
fill(BGColor, 100);
//stroke(1);
//rect( 0, 0, width, height );
SkyBG.drawMe();
SkyMove.updateMe();
SkyMove.drawMe();
}
//image(transp,0,0);
}
void mouseReleased() {
SkyMove.addStars(numStars, random(width), random(width), 5, 255, 255, 1);
//SkyBG.addStars( numStarsBG );
}
class Constellation {
Star[] celestialPlane = {};
boolean drawLine;
int minDistToDraw;
color linecol = color(0, 146, 70); //slush green //LINE COLOR
Constellation() {
//addStars();
}
Constellation(boolean _drawLine) {
drawLine = _drawLine;
minDistToDraw = 300;
//addStars();
}
void drawMe() {
for (int i = 0; i < celestialPlane.length; i++) {
Star thisStar = celestialPlane[i];
thisStar.drawMe();
if (drawLine) {
for (int j = 0; j < 2; j++) {
int idx = thisStar.idxNeigh[j];
float dist = thisStar.distNeigh[j];
int strokeAlpha = (int) map(min(dist, minDistToDraw), 0, minDistToDraw, 255, 10);
//int strokeAlpha=255;
stroke(linecol, strokeAlpha);
line(thisStar.x, thisStar.y, celestialPlane[idx].x, celestialPlane[idx].y);
}
}
} //<>//
}
void updateMe() {
for (int i = 0; i < celestialPlane.length; i++) {
Star thisStar = celestialPlane[i];
thisStar.updateMe();
}
updateDist();
}
void updateDist() {
for (int i = 0; i < celestialPlane.length; i++) {
Star thisStar = celestialPlane[i];
thisStar.updateDist(celestialPlane);
}
}
void addStars() {
for (int i = 0; i < 30; i++) {
Star thisStar = new Star(200.0);
celestialPlane = (Star[]) append(celestialPlane, thisStar);
}
}
void addStars(int starNum) {
for (int i = 0; i < starNum; i++) {
Star thisStar = new Star(200.0);
celestialPlane = (Star[]) append(celestialPlane, thisStar);
}
}
void addStars(int starNum, float _x, float _y, float _r, float _l, color _t, int _m) {
for (int i = 0; i < starNum; i++) {
Star thisStar = new Star(_x, _y, _r, _l, _t, _m);
celestialPlane = (Star[]) append(celestialPlane, thisStar);
}
}
}
class Star {
// Physical Properties
float x, y;
float radius;
float luminosity;
color temperature;
// Dynamic Properties
int movType; // 0: static, 1:random, 2:linear, 3:circular+random, 4:walk
float xmove, ymove;
// Neighboring stars
float[] distNeigh;
int[] idxNeigh;
Star() { //no impact
// Init a static, random physics star
x = random(width);
y = random(height);
radius = random(2);
luminosity = random(100);
//temperature = color( random(255), 0, random(255) );
//temperature = color(255,255,255,20);
temperature = color(0, 146, 70); //slush green
movType = 0;
xmove = random(10) - 5;
ymove = random(10) - 5;
distNeigh = new float[3];
idxNeigh = new int[3];
}
Star(float _t) { //no impact
// Init dynamic or static, random physics star
x = random(width);
y = random(height);
radius = random(2);
luminosity = random(100);
temperature = color(5, 217, 0); //slush green
movType = 0;
xmove = random(10) - 5;
ymove = random(10) - 5;
distNeigh = new float[3];
idxNeigh = new int[3];
}
Star(int _m) { // HIDE THE STATIC STARS
// Init dynamic or static, random physics star
/*x = random( width );
y = random( height );
radius = random ( 2 );
luminosity = random ( 100 );
//temperature = color( random(255), 0, random(255) );
temperature = color(255,255,255,0);
movType = _m;
xmove = random( 10 ) - 5;
ymove = random( 10 ) - 5;
distNeigh = new float[1];
idxNeigh = new int[1];*/
}
Star(float _x, float _y, float _r, float _l, color _t, int _m) { //no impact
// Init dynamic or static, completely defined star
x = _x;
y = _y;
radius = _r;
luminosity = _l;
temperature = color(5, 217, 0); //slush green
movType = _m;
xmove = random(10) - 5;
ymove = random(10) - 5;
distNeigh = new float[3];
idxNeigh = new int[3];
}
void drawMe() { //moving balls
//noStroke();
strokeWeight(6); //ball and line size
//fill ( temperature, luminosity );
fill(0, 146, 70); //slush green
//stroke( temperature, luminosity );
stroke(0, 146, 70); //slush green
ellipse(x, y, radius, radius); //DRAW ELLIPSES, MOVING AND STILL
//filter(BLUR, 6);
}
void updateMe() {
switch (movType) {
case 1:
x += xmove;
y += ymove;
break;
case 2:
x += random(10) - 5;
y += random(10) - 5;
default:
}
if (x > width) x = 0;
if (x < 0) x = width;
if (y > height) y = 0;
if (y < 0) y = height;
}
void updateDist(Star[] celestialPlane) {
distNeigh[0] = 10e8;
distNeigh[1] = 10e8;
distNeigh[2] = 10e8;
for (int i = 0; i < celestialPlane.length; i++) {
Star otherStars = celestialPlane[i];
//if (otherCirc != this){
float dis = dist(x, y, otherStars.x, otherStars.y);
//println("i: ",i, ", dis:", dis);
if (dis <= distNeigh[2]) {
distNeigh[0] = distNeigh[1];
distNeigh[1] = distNeigh[2];
distNeigh[2] = dis;
idxNeigh[0] = idxNeigh[1];
idxNeigh[1] = idxNeigh[2];
idxNeigh[2] = i;
continue;
}
if (dis <= distNeigh[1]) {
distNeigh[0] = distNeigh[1];
distNeigh[1] = dis;
idxNeigh[0] = idxNeigh[1];
idxNeigh[1] = i;
continue;
}
if (dis <= distNeigh[0]) {
distNeigh[0] = dis;
idxNeigh[0] = i;
continue;
}
//}
}
}
}
/**
* Independent code for I/O control
* - Keyboard events
* - PDF and tiff export
* - Various utility functions
*
* Alessio Degani, 2015,
*
*/
boolean pause = false;
boolean savePdf = false;
boolean takePdf = false;
// ----- KEYBOARD CONTROL ------
void keyPressed() {
if (key == 'd' || key == 'D') {
takePdf = !takePdf;
}
// Take a screenshot (tiff)
if (key == 's' || key == 'S') {
saveFrame("export/" + timestamp() + ".jpg");
}
// Pause toggle
if (key == 'p' || key == 'P' || keyCode == KeyEvent.VK_SPACE) pause = !pause;
// Record a PDF
if (key == 'a' || key == 'A') {
if (savePdf) {
endRecord();
} else {
beginRecord(PDF, "export/" + timestamp() + ".pdf");
}
savePdf = !savePdf;
}
}
// ------ MISC UTILS -------
// Generate timestamp string
String timestamp() {
String time, year, month, day, hour, minute, second;
year = nf(year(), 4);
month = nf(month(), 2);
day = nf(day(), 2);
hour = nf(hour(), 2);
minute = nf(minute(), 2);
second = nf(second(), 2);
time = year + "_" + month + "_" + day + "_" + hour + "_" + minute + "_" + second;
return time;
}