-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Samuel Abramov
committed
Nov 9, 2023
1 parent
c05380c
commit 476bd07
Showing
10 changed files
with
289 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
rootProject.name = "MandelbrotGan" | ||
rootProject.name = "FractalityLab" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 31 additions & 37 deletions
68
src/main/java/de/fractalitylab/generators/MandelbrotGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,59 @@ | ||
package de.fractalitylab.generators; | ||
|
||
import de.fractalitylab.data.ImageWriter; | ||
import de.fractalitylab.data.DataElement; | ||
import de.fractalitylab.data.ImageWriter; | ||
|
||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.awt.image.BufferedImage; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.logging.Logger; | ||
import java.util.stream.IntStream; | ||
|
||
public class MandelbrotGenerator implements ImageGenerator { | ||
private static final Logger LOGGER = Logger.getLogger(MandelbrotGenerator.class.getName()); | ||
|
||
ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
|
||
@Override | ||
public List<DataElement> generateImage(int width, int height, int maxIterations, int numberOfImages) { | ||
List<DataElement> result = new ArrayList<>(); | ||
int iterations = maxIterations * 100; | ||
IntStream.range(1, numberOfImages + 1).parallel().forEach(imageNumber -> { | ||
IntStream.range(1, numberOfImages + 1).forEach(imageNumber -> { | ||
BufferedImage image; | ||
do { | ||
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||
double zoom = 1000.0 + random.nextDouble() * 10000.0; | ||
double moveX = -0.7 + random.nextDouble() * 0.7; | ||
double moveY = random.nextDouble() * 0.7; | ||
BufferedImage finalImage = image; | ||
IntStream.range(0, height).parallel().forEach(y -> { | ||
for (int x = 0; x < width; x++) { | ||
double zx = (x - width / 2.0) / zoom + moveX; | ||
double zy = (y - height / 2.0) / zoom + moveY; | ||
double cX = zx; | ||
double cY = zy; | ||
int iter = 0; | ||
double tmp; | ||
while ((zx * zx + zy * zy < 4) && (iter < iterations)) { | ||
tmp = zx * zx - zy * zy + cX; | ||
zy = 2.0 * zx * zy + cY; | ||
zx = tmp; | ||
iter++; | ||
} | ||
int color = Color.HSBtoRGB((float) iter / iterations + (iter % 2) * 0.5f, 1, iter < iterations ? 1 : 0); | ||
finalImage.setRGB(x, y, color); | ||
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | ||
double zoom = 1000.0 + random.nextDouble() * 10000.0; | ||
double moveX = -0.7 + random.nextDouble() * 0.7; | ||
double moveY = random.nextDouble() * 0.7; | ||
BufferedImage finalImage = image; | ||
IntStream.range(0, height).parallel().forEach(y -> { | ||
for (int x = 0; x < width; x++) { | ||
double zx = (x - width / 2.0) / zoom + moveX; | ||
double zy = (y - height / 2.0) / zoom + moveY; | ||
double cX = zx; | ||
double cY = zy; | ||
int iter = 0; | ||
double tmp; | ||
while ((zx * zx + zy * zy < 4) && (iter < iterations)) { | ||
tmp = zx * zx - zy * zy + cX; | ||
zy = 2.0 * zx * zy + cY; | ||
zx = tmp; | ||
iter++; | ||
} | ||
}); | ||
} while (isImageAllBlack(image)); | ||
int color = Color.HSBtoRGB((float) iter / iterations + (iter % 2) * 0.5f, 1, iter < iterations ? 1 : 0); | ||
finalImage.setRGB(x, y, color); | ||
} | ||
}); | ||
UUID uuid = UUID.randomUUID(); | ||
ImageWriter.writeImage("mandelbrot", uuid.toString(), image); | ||
result.add(new DataElement(uuid.toString(), "mandelbrot")); | ||
}); | ||
|
||
LOGGER.info("Mandelbrot generation finished."); | ||
|
||
return result; | ||
} | ||
|
||
private boolean isImageAllBlack(BufferedImage image) { | ||
for (int y = 0; y < image.getHeight(); y++) { | ||
for (int x = 0; x < image.getWidth(); x++) { | ||
if (image.getRGB(x, y) != Color.BLACK.getRGB()) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.