I'm just adapting a Java program I found on the net (thanks Justin Seyster for releasing into the public domain). The original program is here:
http://www.ic.sunysb.edu/Stu/jseyster/plasma/I've got it working, but it's drawing a grid over the fractal and I can't see why. Any suggestions welcome, thanks.
EDIT: Doesn't actually need the Buffer - can write the program without it. Doesn't fix the problem, though.
Code:PGraphics Buffer; //A buffer used to store the image
void setup()
{
size(600, 600);
noStroke();
colorMode(RGB, 1.0, 1.0, 1.0);
Buffer = createGraphics(width, height, P2D);
drawPlasma(width, height);
}
void draw()
{
}
float Displace(float num)
{
float max = num / (float)(width + height) * 3;
return random(0.5)* max;
}
//Returns a color based on a color value, c.
color ComputeColor(float c)
{
float Red = 0;
float Green = 0;
float Blue = 0;
if (c < 0.5)
{
Red = c * 2;
}
else
{
Red = (1.0 - c) * 2;
}
if (c >= 0.3f && c < 0.8f)
{
Green = (c - 0.3) * 2;
}
else if (c < 0.3)
{
Green = (0.3 - c) * 2;
}
else
{
Green = (1.3 - c) * 2;
}
if (c >= 0.5)
{
Blue = (c - 0.5) * 2;
}
else
{
Blue = (0.5 - c) * 2;
}
return color(Red, Green, Blue);
}
//This is something of a "helper function" to create an initial grid
//before the recursive function is called.
void drawPlasma(int w, int h)
{
float c1, c2, c3, c4;
//Assign the four corners of the intial grid random color values
//These will end up being the colors of the four corners of the applet.
c1 = random(0,1);
c2 = random(0,1);
c3 = random(0,1);
c4 = random(0,1);
DivideGrid(0, 0, w , h , c1, c2, c3, c4);
image(Buffer, 0 ,0);
}
//This is the recursive function that implements the random midpoint
//displacement algorithm. It will call itself until the grid pieces
//become smaller than one pixel.
void DivideGrid(float x, float y, float w, float h, float c1, float c2, float c3, float c4)
{
float Edge1, Edge2, Edge3, Edge4, Middle;
float newWidth = w / 2;
float newHeight = h / 2;
if (w > 2 || h > 2)
{
Middle =(c1 + c2 + c3 + c4) / 4 + Displace(newWidth + newHeight); //Randomly displace the midpoint!
Edge1 = (c1 + c2) / 2; //Calculate the edges by averaging the two corners of each edge.
Edge2 = (c2 + c3) / 2;
Edge3 = (c3 + c4) / 2;
Edge4 = (c4 + c1) / 2;
//Make sure that the midpoint doesn't accidentally "randomly displaced" past the boundaries!
if (Middle < 0)
{
Middle = 0;
}
else if (Middle > 1.0)
{
Middle = 1.0;
}
//Do the operation over again for each of the four new grids.
DivideGrid(x, y, newWidth, newHeight, c1, Edge1, Middle, Edge4);
DivideGrid(x + newWidth, y, newWidth, newHeight, Edge1, c2, Edge2, Middle);
DivideGrid(x + newWidth, y + newHeight, newWidth, newHeight, Middle, Edge2, c3, Edge3);
DivideGrid(x, y + newHeight, newWidth, newHeight, Edge4, Middle, Edge3, c4);
}
else //This is the "base case," where each grid piece is less than the size of a pixel.
{
//The four corners of the grid piece will be averaged and drawn as a single pixel.
float c = (c1 + c2 + c3 + c4) / 4;
Buffer.beginDraw();
Buffer.noStroke();
Buffer.set((int)x, (int)y, ComputeColor(c));
Buffer.endDraw();
}
}
//Draw a new plasma fractal whenever the applet is clicked.
void mouseReleased()
{
drawPlasma(width, height);
}