01: /*
02: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
03:
04: This program is free software; you can redistribute it and/or
05: modify it under the terms of the GNU General Public License
06: as published by the Free Software Foundation; either version 2
07: of the License, or (at your option) any later version.
08:
09: 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: package org.homedns.dade.jcgrid.worker.impl.mandel;
20:
21: import org.homedns.dade.jcgrid.*;
22: import org.homedns.dade.jcgrid.worker.*;
23:
24: public class MandelWorker implements Worker {
25: public MandelWorker() {
26: }
27:
28: private short mandelIterate(double x, double y, short maxIter) {
29: double r = 0.0;
30: double i = 0.0;
31: double m = 0.0;
32:
33: short iter = 1;
34: while ((iter < maxIter) && (m < 4.0)) {
35: iter++;
36: m = r * r - i * i;
37: i = 2.0 * r * i + y;
38: r = m + x;
39: }
40:
41: return --iter;
42: }
43:
44: public WorkResult doWork(WorkRequest work, String workDir)
45: throws Exception {
46: MandelWorkRequest mwr = (MandelWorkRequest) work;
47:
48: // Check request parameters
49:
50: if (mwr.getMaxIter() > MandelConfig.MANDELGRID_MAX_ITER)
51: mwr.setMaxIter(MandelConfig.MANDELGRID_MAX_ITER);
52: else if (mwr.getMaxIter() < MandelConfig.MANDELGRID_MIN_ITER)
53: mwr.setMaxIter(MandelConfig.MANDELGRID_MIN_ITER);
54:
55: if (mwr.getXStep() > MandelConfig.MANDELGRID_FRAG_SIZE)
56: mwr.setXStep(MandelConfig.MANDELGRID_FRAG_SIZE);
57: else if (mwr.getXStep() < 1)
58: mwr.setYStep(1);
59:
60: if (mwr.getYStep() > MandelConfig.MANDELGRID_FRAG_SIZE)
61: mwr.setYStep(MandelConfig.MANDELGRID_FRAG_SIZE);
62: else if (mwr.getYStep() < 1)
63: mwr.setYStep(1);
64:
65: // Render fragment
66:
67: short[][] iter = new short[mwr.getXStep()][mwr.getYStep()];
68:
69: double y = mwr.getYOrig();
70: for (int j = 0; j < mwr.getYStep(); j++) {
71: double x = mwr.getXOrig();
72:
73: for (int i = 0; i < mwr.getXStep(); i++) {
74: iter[i][j] = this .mandelIterate(x, y, mwr.getMaxIter());
75:
76: x += mwr.getDX();
77: }
78:
79: y += mwr.getDY();
80: }
81:
82: return new MandelWorkResult(mwr.getSessionName(), mwr.getRID(),
83: iter);
84: }
85: }
|