Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
emosher
committed
Jul 7, 2012
1 parent
9d488ab
commit 773504b
Showing
4 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mandelbrot; | ||
|
||
/** | ||
* A color scheme. | ||
* @author Eric Mosher | ||
*/ | ||
public class ColorScheme { | ||
|
||
// Store the maximum iterations as a number to use in the calculations | ||
// of color. | ||
private int maxIterations; | ||
|
||
/** | ||
* Construct a color scheme object | ||
* @param maxIterations for use in calculation | ||
*/ | ||
public ColorScheme(int maxIterations){ | ||
this.maxIterations = maxIterations; | ||
} | ||
|
||
/** | ||
* Get the color of a point | ||
* @param i the number to color | ||
* @return the color in this color scheme | ||
*/ | ||
public int getColor(int i) { | ||
// A color scheme | ||
int a = (int) (255 * ((double) i) / (maxIterations / 4)); | ||
return //( (2*a<<16) ); | ||
// Other options of varying qualities... | ||
//( (255 * (i/15)) << 16 | (255 * (i/15)) ); | ||
//((255 * (i/20)) << 16 | 0 | 0 ); | ||
//((255 * (i/10)) << 16 | (255 * (i/10)) << 8 | (255 * (i/10)) ); | ||
(65536 + i*256 + i/2+128); | ||
//( (0) | (2*a<<16) | (a<<8) | ((a*2)<<0) ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package mandelbrot; | ||
|
||
|
||
|
||
/** | ||
* Main class in the Mandelbrot project. This will eventually, begin threads to | ||
* create different images of the Mandelbrot set. | ||
* @author Eric Mosher | ||
*/ | ||
public class Main { | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
// The number of threads to use. We will use 4 because I have a | ||
// Quad-core processor. (Intel i5) | ||
int numThreads = 4; | ||
// The threads used in this program | ||
Thread[] threads = new Thread[numThreads]; | ||
// The number of pictures per thread | ||
int picsPerThread = 3; | ||
// The names of the files will be mbrot1, mbrot2, etc. | ||
String fName = "mbrot"; | ||
// The number we will attach to the file name | ||
int count = 1; | ||
// The beginning resolution | ||
int resolution = 500; | ||
// The image center | ||
double xCenter = -0.38; | ||
double yCenter = -0.665; | ||
// Number of total iterations | ||
int maxIterations = 1000; | ||
|
||
ColorScheme color = new ColorScheme(maxIterations); | ||
|
||
// For the number of threads being used... | ||
for (int a = 0; a < numThreads; a++) { | ||
// Create an array to give to each thread | ||
MbrotParameter[] mbrots = new MbrotParameter[picsPerThread]; | ||
// For every parameter in the array... | ||
for (int b = 0; b < picsPerThread; b++) { | ||
// Create the parameter and assign it to the place in the array | ||
mbrots[b] = new MbrotParameter(fName + String.valueOf(count), | ||
resolution, xCenter, yCenter); | ||
// Update our data | ||
count++; | ||
resolution += 1000; | ||
} | ||
// Create a new thread with the appropriate array as the parameter. | ||
threads[a] = new Thread(new MandelbrotRenderer(mbrots, color, | ||
maxIterations)); | ||
} | ||
|
||
// Start the threads | ||
for (Thread t : threads) { | ||
t.start(); | ||
} | ||
|
||
// Join the threads | ||
for (Thread t : threads) { | ||
try { | ||
t.join(); | ||
} | ||
catch (Exception e) {} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package mandelbrot; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import javax.imageio.ImageIO; | ||
|
||
/** MandelbrotRenderer.java | ||
* The thread class that will do the work of making images of the Mandelbrot set. | ||
* Much of the computation is or is based on Dr. Matt Lang's code. | ||
* @author Eric Mosher, Dr. Matt Lang | ||
*/ | ||
public class MandelbrotRenderer implements Runnable { | ||
|
||
// The size of all images | ||
private int width = 1920; | ||
private int height = 1080; | ||
|
||
// The number of maximum iterations | ||
private int maxIterations; | ||
|
||
// Divergence radius | ||
private double radius = 2; | ||
private double divergence = radius * radius; | ||
|
||
// Offsets between image[0][0] and center point | ||
private int xOffset = -(width - 1)/2; | ||
private int yOffset = (height - 1)/2; | ||
|
||
private int black = 0; | ||
|
||
private MbrotParameter[] mbrots; | ||
|
||
private ColorScheme color; | ||
|
||
/** | ||
* This class needs a list of parameters to iterate over, this is taken as | ||
* an array of MbrotParameters. | ||
* @param imgs the images to produce | ||
* @param color the color scheme to use | ||
*/ | ||
public MandelbrotRenderer(MbrotParameter[] imgs, ColorScheme color, | ||
int maxIterations) { | ||
mbrots = imgs; | ||
this.color = color; | ||
this.maxIterations = maxIterations; | ||
} | ||
|
||
/** | ||
* This class will be used as a thread, so we must use run() to do our work. | ||
*/ | ||
public void run() { | ||
int iteration, point; | ||
double a, b, aOld, x, y; | ||
int[] colorPalette = new int[maxIterations]; | ||
// Fill the color palette | ||
for (int i = 0; i < maxIterations; i++) { | ||
colorPalette[i] = color.getColor(i); | ||
} | ||
|
||
// For each element in the list of MbrotParameters... | ||
for (MbrotParameter currMbrot : mbrots){ | ||
// Do the work to create each image | ||
// Create a new image | ||
BufferedImage img = new BufferedImage(width, height, | ||
BufferedImage.TYPE_INT_RGB); | ||
File out = new File(currMbrot.fName); | ||
|
||
// For each pixel | ||
for (int r = 0; r < height; r++) | ||
{ | ||
for (int c = 0; c < width; c++) | ||
{ | ||
// Calculate the point the pixel actually represents | ||
x = currMbrot.xCenter + (xOffset + c) | ||
/ currMbrot.resolution; | ||
y = currMbrot.yCenter + (yOffset - r) | ||
/ currMbrot.resolution; | ||
|
||
iteration = 0; | ||
a = x; | ||
b = y; | ||
|
||
// While the point is within the divergence radius and | ||
// the iteration bound | ||
while (a*a + b*b <= divergence && iteration < maxIterations) | ||
{ | ||
// Calculate the sequence | ||
aOld = a; | ||
a = a*a - b*b + x; | ||
|
||
b = 2*aOld*b + y; | ||
iteration ++; | ||
} | ||
|
||
/* | ||
* If the point looks as if it will diverge, color it black. | ||
* Otherwise, give it a color from our color palette. | ||
*/ | ||
if (iteration == maxIterations) | ||
point = black; | ||
else { | ||
point = colorPalette[iteration]; | ||
} | ||
// Set the color of the pixel | ||
img.setRGB(c, r, point); | ||
} | ||
} | ||
|
||
// Write the image | ||
try { | ||
ImageIO.write(img, "png", out); | ||
} | ||
catch (Exception e) { | ||
System.out.println("Oops."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mandelbrot; | ||
|
||
/** | ||
* This class will be the parameters for the MandelbrotRenderer class. This | ||
* will wrap the data that we will need for each picture. So we can think of | ||
* one instance of this class as being a picture BEFORE it is rendered. | ||
* @author Eric Mosher | ||
*/ | ||
public class MbrotParameter { | ||
// The name of the image | ||
public String fName; | ||
// The resolution | ||
public double resolution; | ||
// The center of the picture | ||
public double xCenter; | ||
public double yCenter; | ||
|
||
/** | ||
* Construct an outline of a picture, to use for rendering. | ||
* @param fName the name of the file | ||
* @param resolution the resolution of this image | ||
* @param xCenter the x value of the center | ||
* @param yCenter the y value of the center | ||
*/ | ||
public MbrotParameter(String fName, double resolution, double xCenter, | ||
double yCenter) { | ||
this.fName = fName; | ||
this.resolution = resolution; | ||
this.xCenter = xCenter; | ||
this.yCenter = yCenter; | ||
} | ||
|
||
} |