mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-29 15:34:21 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -27,13 +27,23 @@ public class Mandelbrot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Test black and white
|
||||
BufferedImage blackAndWhiteImage = getImage(800, 600, -0.6, 0, 3.2, 50, false);
|
||||
BufferedImage blackAndWhiteImage = getImage(
|
||||
800,
|
||||
600,
|
||||
-0.6,
|
||||
0,
|
||||
3.2,
|
||||
50,
|
||||
false
|
||||
);
|
||||
|
||||
// Pixel outside the Mandelbrot set should be white.
|
||||
assert blackAndWhiteImage.getRGB(0, 0) == new Color(255, 255, 255).getRGB();
|
||||
assert blackAndWhiteImage.getRGB(0, 0) ==
|
||||
new Color(255, 255, 255).getRGB();
|
||||
|
||||
// Pixel inside the Mandelbrot set should be black.
|
||||
assert blackAndWhiteImage.getRGB(400, 300) == new Color(0, 0, 0).getRGB();
|
||||
assert blackAndWhiteImage.getRGB(400, 300) ==
|
||||
new Color(0, 0, 0).getRGB();
|
||||
|
||||
// Test color-coding
|
||||
BufferedImage coloredImage = getImage(800, 600, -0.6, 0, 3.2, 50, true);
|
||||
@ -71,44 +81,62 @@ public class Mandelbrot {
|
||||
* @return The image of the rendered Mandelbrot set.
|
||||
*/
|
||||
public static BufferedImage getImage(
|
||||
int imageWidth,
|
||||
int imageHeight,
|
||||
double figureCenterX,
|
||||
double figureCenterY,
|
||||
double figureWidth,
|
||||
int maxStep,
|
||||
boolean useDistanceColorCoding) {
|
||||
int imageWidth,
|
||||
int imageHeight,
|
||||
double figureCenterX,
|
||||
double figureCenterY,
|
||||
double figureWidth,
|
||||
int maxStep,
|
||||
boolean useDistanceColorCoding
|
||||
) {
|
||||
if (imageWidth <= 0) {
|
||||
throw new IllegalArgumentException("imageWidth should be greater than zero");
|
||||
throw new IllegalArgumentException(
|
||||
"imageWidth should be greater than zero"
|
||||
);
|
||||
}
|
||||
|
||||
if (imageHeight <= 0) {
|
||||
throw new IllegalArgumentException("imageHeight should be greater than zero");
|
||||
throw new IllegalArgumentException(
|
||||
"imageHeight should be greater than zero"
|
||||
);
|
||||
}
|
||||
|
||||
if (maxStep <= 0) {
|
||||
throw new IllegalArgumentException("maxStep should be greater than zero");
|
||||
throw new IllegalArgumentException(
|
||||
"maxStep should be greater than zero"
|
||||
);
|
||||
}
|
||||
|
||||
BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
|
||||
BufferedImage image = new BufferedImage(
|
||||
imageWidth,
|
||||
imageHeight,
|
||||
BufferedImage.TYPE_INT_RGB
|
||||
);
|
||||
double figureHeight = figureWidth / imageWidth * imageHeight;
|
||||
|
||||
// loop through the image-coordinates
|
||||
for (int imageX = 0; imageX < imageWidth; imageX++) {
|
||||
for (int imageY = 0; imageY < imageHeight; imageY++) {
|
||||
// determine the figure-coordinates based on the image-coordinates
|
||||
double figureX = figureCenterX + ((double) imageX / imageWidth - 0.5) * figureWidth;
|
||||
double figureY = figureCenterY + ((double) imageY / imageHeight - 0.5) * figureHeight;
|
||||
double figureX =
|
||||
figureCenterX +
|
||||
((double) imageX / imageWidth - 0.5) *
|
||||
figureWidth;
|
||||
double figureY =
|
||||
figureCenterY +
|
||||
((double) imageY / imageHeight - 0.5) *
|
||||
figureHeight;
|
||||
|
||||
double distance = getDistance(figureX, figureY, maxStep);
|
||||
|
||||
// color the corresponding pixel based on the selected coloring-function
|
||||
image.setRGB(
|
||||
imageX,
|
||||
imageY,
|
||||
useDistanceColorCoding
|
||||
? colorCodedColorMap(distance).getRGB()
|
||||
: blackAndWhiteColorMap(distance).getRGB());
|
||||
imageX,
|
||||
imageY,
|
||||
useDistanceColorCoding
|
||||
? colorCodedColorMap(distance).getRGB()
|
||||
: blackAndWhiteColorMap(distance).getRGB()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -177,7 +205,11 @@ public class Mandelbrot {
|
||||
* @param maxStep Maximum number of steps to check for divergent behavior.
|
||||
* @return The relative distance as the ratio of steps taken to maxStep.
|
||||
*/
|
||||
private static double getDistance(double figureX, double figureY, int maxStep) {
|
||||
private static double getDistance(
|
||||
double figureX,
|
||||
double figureY,
|
||||
int maxStep
|
||||
) {
|
||||
double a = figureX;
|
||||
double b = figureY;
|
||||
int currentStep = 0;
|
||||
|
Reference in New Issue
Block a user