Source code: exptree/operators/mandel.java
1 /* Evolvo - Image Generator
2 * Copyright (C) 2000 Andrew Molloy
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /*
20 * @(#)mandel.java 0.1 08/19/2000
21 */
22 package exptree.operators;
23
24 import java.io.*;
25 import exptree.*;
26
27 /**
28 * Returns a value based on a mandelbrot set.
29 *
30 * This function takes 6 parameters. The first four define a rectangular space within the mandelbrot set.
31 * The next two define a point inside that space, relative to that space's own coordinate system.
32 *
33 * The value returned is in [-1.0,1.0], where -1.0 is the value chosen for the inside of the set, and 1.0 is that
34 * for the outside.
35 *
36 * @version 1.1 08/19/2000
37 * @author Andy Molloy
38 */
39 public class mandel implements operatorInterface, Serializable
40 {
41
42 // parameters for this operator are used as such:
43 //
44 // 0=x1 x1,y1 *------------------------+ tx = translated x, ty = translated y
45 // 1=y1 | | tx = (x2-x1) * ((x + 1) / 2) + x1
46 // 2=x2 | * tx, ty | ty = (y2-y1) * ((y + 1) / 2) + y1
47 // 3=y2 | |
48 // 4=x | |
49 // 5=y +------------------------* x2, y2
50
51 /** Perform the operation. */
52 public double evaluate(expressionTree params[])
53 {
54 double x1 = params[0].evaluate();
55 double y1 = params[1].evaluate();
56 double x2 = params[2].evaluate();
57 double y2 = params[3].evaluate();
58 double x = params[4].evaluate();
59 double y = params[5].evaluate();
60 double OLD_A;
61 int Iteration;
62 double A, B;
63 double lengthz;
64
65 double CA;
66 double CBi;
67
68 if ( x1 > x2 )
69 {
70 double temp = x1;
71 x1 = x2;
72 x2 = temp;
73 }
74 if ( y1 > y2 )
75 {
76 double temp = y1;
77 y1 = y2;
78 y2 = temp;
79 }
80
81 CA = (x2 - x1) * ((x + 1.0) / 2.0) + x1;
82 CBi = (y2 - y1) * ((y + 1.0) / 2.0) + y1;
83
84 A = 0.0;
85 B = 0.0;
86
87 Iteration = 0;
88
89 do
90 {
91 OLD_A = A;
92
93 A = A*A - B*B + CA;
94 B = 2*OLD_A*B + CBi;
95
96 Iteration++;
97
98 lengthz = A*A + B*B;
99 }
100 while ( (lengthz <= 4.0) && (Iteration < 255) );
101
102 return( (double)Iteration / 128.0 - 1.0 );
103 }
104
105 /** Returns the operator's name. */
106 public String getName()
107 {
108 return "mandel";
109 }
110
111 /** Performs any initialization the operator requires. */
112 public void init() {}
113
114 /** Returns the number of parameters expected by the operator. */
115 public int getNumberOfParameters() { return 6; }
116 }