This project has retired. For details please refer to its
Attic page.
FractalCalculator xref
1////////////////////////////////////////////////////////////////////////////////2////////////////////////////////////////////////////////////////////////////////3/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.chemistry.opencmis.util.content.fractal;
2324import java.awt.Color;
25import java.awt.image.BufferedImage;
2627finalclassFractalCalculator {
28privateint[] colorMap;
29protectedint[][] noIterations;
30privatedouble delta;
31privatedouble iRangeMax;
32privatedouble iRangeMin;
33privateint maxIterations;
34privateComplexRectangle newRect;
35privateint numColors;
36privateint imageHeight;
37privateint imageWidth;
38privatedouble rRangeMax;
39privatedouble rRangeMin;
40// For Julia set:41privatedouble cJuliaPointR = 0.0; // Real42privatedouble cJuliaPointI = 0.0; // Imaginary43boolean useJulia = false;
4445publicFractalCalculator(ComplexRectangle complRect, int maxIters, int imgWidth, int imgHeight, int[] colMap,
46ComplexPoint juliaPoint) {
47 maxIterations = maxIters;
48 newRect = complRect;
49 imageWidth = imgWidth;
50 imageHeight = imgHeight;
51 colorMap = colMap;
52 numColors = colorMap.length;
53 rRangeMin = newRect.getRMin();
54 rRangeMax = newRect.getRMax();
55 iRangeMin = newRect.getIMin();
56 iRangeMax = newRect.getIMax();
57 delta = (rRangeMax - rRangeMin) / imageWidth;
58if (null != juliaPoint) {
59 cJuliaPointR = juliaPoint.getReal();
60 cJuliaPointI = juliaPoint.getImaginary();
61 useJulia = true;
62 }
63 }
6465publicint[][] calcFractal() {
66 noIterations = newint[ imageWidth ][ imageHeight ];
6768// For each pixel...69for (int x = 0; x < imageWidth; x++) {
70for (int y = 0; y < imageHeight; y++) {
71double zR = rRangeMin + x * delta;
72double zI = iRangeMin + (imageHeight - y) * delta;
7374// Is the point inside the set?75if (useJulia)
76 noIterations[x][y] = testPointJuliaSet(zR, zI, maxIterations);
77else78 noIterations[x][y] = testPointMandelbrot(zR, zI, maxIterations);
79 }
80 }
81return noIterations;
82 }
8384public BufferedImage mapItersToColors(int[][] iterations) {
8586// Assign a color to every pixel ( x , y ) in the Image, corresponding87// to88// one point, z, in the imaginary plane ( zr, zi ).89 BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_3BYTE_BGR );
9091// For each pixel...92for (int x = 0; x < imageWidth; x++) {
93for (int y = 0; y < imageHeight; y++) {
94int color = getColor(iterations[x][y]);
95 image.setRGB(x, y, color);
96 }
97 }
98return image;
99 }
100101protectedint getColor(int numIterations) {
102int c = Color.black.getRGB();
103104if (numIterations != 0) {
105// The point is outside the set. It gets a color based on the number106// of iterations it took to know this.107int colorNum = (int) (numColors * (1.0 - (float) numIterations / (float) maxIterations));
108 colorNum = (colorNum == numColors) ? 0 : colorNum;
109110 c = colorMap[colorNum];
111 }
112return c;
113 }
114115privateint testPointMandelbrot(double cR, double cI, int maxIterations) {
116// Is the given complex point, (cR, cI), in the Mandelbrot set?117// Use the formula: z <= z*z + c, where z is initially equal to c.118// If |z| >= 2, then the point is not in the set.119// Return 0 if the point is in the set; else return the number of120// iterations it took to decide that the point is not in the set.121double zR = cR;
122double zI = cI;
123124for (int i = 1; i <= maxIterations; i++) {
125// To square a complex number: (a+bi)(a+bi) = a*a - b*b + 2abi126double zROld = zR;
127 zR = zR * zR - zI * zI + cR;
128 zI = 2 * zROld * zI + cI;
129130// We know that if the distance from z to the origin is >= 2131// then the point is out of the set. To avoid a square root,132// we'll instead check if the distance squared >= 4.133double distSquared = zR * zR + zI * zI;
134if (distSquared >= 4) {
135return i;
136 }
137 }
138return 0;
139 }
140141privateint testPointJuliaSet(double zR, double zI, int maxIterations) {
142// Is the given complex point, (zR, zI), in the Julia set?143// Use the formula: z <= z*z + c, where z is the point being tested,144// and c is the Julia Set constant.145// If |z| >= 2, then the point is not in the set.146// Return 0 if the point is in the set; else return the number of147// iterations it took to decide that the point is not in the set.148for (int i = 1; i <= maxIterations; i++) {
149double zROld = zR;
150// To square a complex number: (a+bi)(a+bi) = a*a - b*b + 2abi151 zR = zR * zR - zI * zI + cJuliaPointR;
152 zI = 2 * zROld * zI + cJuliaPointI;
153// We know that if the distance from z to the origin is >= 2154// then the point is out of the set. To avoid a square root,155// we'll instead check if the distance squared >= 4.156double distSquared = zR * zR + zI * zI;
157if (distSquared >= 4) {
158return i;
159 }
160 }
161return 0;
162 }
163 }