77 lines
2.0 KiB
Java
77 lines
2.0 KiB
Java
import java.util.Arrays;
|
|
|
|
public class EscapeJava {
|
|
|
|
public static final int TIMES = 100;
|
|
public static final int SAMPLES = 100;
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
double[] collectedTimes = new double[TIMES];
|
|
|
|
for (int i = 0; i < TIMES; i++) {
|
|
long[] samples = new long[SAMPLES];
|
|
for (int j = 0; j < SAMPLES; j++) {
|
|
long start = System.nanoTime();
|
|
|
|
int[][] a = calculate(generateGrid(i), i);
|
|
|
|
long end = System.nanoTime();
|
|
|
|
long elapsed = end - start;
|
|
samples[j] = elapsed;
|
|
}
|
|
collectedTimes[i] = Arrays.stream(samples).mapToDouble(v -> v/10e9).min().orElse(-1);
|
|
}
|
|
Arrays.stream(collectedTimes).forEach(System.out::print);
|
|
}
|
|
|
|
public static int escape(double cx, double cy, int maximum, double bounds) {
|
|
int i = 0;
|
|
double x = 0;
|
|
double y = 0;
|
|
|
|
while (Math.sqrt(x * x + y * y) < bounds && i < maximum) {
|
|
y = 2 * x * y + cy;
|
|
x = x * x - y * y + cx;
|
|
i++;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
static class Complex {
|
|
public double real;
|
|
public double imag;
|
|
|
|
Complex(double real, double imag) {
|
|
this.real = real;
|
|
this.imag = imag;
|
|
}
|
|
}
|
|
|
|
public static Complex[][] generateGrid(int n) {
|
|
Complex[][] grid = new Complex[n][n];
|
|
for (int yi = 0; yi < n; yi++) {
|
|
for (int xi = 0; xi < n; xi++) {
|
|
double cx = -2.0 + (4.0/n) * xi;
|
|
double cy = -2.0 + (4.0/n) * yi;
|
|
|
|
grid[yi][xi] = new Complex(cx, cy);
|
|
}
|
|
}
|
|
return grid;
|
|
}
|
|
|
|
public static int[][] calculate(Complex[][] grid, int n) {
|
|
int[][] results = new int[n][n];
|
|
for (int yi = 0; yi < n; yi++) {
|
|
for (int xi = 0; xi < n; xi++) {
|
|
Complex c = grid[yi][xi];
|
|
results[yi][xi] = escape(c.real, c.imag, 10, 2);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
}
|