-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMandelbrot.java
More file actions
84 lines (73 loc) · 2.18 KB
/
Copy pathMandelbrot.java
File metadata and controls
84 lines (73 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
keep the check-if-in-set cap at 50 for now
Mandelbrot
Z starts at 0
p is the point we are checking if in the set or not
next z = (previous z^2) + p
if next Z is not larger than 2, then p is in the set
FEEL FREE TO ADD MORE METHODS
*/
import java.util.*;
//import Complex.java;
//import Fractal?
public class Mandelbrot extends Fractal{
//constructor
public Mandelbrot(Complex low, Complex high, int nRows, int nCols, int maxIters)
{
this.low = low;
this.high = high;
this.nrows = nRows;
this.ncols = nCols;
this.maxIters = maxIters;
this.c = new Complex(0.0, 0.0);
this.escapeVals = new int[nrows][ncols];
this.escapeVals = escapes();
}
public int escapeCount(Complex p)
{
//implementing Fractal's method
//Z starts at 0
//p is the point we are checking if in the set or not
Complex z = new Complex(0.0, 0.0);
return escapeCountHelper(0, z, p);
}
public int escapeCountHelper(int count, Complex z, Complex p)
{
//next z = (previous z^2) + p
Complex newZ = Complex.add(Complex.mul(z, z), p);
//do we stop?
if((count==maxIters)||(Complex.abs(newZ)>2.0))
return count;
//recursion!
return escapeCountHelper(count+1, newZ, p);
}
@Override public String toString()
{
String s = "";
s = s + Integer.toString(nrows)+" "+Integer.toString(ncols)+" "+Integer.toString(maxIters)+"\n";
s = s + Double.toString(low.r)+" "+Double.toString(high.r)+" "+Double.toString(low.i)+" "+Double.toString(high.i)+"\n";
s = s + Double.toString(c.r)+" "+Double.toString(c.i)+"\n\n";
for(int[] arr : escapeVals)
{
for(int num : arr)
{
s = s + Integer.toString(num) + "\t";
}
s = s + "\n";
}
return s;
}
public static void main(String[] args)
{
//eight arguments are accepted
//doubles lowR, highR, lowI, highI
//ints nRows, nCols, maxIters
//the filename
//create a Mandelbrot object
Complex low = new Complex(Double.parseDouble(args[0]), Double.parseDouble(args[2]));
Complex high = new Complex(Double.parseDouble(args[1]), Double.parseDouble(args[3]));
Mandelbrot mad = new Mandelbrot(low, high, Integer.parseInt(args[4]), Integer.parseInt(args[5]), Integer.parseInt(args[6]));
//write to the file
mad.write(args[7]);
}
}