package ip.raul;
public class Fractals {
int maxIter = 30;
public static float xMin = -2f;
public static float yMax = 1.5f;
public static float xMax = 1f;
public static float yMin = -1.5f;
short colorR[] = new short[maxIter];
short colorG[] = new short[maxIter];
short colorB[] = new short[maxIter];
public Fractals() {
float deltaTheta = (float) (Math.PI / maxIter);
for (int i = 0; i < maxIter; i++) {
colorR[i] = (short) (Math.sin((float) (i * deltaTheta)) * 255f);
colorG[i] = 0;
colorB[i] = (short) (i * 255 / maxIter);
}
}
public int getColor(float pixelr, float pixeli) {
float zr = 0;
float zi = 0;
float tmp = 0;
int iter = 0;
do {
iter++;
tmp = zr;
zr = zr * zr - zi * zi;
zi = 2 * tmp * zi;
zr = zr + pixelr;
zi = zi + pixeli;
} while (((zr * zr + zi * zi) <= 300) && (iter <= maxIter));
if (iter > maxIter)
return -1;
else
return iter;
}
public void mandelbrot(short[][] r, short[][] g, short[][] b) {
int height = r[0].length;
int width = r.length;
int Clr;
float pixelr,pixeli;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
pixelr = xMin + (float) x / width * (xMax - xMin);
pixeli = yMin + (float) y / height * (yMax - yMin);
Clr = getColor(pixelr, pixeli);
if (Clr == -1) {
r[x][y] = 255;
g[x][y] = 128;
b[x][y] = 0;
} else {
r[x][y] = colorR[Clr % maxIter];
g[x][y] = colorG[Clr % maxIter];
b[x][y] = colorB[Clr % maxIter];
}
}
}
}