Registered Member #540
Joined: Mon Feb 19 2007, 07:49PM
Location: MIT
Posts: 969
|
Hi. I tried coding up a program to render a Julia set given the complex number c. For some reason, the code's algorithm always escapes to infinity leading it to plot no points in the window. Can someone look over my code and try to find what is causing it to do this?
import gpdraw.DrawingTool;
import gpdraw.SketchPad;
public class JuliaSet {
private double Cre, Cim;
private double newRe, newIm, oldRe, oldIm;
private int a = -100;
private int b = -100;
private int maxIterations = 150;
SketchPad paper = new SketchPad(200, 200);
DrawingTool pen = new DrawingTool(paper);
public JuliaSet(double re, double im) {
Cre = re;
Cim = im;
}
public void render() {
for (int re = a; re <= 100; re++) {
for (int im = b; im <= 100; im++) {
newRe = re / 100.0;
newIm = im / 100.0;
int k;
for (k = 0; k < maxIterations; k++) {
oldRe = newRe;
oldIm = newIm;
newRe = oldRe * oldRe - oldIm * oldIm + Cre;
newIm = 2 * oldRe * oldIm + Cim;
if ((newRe * newRe + newIm * newIm) > 100)
break;
}
if ((newRe * newRe + newIm * newIm) <= 4)
pointAt(re, im);
}
}
}
public void pointAt(int x, int y) {
System.out.println(x + " " + y);
pen.up();
pen.move(x, y);
pen.down();
pen.move(x + 1, y);
}
public static void main(String[] args) {
JuliaSet set = new JuliaSet(1, 0);
set.render();
}
} EDIT: Nevermind, I got it working and here is the working code: (I still need to implement the color part of the fractal)
import java.awt.Color;
import gpdraw.DrawingTool;
import gpdraw.SketchPad;
public class JuliaSet {
private double Cre, Cim;
private double newRe, newIm, oldRe, oldIm;
private int a = -800;
private int b = -800;
private int maxIterations = 256;
private double zoom = 400;
SketchPad paper = new SketchPad(1600, 1600);
DrawingTool pen = new DrawingTool(paper);
public JuliaSet(double re, double im) {
Cre = re;
Cim = im;
}
public void render() {
for (int re = a; re <= Math.abs(a); re++) {
for (int im = b; im <= Math.abs(b); im++) {
newRe = re / zoom;
newIm = im / zoom;
int k;
for (k = 0; k < maxIterations; k++) {
oldRe = newRe;
oldIm = newIm;
newRe = oldRe * oldRe - oldIm * oldIm + Cre;
newIm = 2 * oldRe * oldIm + Cim;
if ((newRe * newRe + newIm * newIm) > 4)
break;
}
if ((newRe * newRe + newIm * newIm) <= 4)
pointAt(re, im);
// else
// pointAt(re, im, new Color(Color.HSBtoRGB(k/(float)maxIterations, 1, 1)));
}
}
System.out.println("DONE RENDERING");
}
public void pointAt(int x, int y) {
pen.up();
pen.move(x, y);
pen.down();
pen.move(x + 1, y);
}
// public void pointAt(int x, int y, Color col) {
// System.out.println(x + " " + y);
// pen.up();
// pen.setColor(col);
// pen.move(x, y);
// pen.down();
// pen.move(x + 1, y);
// }
public static void main(String[] args) {
JuliaSet set = new JuliaSet(-1, 0);
set.render();
}
} And here is a screenshot of the julia set for when C = -1.17-1.6i:
|