I'm looking for help with the programming language/environment Processing.
I'm fairly new to Processing and I am experimenting with a piece of code from someone over at openprocessing.org. I really like the visual results this code delivers and I would like to further work with it. Unfortunately, the output is very low resolution. Hence I'm looking for someone who might be able to help me figure out how to a) Increase the size or resolution of the shapes generated and b) save everything as a pdf file.
You can find the original code in action here: https://www.openprocessing.org/sketch/377730
This is the code:
import java.util.Arrays;
float[][] z, v, a;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
size(512, 512);
colorMode(RGB, 2);
z = new float[width][height];
v = new float[width][height];
a = new float[width][height];
loadPixels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() {
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
a[x][y] = (v[x-1][y] + v[x+1][y] + v[x][y-1] + v[x][y+1])/4 - v[x][y];
}
}
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
v[x][y] += a[x][y];
z[x][y] += v[x][y];
pixels[width*y+x] = color(sin(z[x][y]) + 1, cos(z[x][y]), 1);
}
}
updatePixels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void move() {
if (mouseX > -1 && mouseX < width && mouseY > -1 && mouseY < height) {
v[mouseX][mouseY] = randomGaussian() * TAU;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mouseClicked() { move(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mouseDragged() { move(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void keyPressed() {
noLoop();
for (int x = 0; x < width; x++) Arrays.fill(z[x], 0);
for (int x = 0; x < width; x++) Arrays.fill(v[x], 0);
loop();
}
So far I have experimented with a supposed high-resolution method posted to the Processing-Forum, which hasn't worked for me though, at least not in the context of the code above I'm working with. Here's the piece of code from the forum which demonstrates one user's way of saving the output of a Processing output in a high resolution:
int dim = 5000;
int dimScreen = dim/10;
color c1 = #AFA786;
color c2 = #000000;
void setup() { size(dimScreen,dimScreen); }
void draw() { exampleSketch(); }
void exampleSketch() {
for (int y=0; y<=height; y++) {
stroke(lerpColor(c1,c2,float(y)/height));
line(0,y,width,y);
}
stroke(#FFFFFF);
fill(#BBBBBB);
ellipse(width/2, height/2, width/2, height/2);
line(0, 0, width, height);
}
void keyPressed() {
if (key ==' ') {
g = createGraphics(dim,dim,JAVA2D);
this.height = dim;
this.width = dim;
g.beginDraw();
exampleSketch();
g.endDraw();
save("result.png");
println("screenshot saved");
this.height = dimScreen;
this.width = dimScreen;
}
}
I'd be hugely grateful to anyone who's proficient enough with Processing and Java to help me out. Thanks so much and have a good night.
Here is my attempt of implementing the second code into the first one:
import processing.pdf.*;
import java.util.Arrays;
float[][] z, v, a;
int dim = 900;
int dimScreen = dim/10;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
size(900, 900);
smooth(8);
colorMode(RGB, 2);
z = new float[width][height];
v = new float[width][height];
a = new float[width][height];
loadPixels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void draw() { exampleSketch(); }
void exampleSketch() {
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
a[x][y] = (v[x-1][y] + v[x+1][y] + v[x][y-1] + v[x][y+1])/4 - v[x][y];
}
}
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
v[x][y] += a[x][y];
z[x][y] += v[x][y];
pixels[width*y+x] = color(sin(z[x][y]) + 1, cos(z[x][y]), 1);
}
}
updatePixels();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void move() {
if (mouseX > -1 && mouseX < width && mouseY > -1 && mouseY < height) {
v[mouseX][mouseY] = randomGaussian() * TAU;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mouseClicked() { move(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mouseDragged() { move(); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void keyPressed() {
if (key ==' ') {
g = createGraphics(dim,dim,JAVA2D);
this.height = dim;
this.width = dim;
g.beginDraw();
exampleSketch();
g.endDraw();
save("result2.png");
println("screenshot saved");
this.height = 900;
this.width = 900;
}
}
Edit: Two screenshots comparing the different visual results before and after implementing George's solution for a white background:
You're really talking about two different things:
Thing One: Making the colored blobs bigger. This is a little annoying because the algorithm uses the pixels array, so it's not as simple as calling the
scale()
function.In fact, because the algorithm works on pixels, any simple approach for changing the resolution is going to become pixelated.
The solution you got on the forum is to draw to an off-screen buffer, which won't actually change the size of the blobs. The only way to change the size of the blobs is to change your algorithm.
You might be able to modify how you're manipulating the arrays so the blobs are larger, but honestly I don't totally understand exactly what the arrays are doing now, which makes it hard to help.
Thing Two: Exporting as an image.
The easiest way to do this is to simply call the
save("ImageNameHere.png")
function. That will create an image with the same size as your sketch that contains whatever was showing on the screen when the function was called.The solution you found on the forum uses an off-screen buffer, but again: that won't help with the size of the blobs. You can indeed use an off-screen buffer to draw to an image that's larger than the sketch window, but that's only half of what you want. Saving this way is useless if your blobs are still small!
So, my advice for you is to fix Thing One first, and come up with an algorithm that generates larger blobs. Then we can talk about scaling and exporting as an image, which will be pretty easy once you have the algorithm working.
In addition to Kevin's answer, you can use JVisualVM to see where most of the CPU time is spent on. After sampling and profiling the CPU at different times surprisingly most of the time was computing the RGB values:
It's best to start with the functions taking most of the CPU when optimizing.
Here is a version of the sketch that uses the default 0-255 RGB range and computes the RGB value inlined (putting the A,R,G,B bytes together):
Update: Here's an a modified function that maps the values so the background is white:
Heres what I could save:
Update 2: Here is a version that computes a transparency value instead of white:
Unfortunately I don't have the time to get into more detail and provide a fast working solution, but I can provide some pointers that may help:
This looks like a simplified BZ or Grey Scott reaction diffusion simulation:
Check out Daniel Shiffman's video tutorial on it: it will help you understand the algorithm better and write a more efficient implementation.
There are a couple more hardcore methods of speeding this up that come to mind: